以文本方式查看主題 - 曙海教育集團論壇 (http://www.hufushizhe.com/bbs/index.asp) -- JAVA語言開發 (http://www.hufushizhe.com/bbs/list.asp?boardid=64) ---- 一個用JAVA語言開發的含有過濾器技術的Web小例子 (http://www.hufushizhe.com/bbs/dispbbs.asp?boardid=64&id=2475) |
-- 作者:wangxinxin -- 發布時間:2010-12-11 9:53:22 -- 一個用JAVA語言開發的含有過濾器技術的Web小例子 一個用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 { |