Sessionを利用して、以下のようなお問い合わせフォームを作成しよう。
[実行例]
●スタート画面

●フォームに入力する

●確認ボタンを押すと、確認画面に遷移する。

●戻るを押すと、フォームに戻る(その際、入力内容が表示されている)

●内容を編集する

●確認を押す

●送信を押すと遷移してメッセージが表示される。

●送信後、再びフォーム画面をリクエストすると。入力がクリアされている。

[作成]
1.エクリプス、新規動的Project作成から「joytas6」アプリケーションを作成する。
2.modelの作成。modelパッケージにPersonクラスを以下のように作成
●model.Person.java
03 | import java.io.Serializable; |
05 | public class Person implements Serializable{ |
10 | public Person(String name,String email,String memo){ |
15 | public String getName() { |
18 | public void setName(String name) { |
21 | public String getEmail() { |
24 | public void setEmail(String email) { |
27 | public String getMemo() { |
30 | public void setMemo(String memo) { |
3.viewの作成。以下の図のようなフォルダ構成としファイルを作成する。

●form.jsp
01 | <%@ page language= "java" contentType= "text/html; charset=UTF-8" |
02 | pageEncoding= "UTF-8" import = "model.*" %> |
04 | Person person=(Person)session.getAttribute( "person" ); |
05 | String name=person== null ? "" :person.getName(); |
06 | String email=person== null ? "" :person.getEmail(); |
07 | String memo=person== null ? "" :person.getMemo(); |
12 | <meta charset= "UTF-8" /> |
13 | <link rel= "stylesheet" type= "text/css" href= "http://yui.yahooapis.com/3.18.1/build/cssreset/cssreset-min.css" > |
14 | <link rel= "stylesheet" href= "/joytas6/css/main.css" > |
15 | <title>Joytas6(Session)</title> |
19 | <form action= "/joytas6/contact" method= "post" > |
21 | <tr><th>お名前</th><td><input type= "text" name= "name" value= "<%=name%>" ></td></tr> |
22 | <tr><th>メールアドレス</th><td><input type= "email" name= "email" value= "<%=email%>" ></td></tr> |
23 | <tr><th>お問い合わせ内容</th><td><textarea name= "memo" ><%=memo%></textarea></td></tr> |
25 | <input type= "submit" value= "確認" class = "button" > |
●confirm.jsp
01 | <%@ page language= "java" contentType= "text/html; charset=UTF-8" |
02 | pageEncoding= "UTF-8" import = "model.*" %> |
04 | Person person=(Person)session.getAttribute( "person" ); |
09 | <meta charset= "UTF-8" /> |
10 | <link rel= "stylesheet" type= "text/css" href= "http://yui.yahooapis.com/3.18.1/build/cssreset/cssreset-min.css" > |
11 | <link rel= "stylesheet" href= "/joytas6/css/main.css" > |
12 | <title>Joytas6(Session)</title> |
16 | <p class = "normalP" >以下の内容でよろしいですか?</p> |
19 | <tr><th>お名前</th><td><%=person.getName() %></td></tr> |
20 | <tr><th>メールアドレス</th><td><%=person.getEmail() %></td></tr> |
21 | <tr><th>お問い合わせ内容</th><td><%=person.getMemo() %></td></tr> |
23 | <div id= "btBox" ><a href= "/joytas6/contact?mode=back" class = "button2" >戻る</a><a href= "/joytas6/contact?mode=send" class = "button2" >送信</a></div> |
●send.jsp
01 | <%@ page language= "java" contentType= "text/html; charset=UTF-8" |
02 | pageEncoding= "UTF-8" %> |
06 | <meta charset= "UTF-8" /> |
07 | <link rel= "stylesheet" type= "text/css" href= "http://yui.yahooapis.com/3.18.1/build/cssreset/cssreset-min.css" > |
08 | <link rel= "stylesheet" href= "/joytas6/css/main.css" > |
09 | <title>Joytas6(Session)</title> |
13 | <p class = "normalP" >送信完了!</p> |
●main.css
03 | box-sizing:border-box; |
09 | border-left : 10px solid #e33234 ; |
17 | border : 1px solid #555 ; |
39 | border : 2px solid #f4a895 ; |
49 | border : 2px solid #f4a895 ; |
61 | transition-duration: 300 ms; |
67 | margin : 20px auto 10px ; |
4.controllerの作成。以下のようなフォルダ構成とし、Contact.java(Servlet)を作成する。

●controller.Contact.java
03 | import java.io.IOException; |
05 | import javax.servlet.RequestDispatcher; |
06 | import javax.servlet.ServletException; |
07 | import javax.servlet.annotation.WebServlet; |
08 | import javax.servlet.http.HttpServlet; |
09 | import javax.servlet.http.HttpServletRequest; |
10 | import javax.servlet.http.HttpServletResponse; |
11 | import javax.servlet.http.HttpSession; |
15 | @WebServlet ( "/contact" ) |
16 | public class Contact extends HttpServlet { |
17 | private static final long serialVersionUID = 1L; |
19 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
21 | String mode=request.getParameter( "mode" ); |
22 | if (mode == null || mode.equals( "back" )){ |
23 | path= "/WEB-INF/view/form.jsp" ; |
25 | path= "/WEB-INF/view/send.jsp" ; |
26 | HttpSession session=request.getSession(); |
29 | RequestDispatcher rd=request.getRequestDispatcher(path); |
30 | rd.forward(request, response); |
33 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
34 | request.setCharacterEncoding( "UTF-8" ); |
35 | String name=request.getParameter( "name" ); |
36 | String email=request.getParameter( "email" ); |
37 | String memo=request.getParameter( "memo" ); |
38 | Person person= new Person(name,email,memo); |
39 | HttpSession session=request.getSession(); |
40 | session.setAttribute( "person" , person); |
41 | RequestDispatcher rd=request.getRequestDispatcher( "/WEB-INF/view/confirm.jsp" ); |
42 | rd.forward(request, response); |
5.以下のアドレスにリクエストして、実行例のようになることを確認する。
http://localhost:8080/joytas6/contact
自動返信メール
お問い合わせをした人に自動返信メールを送ってみよう。
1.まずは以下の二つのファイルをダウンロードし/WEB-INF/libフォルダ内にコピペで配置する。
2.controller.Contact.javaを以下のように修正する。
今回はyahooのメールサーバーを利用してるので各自yahooのidとパスワードを入力すること。
03 | import java.io.IOException; |
05 | import javax.servlet.RequestDispatcher; |
06 | import javax.servlet.ServletException; |
07 | import javax.servlet.annotation.WebServlet; |
08 | import javax.servlet.http.HttpServlet; |
09 | import javax.servlet.http.HttpServletRequest; |
10 | import javax.servlet.http.HttpServletResponse; |
11 | import javax.servlet.http.HttpSession; |
13 | import org.apache.commons.mail.DefaultAuthenticator; |
14 | import org.apache.commons.mail.Email; |
15 | import org.apache.commons.mail.EmailException; |
16 | import org.apache.commons.mail.SimpleEmail; |
21 | @WebServlet ( "/contact" ) |
22 | public class Contact extends HttpServlet { |
23 | private static final long serialVersionUID = 1L; |
25 | protected void doGet(HttpServletRequest request, HttpServletResponse response) |
26 | throws ServletException, IOException { |
28 | String mode = request.getParameter( "mode" ); |
29 | if (mode == null || mode.equals( "back" )) { |
30 | path = "/WEB-INF/view/form.jsp" ; |
32 | HttpSession session = request.getSession(); |
33 | Person person = (Person) session.getAttribute( "person" ); |
34 | StringBuilder sb = new StringBuilder(); |
35 | sb.append( "以下のように受け付けました" ); |
36 | sb.append(System.lineSeparator()); |
37 | sb.append( "お名前:" + person.getName()); |
38 | sb.append(System.lineSeparator()); |
39 | sb.append( "email:" + person.getEmail()); |
40 | sb.append(System.lineSeparator()); |
41 | sb.append( "お問い合わせ内容:" + person.getMemo()); |
42 | sb.append(System.lineSeparator()); |
45 | Email email = new SimpleEmail(); |
46 | email.setHostName( "smtp.mail.yahoo.co.jp" ); |
47 | email.setSmtpPort( 465 ); |
48 | email.setAuthenticator( new DefaultAuthenticator( "yahooアカウント" , "yahooパスワード" )); |
49 | email.setSSLOnConnect( true ); |
50 | email.setFrom( "yahooアカウント@yahoo.co.jp" ); |
51 | email.setSubject( "お問い合わせありがとうございます。" ); |
52 | email.setMsg(sb.toString()); |
53 | email.addTo(person.getEmail()); |
55 | } catch (EmailException e) { |
59 | path = "/WEB-INF/view/send.jsp" ; |
61 | RequestDispatcher rd = request.getRequestDispatcher(path); |
62 | rd.forward(request, response); |
66 | protected void doPost(HttpServletRequest request, HttpServletResponse response) |
67 | throws ServletException, IOException { |
68 | request.setCharacterEncoding( "UTF-8" ); |
69 | String name = request.getParameter( "name" ); |
70 | String email = request.getParameter( "email" ); |
71 | String memo = request.getParameter( "memo" ); |
72 | Person person = new Person(name, email, memo); |
73 | HttpSession session = request.getSession(); |
74 | session.setAttribute( "person" , person); |
75 | RequestDispatcher rd = request.getRequestDispatcher( "/WEB-INF/view/confirm.jsp" ); |
76 | rd.forward(request, response); |
3.フォームから送信してみよう。入力したメールアドレス宛に自動返信メールが届くはずだ。
コメント