用户登录功能的实现,非法用户不能访问指定资源
代码结构如下:
[img]http://dl2.iteye.com/upload/attachment/0085/7968/3f99c3f9-86c9-3f24-893b-6219643ed856.jpg[/img]
UserInfo.java代码如下:
package com.bean;
public class UserInfo {
private String name ;
private String password;
public UserInfo(){
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
FilterStation.java
package com.filter;
import java.io.IOException;
import java.io.PrintWriter;
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.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
public class FilterStation extends HttpServlet implements Filter{
/**
*
*/
private static final long serialVersionUID = 1L;
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpSession session = ((HttpServletRequest)request).getSession();
response.setCharacterEncoding("utf-8");
if(session.getAttribute("user")==null){
PrintWriter out = response.getWriter();
out.print("<script language=javascript>alert('you do not login!');window.location.href='../index.jsp';</script>");
}else{
chain.doFilter(request, response);
}
}
public void init(FilterConfig config) throws ServletException {
}
}
web.xml代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>filterstation</filter-name>
<filter-class>com.filter.FilterStation</filter-class>
</filter>
<filter-mapping>
<filter-name>filterstation</filter-name>
<url-pattern>/jsp/*</url-pattern>
</filter-mapping>
</web-app>
index.jsp代码如下:
<%@ 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>login</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">
</head>
<body>
<form name="form1" method="post" action="result.jsp">
<table width="233" border="1" cellpadding="0" cellspacing="0">
<tr align="center">
<td width="48" height="30">
账号
</td>
<td width="179">
<input type="text" name="account">
</td>
</tr>
<tr align="center">
<td height="30">
密码
</td>
<td>
<input type="password" name="password">
</td>
</tr>
</table>
<input type="submit" name="Submit" value="提交" />
</form>
</body>
</html>
result.jsp代码如下:
<%@ page import="com.bean.UserInfo"%>
<%
request.setCharacterEncoding("utf-8");
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");
%>
back.jsp代码如下:
<%
session.invalidate();
out.print("<script language='javascript'> window.location.href='../index.jsp';</script>");
%>
showInformation.jsp代码如下:
<%@ 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>show information</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>
<script language="javascript">
window.alert('您登录成功');
window.location.href="jsp/back.jsp";
</script>
</body>
</html>
运行结果:
合法用户登录
[img]http://dl2.iteye.com/upload/attachment/0085/7970/3fed0015-50fd-33c3-a4bc-3062169a71f6.jpg[/img]
[img]http://dl2.iteye.com/upload/attachment/0085/7972/cf3a6d7f-4035-31e5-b2ba-d81b77a96a5a.jpg[/img]
非法访问资源
[img]http://dl2.iteye.com/upload/attachment/0085/7974/c3b4afd8-f0a7-3931-aa72-b537ace53c32.jpg[/img]
[img]http://dl2.iteye.com/upload/attachment/0085/7976/4629eeac-71d6-32b2-adfe-ae7acf8b47be.jpg[/img]