<dfn id="is4kg"></dfn>
  • <ul id="is4kg"></ul>
  • <abbr id="is4kg"></abbr>
  • <ul id="is4kg"></ul>
    <bdo id="is4kg"></bdo>
    以文本方式查看主題

    -  曙海教育集團論壇  (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
                         {
            
    主站蜘蛛池模板: 欧美va天堂视频在线| 香蕉在线精品视频在线观看2| 日本三区精品三级在线电影 | 中文字幕在线视频网| 樱花草视频www| 免费中文字幕不卡视频| 美女高清特黄a大片| 国产熟女一区二区三区五月婷| tubesex69| 性欧美18-19sex性高清播放| 五月婷婷综合色| 欧美性xxxxx极品娇小| 免费观看呢日本天堂视频| 色偷偷成人网免费视频男人的天堂 | 国产高清视频网站| 中文字幕精品一区二区2021年| 日韩精品一区在线| 亚洲欧美日韩国产精品一区| 狠狠躁夜夜躁人人爽超碰97香蕉| 国产主播福利在线观看| 18禁止午夜福利体验区| 欧美交性又色又爽又黄| 制服丝袜第六页| 自慰被室友看见强行嗯啊男男| 国产精品亚洲综合一区在线观看| √天堂中文官网8在线| 成人国产一区二区三区| 久久这里只有精品18| 欧美一级视频精品观看| 任你操在线观看| 竹菊影视国产精品| 国产乱码卡一卡2卡三卡四| 高清中文字幕视频在线播 | 男女男精品视频| 国产一级一片免费播放i| 韩国午夜理伦三级2020韩| 国产精品久久久福利| 99久久国产综合精品麻豆| 天天综合天天做| 中文天堂最新版www| 成年网站在线观看|