驗證名字 密碼是否正確 (Filter)
阿新 • • 發佈:2018-11-04
web.xml <context-param> <param-name>username</param-name> <param-value>tom</param-value> </context-param> <context-param> <param-name>password</param-name> <param-value>123123</param-value> </context-param>
package com.Greatest.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.annotation.WebFilter; /** * Servlet Filter implementation class UsernameFilter */ @WebFilter("/hello.jsp") public class UsernameFilter implements Filter { private FilterConfig f; /** * Default constructor. */ public UsernameFilter() { // TODO Auto-generated constructor stub } /** * @see Filter#destroy() */ public void destroy() { // TODO Auto-generated method stub } /** * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { String name = f.getServletContext().getInitParameter("username"); String name1 = request.getParameter("username"); if (!(name.equals(name1))) { request.setAttribute("message", "對不起,您輸入的名字不正確!"); request.getRequestDispatcher("/login.jsp").forward(request, response); return; } chain.doFilter(request, response); } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { this.f = fConfig; } }
package com.Greatest.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.annotation.WebFilter; /** * Servlet Filter implementation class PasswordFilter */ @WebFilter("/hello.jsp") public class PasswordFilter implements Filter { private FilterConfig f; /** * Default constructor. */ public PasswordFilter() { // TODO Auto-generated constructor stub } /** * @see Filter#destroy() */ public void destroy() { // TODO Auto-generated method stub } /** * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { String password=f.getServletContext().getInitParameter("password"); String password1=request.getParameter("password"); if(!(password.equals(password1))){ request.setAttribute("message", "對不起,您輸入的密碼不正確!"); request.getRequestDispatcher("/login.jsp").forward(request, response); return; } chain.doFilter(request, response); } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { this.f=fConfig; } }
hello.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>
Hello: ${param.username }
</body>
</html>
login.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>
<font color="red">${message }</font>
<br><br>
<form action="hello.jsp" method="post">
username: <input type="text" name="username" value="${param.username }"/>
password: <input type="password" name="password"/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>