Java建立servlet hello world example (一)
專案名稱:CookieServlet
二、首頁 index.html:
程式
<!DOCTYPE>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>CookieServlet</title>
</head>
<body>
<h1>CookieServlet</h1>
<table>
<tr>
<td colspan="2" style="font-weight:bold;">測試自動登入頁:</td>
</tr>
<tr>
<td><a href="login.view">登入頁</a></td>
</tr>
</table>
</body>
</html>
三、登入頁 login.html:
程式
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>自動登入測試</title>
</head>
<body>
<form action="login.do" method="post">
帳號:<input type="text" name="user"><br>
密碼:<input type="password" name="password"><br>
自動登入:<input type="checkbox" name="login" value="auto"><br>
<input type="submit" value="送出">
</form>
<div>測試 帳號:levin 密碼:123456</div>
</body>
</html>
三、後端接收資料程式
1、登入程式
登入送出,後端接收資料程式,並判斷是否有設定自動登入。
如有勾選自動登入,將資料存入Cookie裡。
新增 LoginServlet.java :
程式
package com.test; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LoginServlet extends HttpServlet { private String AUTO_USER_KEY = "AUTO_USER_KEY"; private String TEST_USER = "levin"; private String TEST_PWD = "123456"; @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String user = request.getParameter("user"); String passwd = request.getParameter("password"); if (TEST_USER.equals(user) && TEST_PWD.equals(passwd)) { String login = request.getParameter("login"); if ("auto".equals(login)) { Cookie cookie = new Cookie(AUTO_USER_KEY, TEST_USER + "||" + TEST_PWD); cookie.setMaxAge(7 * 24 * 60 * 60); response.addCookie(cookie); } request.setAttribute("user", user); request.getRequestDispatcher("user.view").forward(request, response); } else { response.sendRedirect("login.html"); } } }
2、是否需要自動登入
取出Cookie後,判斷是否有符合的帳號及密碼
新增程式 LoginCookieServlet.java :
程式
package com.test; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LoginCookieServlet extends HttpServlet { private String AUTO_USER_KEY = "AUTO_USER_KEY"; private String TEST_USER = "levin"; private String TEST_PWD = "123456"; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Cookie[] cookies = request.getCookies(); if (cookies != null) { for (Cookie cookie : cookies) { if (null != cookie.getName() && null != cookie.getValue()) { String name = cookie.getName(); String value = cookie.getValue(); if (AUTO_USER_KEY.equals(name) && (TEST_USER + "||"+TEST_PWD).equals(value)) { // 有取得user request.setAttribute("user", value.toString().split("||")[0]); request.getRequestDispatcher("/user.view").forward(request, response); return; } } } } response.sendRedirect("login.html"); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } }
3、登入成功顯示程式
新增程式 UserServlet.java:
程式
package com.test; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class UserServlet extends HttpServlet { private String AUTO_USER_KEY = "AUTO_USER_KEY"; private String TEST_USER = "levin"; private String TEST_PWD = "123456"; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); if (request.getAttribute("user") == null) { response.sendRedirect("login.view"); } else { PrintWriter out = response.getWriter(); out.println(""); out.println(""); out.println("Servlet User Login "); out.println(""); out.println(""); out.println("" + request.getAttribute("user") + "已登入
"); out.println("回首頁
"); out.println(""); out.println(""); out.close(); } } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } }
4、設定web.xml :
LoginServlet mapping : /login.do
LoginCookieServlet mapping : /login.view
UserServlet mapping : /user.view
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>CookieServlet</display-name>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.test.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>com.test.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/user.view</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>LoginCookieServlet</servlet-name>
<servlet-class>com.test.LoginCookieServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginCookieServlet</servlet-name>
<url-pattern>/login.view</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
5、使用到相關的lib
Java jdk 1.6
Apache Tomcat 6.0
圖1
6、測試:
http://localhost:8081/CookieServlet/index.html
圖1 首頁 -> 按下 登入頁
圖2 登入頁
圖3 進入登入 -> 輸入帳號及密碼 -> 送出(第一次先不勾選自動登入)
圖4 登入成功頁 -> 回首頁 -> 再依 圖1~ 圖3
圖5 進入登入 -> 輸入帳號及密碼 -> 送出 (選自動登入)
圖6 登入成功頁 -> 回首頁
圖7 直接 登入成功頁
設定有效Cookie時間,這行:
cookie.setMaxAge(7 * 24 * 60 * 60);
此為 設定7天有效。
結束,
感謝~
其它文章
沒有留言:
張貼留言