完成登录验证的程序设计:设计一个过滤器,过滤用户名以“T”开头的用户名登录,使其登录到错误页面,其他则正确登录到成功界面。
package com.inspur.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class loginFilter implements Filter {
/**
* 【案例】禁止从本机浏览器访问web资源的success.jsp页面
*/
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException {
HttpServletResponse resp = (HttpServletResponse) response;
HttpServletRequest req = (HttpServletRequest) request;
// 接收页面录入的用户名
String uname = (String) req.getParameter("username");
// 用户名以“T”开头的用户名登录,登录到错误页面,其他用户登录到欢迎界面
if (uname != null && uname.startsWith("T")) {
resp.sendRedirect("/CH11_LAB/error.jsp");
} else {
resp.sendRedirect("/CH11_LAB/welcome.jsp?username=" + uname);
}
}
public void init(FilterConfig filterConfig) throws ServletException {
}
}
<%@ page language="java" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>登陆界面</title>
</head>
<body>
<!-- 显示错误信息 -->
<%
Object error = request.getAttribute("loginError");
if (error != null) {
%>
<font color="red"><%=error.toString()%></font>
<%
}
%>
<form action="chkuser" method="post">
用户名:
<input type="text" name="username" >
<br>
密 码:
<input type="password" name="password" >
<br>
<input type="submit" value="登陆">
<input type="reset" value="取消">
</form>
</body>
</html>
<%@ page language="java" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>欢迎您</title>
</head>
<body>
欢迎您:<%=request.getParameter("username") %><br>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'filterDemo01.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
对不起,您输入的用户名或密码有误!
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!-- 配置过滤器 loginFilter-->
<filter>
<filter-name>loginFilter</filter-name>
<filter-class>com.inspur.filter.loginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>loginFilter</filter-name>
<url-pattern>/chkuser</url-pattern>
</filter-mapping>
</web-app>