演習課題「投稿フォームで名前を表示する」
右の環境には、ServletとJSPで投稿フォームプログラムが作成してありますが、投稿フォームに名前欄が表示されません。このプログラムを修正して、名前欄を表示するように修正してください。
採点して、すべてのジャッジに正解すれば、演習課題クリアです!
#02:投稿フォームを作ろう
ここでは、JavaでWebフォームを作る方法を学習します。そのために、ServletとJSPを使って、HTMLの投稿フォームを表示させてみましょう。
(tomcat/webapps/myform/FormServlet.java)import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class FormServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("message", "Hello World");
        String view = "/WEB-INF/views/form.jsp";
        RequestDispatcher dispatcher = request.getRequestDispatcher(view);
        dispatcher.forward(request, response);
    }
}
(tomcat/webapps/myform/WEB-INF/web.xml)<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
        http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
        version="4.0" metadata-complete="true">
    <servlet>
        <servlet-name>ShowForm</servlet-name>
        <servlet-class>FormServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ShowForm</servlet-name>
        <url-pattern>/show</url-pattern>
    </servlet-mapping>
</web-app>
(tomcat/webapps/myform/WEB-INF/views/form.jsp)<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
        <title>Java - paiza</title>
    </head>
    <body>
        <% String message = (String)request.getAttribute("message");%>
        <h1><%= message %></h1>
        <form action="result" method="post" >
            <label for="article">投稿</label>
            <input type="text" name="article">
            <p></p>
            <label for="name">名前</label>
            <input type="text" name="userName">
            <button type="submit">送信する</button>
        </form>
    </body>
</html>
$ javac -classpath "../../lib/servlet-api.jar" -d WEB-INF/classes FormServlet.java
- Javaの道>Servlet・JSP
  https://www.javaroad.jp/servletjsp/index.html
- Servletアプリ開発:目次 - Web系開発メモ
  http://web-dev.hatenablog.com/entry/java/servlet/dev-restful-app/table-of-contents
- JSP(formデータ)とJava Servletプログラムの連携 - はしくれエンジニアもどきのメモ
  http://cartman0.hatenablog.com/entry/2015/12/08/181657