曙海教育集团论坛开发语言培训专区JAVA语言开发 → 一个用JAVA语言开发的含有过滤器技术的Web小例子


  共有6710人关注过本帖树形打印

主题:一个用JAVA语言开发的含有过滤器技术的Web小例子

美女呀,离线,留言给我吧!
wangxinxin
  1楼 个性首页 | 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:青蜂侠 帖子:1393 积分:14038 威望:0 精华:0 注册:2010-11-12 11:08:23
一个用JAVA语言开发的含有过滤器技术的Web小例子  发帖心情 Post By:2010-12-11 9:53:22

一个用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
                     {
        

支持(0中立(0反对(0单帖管理 | 引用 | 回复 回到顶部

返回版面帖子列表

一个用JAVA语言开发的含有过滤器技术的Web小例子








签名