java自学===Filter类的应用,验证用户


Filter类
package com;

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

public class FilterStation extends HttpServlet implements Filter {
	private FilterConfig filterConfig;

	public void init(FilterConfig filterConfig) throws ServletException {
		this.filterConfig = filterConfig;
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain filterChain) throws ServletException, IOException {
		HttpSession session = ((HttpServletRequest) request).getSession();
		response.setCharacterEncoding("gb2312");
		if (session.getAttribute("user") == null) {
			PrintWriter out = response.getWriter();
			out.print("<script language=javascript>alert('您还没有登录!!!');window.location.href='../index.jsp';</script>");
		} else {
			filterChain.doFilter(request, response);
		}
	}

	public void destroy() {
	}
}


登录界面的jsp代码

<%@ page contentType="text/html; charset=gb2312" language="java"
	import="java.sql.*" errorPage=""%>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
		<link href="css/style.css" rel="stylesheet" type="text/css">
		<script language="javascript" type="">
function checkEmpty(){
if(document.form.name.value==""){
alert("请输入账号!!!")
document.form.name.focus();
return false;
}
if(document.form.password.value==""){
alert("请输入密码!!!")
document.form.password.focus();
return false;
}
}
</script>

		<title>使用过滤器身份验证</title>
	</head>

	<body>
		<div align="center">
			<form name="form" method="post" action="result.jsp"
				onSubmit="checkEmpty()">
				<table width="419" height="134" border="0" cellpadding="0"
					cellspacing="0">
					<tr>
						<td>
							<img src="images/top.jpg" width="419" height="134">
						</td>
					</tr>
				</table>
				<table width="419" height="88" border="0" cellpadding="0"
					cellspacing="0">
					<tr>
						<td background="images/certer.jpg" align="center">

							<table width="315" border="0" align="center">
								<tr>
									<td width="61" height="25">
										用户名:
									</td>
									<td width="260">
										<input name="name" type="text" size="40">
									</td>
								</tr>
								<tr>
									<td height="25">
										密&nbsp;&nbsp;码:
									</td>
									<td>
										<input name="password" type="password" size="40">
									</td>
								</tr>
							</table>
							<input type="submit" name="Submit" value="登录">


						</td>
					</tr>
				</table>
				<table width="419" height="27" border="0" cellpadding="0"
					cellspacing="0">
					<tr>
						<td>
							<img src="images/down.jpg" width="419" height="27">
						</td>
					</tr>
				</table>
			</form>
		</div>
	</body>
</html>


result.jsp

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="com.UserInfo"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>使用过滤器身份验证</title>
</head>
<%
request.setCharacterEncoding("gb2312");
String name=request.getParameter("name");
String password=request.getParameter("password");
  UserInfo user=new UserInfo();
  user.setName(name);
  user.setPassword(password);
  session.setAttribute("user",user);

response.sendRedirect("jsp/showInformation.jsp");
%>
<body>
</body>
</html>


执行结果如下图;

java自学===Filter类的应用,验证用户
确认后会回到登录界面

如果输入http://localhost:8080/filterAuthen/index.jsp,会显示登录界面
其他代码见附件,web项目目录结构如下图

java自学===Filter类的应用,验证用户

你可能感兴趣的:(java,sql,Web,jsp,servlet)