Visualcafeのウイザードを使ってサーブレットを作成 | |||
1. | カウンターを表示。プラウザを更新すると1カウントづつ増える | ||
※青字のところのみマニュアルで追加記入
|
|||
import java.io. import javax.servlet.*; import javax.servlet.http.*; public class SimpleTest extends HttpServlet int count=1; public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html"); // to do: code goes here. out.println("<HTML>"); out.println("<HEAD><TITLE>SimpleTest output</TITLE></HEAD>"); out.println("<BODY>"); // to do: your HTML goes here. out.println("Request Count: #"+count+" vistor</td></tr><tr><td>"); out.close(); synchronized(this) { count++; } } } |
|||
2. | User名とIDを入力すると、User名とIDをhttpページで表示
|
||
@ | ログイン画面・・・userNameとpassword入力
|
||
<html> <body> <form action="http://127.0.0.1:8080/servlet/Login" method="POST"> <input type="input" name="userName" size="20"><br> <input type="password" name="password" size="20"><br> <input type="submit" value="Login"> </form> </body> </html> |
|||
A | Login・・・・・userNameさんようこそと表示し、ユーザーinfoへのリンク表示
|
||
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Login extends HttpServlet { public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //ブラウザからDATA_INPUT String userName=req.getParameter("userName"); String password=req.getParameter("password"); //sessionオブジェクトに保管 HttpSession session=req.getSession(true); session.putValue("userName",userName); session.putValue("password",password); //ブラウザ画面にDATA送信 resp.setContentType("text/html"); PrintWriter out = new PrintWriter(resp.getOutputStream()); // to do: code goes here. out.println("<HTML>"); out.println("<HEAD><TITLE>Login output</TITLE></HEAD>"); out.println("<BODY>"); // to do: your HTML goes here. out.println(<H1>"+userName+"さん、ようこそ</H1><BR>"); out.println(<a href=http://127.0.0.1:8080/servlet/UserInfo>password</a>"); out.println("</BODY>"); out.println("</HTML>"); out.close(); } } |
|||
B | UserInfo・・・・・userInfo表示
|
||
public class UserInfo extends HttpServlet { public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //sessionオブジェクトから読み込み HttpSession session=req.getSession(true); String userName=(String)session.getValue("userName"); String password=(String)session.getValue("password"); //ブラウザ画面にDATA送信 resp.setContentType("text/html"); PrintWriter out = new PrintWriter(resp.getOutputStream()); // to do: code goes here. out.println("<HTML>"); out.println("<HEAD><TITLE>Login output</TITLE></HEAD>"); out.println("<BODY>"); // to do: your HTML goes here. out.println("<H1>PasswordInfo</H1><BR>"); out.println("<table><tr><td>PasswordInfo</td></tr>"); out.println("<td>"+userName+"</td></tr>"); out.println("<tr><td></td></tr>"); out.println("<tr><td>"+password+"</td></tr></table>"); out.println("</BODY>"); out.println("</HTML>"); out.close(); } } |
|||
*localでtomcatで実行するときは
|
|||
http://127.0.0.1:8080/servlet/Login を http://127.0.0.1:8080/test99/servlet/Login
→test99は tomcatのwebappsで展開した任意のフォルダ名 |
|||