案例需求:編寫一個jsp servlet程序,在login.jsp發起login.do登陸請求,當輸入的用戶名是abc密碼是123時,則判斷是登陸成功,其它暫時認爲是登陸失敗。當用戶登陸成功時,將用戶名放入session對象中並跳轉到main.jsp。當用戶登陸失敗時向request對象中存放一個msg消息,內容是登陸失敗。再發送一個orderInfo.do的查看訂單信息的請求,能夠跳轉到orderInfo.jsp。在上面程序的基礎上,寫一個過濾器,要求用戶登陸成功才能查看訂單信息。html
案例實現:java
● login.jsp:session
<%@ page language="java" contentType="text/html; charset=utf-8"jsp
pageEncoding="utf-8"%>ide
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">ui
<html>spa
<head>orm
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">htm
<title>Insert title here</title>對象
</head>
<body>
<form action="login.do">
用戶名:<input type="text" name="username" > <br>
密碼: <input type="password" name="userpassword" > <br>
<input type="submit" value="登陸"> <span>${msg}</span>
</form>
</body>
</html>
● main.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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>
<h1> 歡迎 ${username} </h1>
</body>
</html>
● orderInfo.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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>
<h1> 查看訂單信息中.... </h1>
</body>
</html>
● XdlUserLoginController.java :
package com.xdl.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;
@WebServlet("/login.do")
public class XdlUserLoginController extends HttpServlet {
@Override
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// 獲取頁面請求參數
String username = request.getParameter("username");
String userpassword = request.getParameter("userpassword");
System.out.println("登陸中...");
if("abc".equals(username) && "123".equals(userpassword)){
request.getSession().setAttribute("username", username);
response.sendRedirect("main.jsp");
}else{
request.setAttribute("msg", "登陸失敗");
request.getRequestDispatcher("login.jsp").
forward(request, response);
}
}
}
● XdlOrderInfoController.java:
package com.xdl.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/orderInfo.do")
public class XdlOrderInfoController extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("orderInfo.jsp").
forward(request, response);
}
}
● 檢查登陸的過濾器:
package com.xdl.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebFilter("*.do")
public class XdlCheckLoginFilter implements Filter {
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain chain)
throws IOException, ServletException {
// 放行 若是用戶已經登陸了 則放行 若是沒登陸則跳轉到 登陸頁面
// 注意 登陸請求時不能攔截的 是否登陸參考的是 session 有沒有 username
HttpServletRequest request = (HttpServletRequest)arg0;
HttpServletResponse response = (HttpServletResponse)arg1;
String servletPath = request.getServletPath();
if("/login.do".equals(servletPath)){
chain.doFilter(arg0, arg1);
}else{
/* System.out.println("此路是我開 此樹是我栽 要想今後過 留下李偉傑");
chain.doFilter(arg0, arg1);
System.out.println("你回來了? 怎麼又是你 你走吧"); */
String username = (String)
request.getSession().getAttribute ("username");
if(username == null){
// 跳轉到登陸頁面
response.sendRedirect("login.jsp");
}else{
chain.doFilter(arg0, arg1);
}
}
}
}