Session案例 使用者登入
阿新 • • 發佈:2019-01-02
一.Session案例思維導圖
二.編寫登入頁面:login.jsp
實現登入功能和顯示登入錯誤資訊
1.登入頁面
2.顯示登入失敗資訊
3.預設賬戶:mahaun 密碼:123456
<%--
Created by IntelliJ IDEA.
User: pc
Date: 17-4-4
Time: 下午6:01
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登入頁面</title>
</head>
<body>
<%
if(null!=request.getAttribute("Ext")){
out.print("<h3 style=' color: red'>"+request.getAttribute("Ext")+"</h3>");
}
%>
<h3 >登入頁面<h3>
<form action="/LoginServlet" method="post">
使用者名稱:<input type="text" name="username"/>
密 碼:<input type="password" name="password">
<input type="submit" value="登入">-
</form>
</body>
</html>
三.編寫登入成功和再次訪問頁面:index.jsp index2.jsp
1.登入成功顯示使用者名稱
2.再次訪問顯示使用者名稱
index.jsp:
1.顯示登入成功
2.顯示登入的使用者名稱密碼
3.判斷session中是否含有username
<%--
Created by IntelliJ IDEA.
User: pc
Date: 17-4-4
Time: 下午6:01
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<%
String username=(String)session.getAttribute("username")
if(null==username){
response.sendError(404);
}else {
%>
<h1>主頁面1</h1>
<h3>使用者名稱:<%=request.getSession().getAttribute("username") %></h3>
恭喜登入成功!
</body>
<%
}
%>
</html>
index2.jsp:
1.通過Session顯示使用者名稱
2.判斷session中是否含有username
3.顯示使用者再次訪問資訊從 session中訪問到username
<%--
Created by IntelliJ IDEA.
User: pc
Date: 17-4-4
Time: 下午6:02
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>主頁2</title>
</head>
<body>
<%
String username=(String)session.getAttribute("username")
if(null==username){
response.sendError(404);
}else {
%>
<h1>主頁面2</h1>
<h3>使用者名稱:<%=request.getSession().getAttribute("username") %></h3>
歡迎<%=request.getSession().getAttribute("username") %>再次訪問!
</body>
<%
}
%>
</body>
</html>
三.寫Servlet類
裡面包含:接受使用者名稱和密碼
1.校驗是否登入成功
2.重定向到index.jsp
3.建立Session:setAttribute:儲存username
4.轉發到login頁面,request儲存失敗資訊
5.建立reques:setAttribute儲存要輸出的錯誤資訊
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Created by pc on 17-4-4.
*/
@WebServlet(name = "Login_Servlet",urlPatterns = "/LoginServlet")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
if("mahuan".equals(username)&&"123456".equals(password)){
//登入成功
request.getSession().setAttribute("username",username);
response.sendRedirect(request.getContextPath()+"/index.jsp");
}else {
//登入失敗
request.setAttribute("Ext","您的賬戶或密碼有誤,請重新登入!");
request.getRequestDispatcher("/login.jsp").forward(request,response);
}
}
}