1. 程式人生 > >JavaWeb session——模擬實現使用者登入(教材學習筆記)

JavaWeb session——模擬實現使用者登入(教材學習筆記)

首先我們要了解使用者登入的整個流程,

1、從瀏覽器訪問網站首頁IndexServlet

2、判斷用後是否登入

3、若以登入則顯示使用者資訊

4、若未登入,進圖登入介面login.html;

      4.1、處理使用者登入資訊(LoginServlet)

      4.2、判斷使用者名稱和密碼是否正確

      4.3、若正確則顯示使用者資訊

      4.4、若不正確則提示使用者名稱或密碼錯誤

5、若使用者退出,則進入LogoutServlet介面

下面我們通過上面的流程來建立程式碼,

一、建立封裝使用者資訊類

建立一個名為web-chapter05的web專案,並在其中建立cn.itcast.chapter05.session.example02的包,並在包中建立一個User類

程式碼如下:

package cn.itcast.chapter05.session.example02;

public class User {
    private String username;
    private String password;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
    
}

二、建立IndexServlet類

在cn.itcast.chapter05.session.example02的包中建立IndexServlet類,該類是顯示網站的首頁

package cn.itcast.chapter05.session.example02;

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;

public class IndexServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//解決亂碼問題
		response.setContentType("text/html;charset=utf-8");
		//建立或者獲取儲存使用者資訊的session物件
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if(user==null) {
			response.getWriter().print("您還沒有登入,請<a href='/web-chapter05/login.html'>登入</a>");
		}else {
			response.getWriter().print("您已登入,歡迎你,"+ user.getUsername()+"!");
			response.getWriter().print("<a href='/web-chapter05/LogoutServlet'>退出</a>");
			//建立cookie存放session的標識號
			Cookie cookie = new Cookie("JSESSIONID", session.getId());
			cookie.setMaxAge(60*30);
			cookie.setPath("/web-chapter05");
			response.addCookie(cookie);
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

三、建立LoginServlet類

在cn.itcast.chapter05.session.example02的包中建立LoginServlet類,該類是顯示網站的登入頁

package cn.itcast.chapter05.session.example02;

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;

public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//解決亂碼問題
		response.setContentType("text/html;charset=utf-8");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		PrintWriter pw = response.getWriter();
		if(("itcast").equals(username)&&("123456").equals(password)) {
			User user = new User();
			user.setUsername(username);
			user.setPassword(password);
			request.getSession().setAttribute("user", user);
			response.sendRedirect("/web-chapter05/IndexServlet");
		}else {
			pw.write("使用者名稱或者密碼錯誤,登入失敗");
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}

四、建立LogoutServlet類

在cn.itcast.chapter05.session.example02的包中建立LogoutServlet類,該類是顯示網站的退出及登出頁面

package cn.itcast.chapter05.session.example02;

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;

public class LogoutServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//將session物件中的user物件移除,之後重定向到首頁
		request.getSession().removeAttribute("user");
		response.sendRedirect("/web-chapter05/IndexServlet");
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}

五、建立登入頁面

在web-chapter05專案的WebContent目錄下建立一個名為login.html的頁面

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                           "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form name="reg" action="/web-chapter05/LoginServlet" method="post">
		使用者名稱: <input name="username" type="text" /><br/>
		密&nbsp;&nbsp;碼:<input name="password" type="password" /><br/>
				
			    <input type="submit" value="提交" id="bt" />
	</form>
</body>
</html>

六、在web.xml中配置相關的三個servlet

 <servlet>
    <description></description>
    <display-name>IndexServlet</display-name>
    <servlet-name>IndexServlet</servlet-name>
    <servlet-class>cn.itcast.chapter05.session.example02.IndexServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>IndexServlet</servlet-name>
    <url-pattern>/IndexServlet</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>LoginServlet</display-name>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>cn.itcast.chapter05.session.example02.LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/LoginServlet</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>LogoutServlet</display-name>
    <servlet-name>LogoutServlet</servlet-name>
    <servlet-class>cn.itcast.chapter05.session.example02.LogoutServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LogoutServlet</servlet-name>
    <url-pattern>/LogoutServlet</url-pattern>
  </servlet-mapping>

七、啟動tomcat,在瀏覽器測試

在瀏覽器網址欄中輸入http://localhost:8080/web-chapter05/login.html,進入下面的介面

然後輸入之前在程式中設定好的賬號itcast,和密碼123456,進入下面的介面

點選退出連結就會進入下面的介面

如果點選登入的話就會接著進入登入的介面

至此,一個簡單的模擬實現使用者登入就完成了