一个用JAVA语言开发的含有过滤器技术的Web小例子
//在MySql中创建数据库
create database logindb;
use logindb;
create table user_info
-> (
-> id int auto_increment primary key,
-> name varchar(10) unique not null,
-> age int not null,
-> password varchar(10) not null,
-> city char(10) not null,
-> type int not null
-> );
insert into user_info values(default,'tom','23','123456','wuxi',0);
insert into user_info values(default,'niit','23','123456','shanghai',1);
insert into user_info values(default,'jerry','25','654321','shanghai',2);
insert into user_info values(default,'admin','25','admin888','shanghai',3);
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//构建一个JavaBean
package com.niit.login;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserInfoBean
{
private Connection con;
public UserInfoBean()
{
con = Connecter.getConnection();
}
/*
* 得到用户的类别代号
* 0:普通用户
* 1:VIP用户
* 2:管理员
* 3:超级管理员
* -1:帐号或者密码错误
*/
public int getUserType(String name,String pass)
{
int type = -1;
try
{
PreparedStatement ps = con
.prepareStatement("select type from user_info where name = ? and password = ?");
ps.setString(1, name);
ps.setString(2, pass);
ResultSet rs = ps.executeQuery();
if(rs.next())
{
type = rs.getInt(1);
}
}
catch (SQLException e)
{
e.printStackTrace();
}
return type;
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//创建基本方法
package com.niit.login;
public class UserInfo
{
private String name;
private int age;
private String password;
private String city;
private int type;
public UserInfo()
{
// TODO 自动生成构造函数存根
}
public UserInfo(String name, int age, String password, String city, int type)
{
this.name = name;
this.age = age;
this.password = password;
this.city = city;
this.type = type;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public String getCity()
{
return city;
}
public void setCity(String city)
{
this.city = city;
}
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;
}
public int getType()
{
return type;
}
public void setType(int type)
{
this.type = type;
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//创建数据库连接(该例使用的是MySql数据库)
package com.niit.login;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Connecter
{
public static Connection getConnection()
{
Connection con = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://127.0.0.1/logindb?useUnicode=true&characterEncoding=GBK";
con = DriverManager.getConnection(url, "root", "");
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (SQLException e)
{
e.printStackTrace();
}
return con;
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//创建过滤器
package com.niit.login;
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;
import javax.servlet.http.HttpSession;
public class ManagerLoginFilter implements Filter
{
public void init(FilterConfig config) throws ServletException
{
}
/*
* 执行功能的核心方法(过滤器)
*/
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException
{
//将请求和响应转变成 HTTP的请求和响应
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)res;
request.setCharacterEncoding("gb2312");
HttpSession session = request.getSession();
Object obj = session.getAttribute("MANAGERLOGIN");
if(obj == null)
{
//目前没有登录成功
String name = request.getParameter("userName");
String pass = request.getParameter("userPass");
if(name == null || pass == null)
{
System.out.println("*********别走后门**********");
response.sendRedirect("login.html");
}
else
{
//用户的请求从 login.html 来
UserInfoBean uib = new UserInfoBean();
int type = uib.getUserType(name, pass);
if(type == -1)
{
//用户名或者密码错误
System.out.println("*********用户名或者密码错误**********");
response.sendRedirect("login.html");
}
else if(type == 0 || type == 1)
{
//权限不足
System.out.println("*********权限不足**********");
response.sendRedirect("login.html");
}
else
{
//管理员登录(OK)
session.setAttribute("MANAGERLOGIN", "OK");
//转向成功页面
chain.doFilter(request, response);
}
}
}
else
{
//已经处在登录成功的状态
chain.doFilter(request, response);
}
}
public void destroy()
{
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//创建login.html网页
<!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" c>
<title>首页</title>
</head>
<body>
<div align="center">
<h1>管理员登录</h1><p>
<form name="form1" method="post" action="success.html">
帐号:<input type="text" name="userName"><p>
密码:<input type="password" name="userPass"><p>
<input type="submit" name="s" value="登录">
<input type="reset" name="r" value="重置">
</form>
</div>
</body>
</html>
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//创建success.html网页
<!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" c>
<title>Insert title here</title>
</head>
<body>
<div align="center">
<font size="7" color="red">成功啦~~</font>
</div>
</body>
</html>
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//改写web.xml代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee [url=http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd]http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd[/url]">
<display-name>
managerLoginProj</display-name>
<filter>
<filter-name>MF</filter-name>
<filter-class>com.niit.login.ManagerLoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MF</filter-name>
<url-pattern>/success.html</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//后记:
本程序使用的是TomCat服务器,所以在进行测试时必须安装apache-tomcat-5.5.20
1万+




被折叠的 条评论
为什么被折叠?



