zoukankan      html  css  js  c++  java
  • java/jsp: 登录系统

    db类

     
      1 package db;
      2 import java.sql.*;
      3 
      4 import javax.naming.InitialContext;
      5 import javax.sql.DataSource;
      6 
      7 
      8 
      9 public class DBConnect {
     10 
     11     private Connection conn;
     12     private Statement stmt;
     13     private PreparedStatement pstmt;
     14     private ResultSet rst;
     15     private String str1;
     16     
     17     private void init()
     18     {
     19         try {
     20             InitialContext ctx = new InitialContext();
     21             DataSource dsSource = (DataSource) ctx.lookup("java:comp/env/jdbc/test");
     22             conn = dsSource.getConnection();
     23             
     24         } catch (Exception e) {
     25             // TODO Auto-generated catch block
     26             e.printStackTrace();
     27         }
     28         
     29     }
     30     
     31     public DBConnect()
     32     {
     33         //构造函数        
     34         try {
     35             //获得一个数据库连接
     36             
     37                 init();
     38                     
     39             stmt = conn.createStatement();
     40             
     41         } catch (Exception e) {
     42             // TODO Auto-generated catch block
     43             e.printStackTrace();
     44         }
     45     }
     46     
     47     //执行数据库查询语句,string s为sql语句
     48     public void excuteQuery(String s)
     49     {
     50         
     51         try {
     52             if(stmt != null)
     53             {
     54                rst = stmt.executeQuery(s);
     55             }
     56         } catch (SQLException e) {
     57             // TODO Auto-generated catch block
     58             e.printStackTrace();
     59         }
     60         
     61     }
     62     
     63     //对数据库执行update操作
     64     public int excuteUpdate(String s)
     65     {
     66         int status = 0;
     67         try {
     68             if(stmt != null)            
     69                 status = stmt.executeUpdate(s);
     70         } catch (SQLException e) {
     71             // TODO Auto-generated catch block
     72             e.printStackTrace();
     73         }
     74         
     75         return status;
     76     }
     77     
     78     
     79     //以下为赋值方法
     80     //字符串赋值
     81     public void setString(int i, String s)
     82     {
     83         try {
     84             pstmt.setString(i, s);
     85         } catch (Exception e) {
     86             // TODO Auto-generated catch block
     87             e.printStackTrace();
     88         }
     89     }
     90     
     91     //boolean赋值
     92     public void setBoolean(int i, Boolean flag)
     93     {
     94         try {
     95             pstmt.setBoolean(i, flag);
     96         } catch (SQLException e) {
     97             // TODO Auto-generated catch block
     98             e.printStackTrace();
     99         }
    100     }
    101     
    102     
    103     //date日期型赋值
    104     public void setDate(int i, Date date) {
    105         try {
    106             pstmt.setDate(i, date);
    107         } catch (SQLException e) {
    108             // TODO Auto-generated catch block
    109             e.printStackTrace();
    110         }
    111     }
    112     
    113     
    114     //时间类型赋值
    115     public void setTime(int i, Time time) {
    116         try {
    117             pstmt.setTime(i, time);
    118         } catch (SQLException e) {
    119             // TODO Auto-generated catch block
    120             e.printStackTrace();
    121         }
    122     }
    123     
    124     
    125     //short类型赋值
    126     public void setShort(int i, Short short1)
    127     {
    128         try {
    129             pstmt.setShort(i, short1);
    130         } catch (SQLException e) {
    131             // TODO Auto-generated catch block
    132             e.printStackTrace();
    133         }
    134     }
    135     
    136     //整形赋值
    137     public void setInt(int i, int int1)
    138     {
    139         try {
    140             pstmt.setInt(i, int1);
    141         } catch (SQLException e) {
    142             // TODO Auto-generated catch block
    143             e.printStackTrace();
    144         }
    145     }
    146     
    147     //长整型赋值
    148     public void setLong(int i, long l)
    149     {
    150         try {
    151             pstmt.setLong(i, l);
    152         } catch (SQLException e) {
    153             // TODO Auto-generated catch block
    154             e.printStackTrace();
    155         }
    156     }
    157     
    158     //浮点型赋值
    159     public void setFloat(int i, float f)
    160     {
    161         try {
    162             pstmt.setFloat(i, f);
    163         } catch (SQLException e) {
    164             // TODO Auto-generated catch block
    165             e.printStackTrace();
    166         }
    167     }
    168     
    169     
    170     
    171     //double类型赋值
    172     public void setDouble(int i, double d)
    173     {
    174         try {
    175             pstmt.setDouble(i, d);
    176         } catch (SQLException e) {
    177             // TODO Auto-generated catch block
    178             e.printStackTrace();
    179         }
    180     }
    181     
    182     
    183     
    184     
    185     //以下为获取的方法
    186     //获取字符串
    187     public String getString(int i) throws SQLException
    188     {
    189 
    190         return rst.getString(i);
    191     }
    192     public String getString(String str1) throws SQLException
    193     {
    194         return rst.getString(str1);
    195     }
    196     
    197     //取得boolean类型
    198     public boolean getBoolean(int i) throws SQLException
    199     {
    200         return rst.getBoolean(i);
    201     }
    202     public boolean getBoolean(String str1) throws SQLException
    203     {
    204         return rst.getBoolean(str1);
    205     }
    206     
    207     
    208     //取得date日期型
    209     public Date getDate(int i) throws SQLException
    210     {
    211         return rst.getDate(i);
    212     }
    213     
    214     //取得日期字符型
    215     public Date getDate(String str1) throws SQLException{
    216         return rst.getDate(str1);
    217     }
    218     
    219     //获取time时间
    220     public Time getTime(int i) throws SQLException{
    221         return rst.getTime(i);
    222     }
    223     
    224     //获取time字符时间
    225     public Time geTime(String str1) throws SQLException
    226     {
    227         return rst.getTime(str1);
    228     }
    229     
    230     //获取双精度型
    231     public double getDouble(int i) throws SQLException
    232     {
    233         return rst.getDouble(i);
    234     }
    235     public double getDouble(String str1) throws SQLException
    236     {
    237         return rst.getDouble(str1);
    238     }
    239     
    240     
    241     //获取浮点型
    242     public float getFloat(int i) throws SQLException{
    243         return rst.getFloat(i);
    244     }
    245     public float getFloat(String str1) throws SQLException
    246     {
    247         return rst.getFloat(str1);
    248     }
    249     
    250     
    251     //获取int型
    252     public int getInt(int i) throws SQLException
    253     {
    254         return rst.getInt(i);
    255     }
    256     public int getInt(String str1) throws SQLException
    257     {
    258         return rst.getInt(str1);
    259         
    260     }
    261     
    262     //获取长整型
    263     public long getLong(int i) throws SQLException
    264     {
    265         return rst.getLong(i);
    266     }
    267     public long getLong(String str1) throws SQLException
    268     {
    269         return rst.getLong(str1);
    270     }
    271     
    272     //获取short短整型
    273     public short getShort(int i) throws SQLException
    274     {
    275         return rst.getShort(i);
    276     }
    277     public short getShort(String str1) throws SQLException
    278     {
    279         return rst.getShort(str1);
    280     }
    281     
    282     
    283     //指针下移一位
    284     public boolean next()
    285     {
    286         try {
    287             return rst.next();
    288         } catch (SQLException e) {
    289             // TODO Auto-generated catch block
    290             e.printStackTrace();
    291             return false;
    292         }
    293     }
    294     
    295     
    296     //释放内存
    297     public void close()
    298     {
    299         
    300         
    301             try {
    302                 if(conn != null)
    303                     conn.close();
    304                 if(stmt != null)
    305                     stmt.close();
    306                 if(rst!=null)
    307                     rst.close();
    308             } catch (SQLException e) {
    309                 // TODO Auto-generated catch block
    310                 e.printStackTrace();
    311             }
    312         
    313     }
    314 }
    View Code

    register包下的内容:

    package register;
    import java.text.ParsePosition;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class DateFormat {
    
    	public static long getDate(String s)
    	{
    		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    		ParsePosition parsePosition = new ParsePosition(0);
    		Date date = simpleDateFormat.parse(s, parsePosition);
    		return date.getTime();
    		
    	}
    	
    }
    

      

     1 package register;
     2 import register.product.Product;
     3 import register.product.SqlProduct;
     4 import register.user.SqlUser;
     5 import register.user.User;
     6 
     7 public abstract class Factory {
     8 
     9     private static Factory factory = null;
    10     public Factory(){}
    11     public static Factory getInstance()
    12     {
    13         if(factory == null)
    14         {
    15             try {
    16                 Class factoryClass = Class.forName("register.SqlFactory");
    17                 factory = (Factory) factoryClass.newInstance();
    18             } catch (Exception e) {
    19                 // TODO Auto-generated catch block
    20                 e.printStackTrace();
    21             }
    22             
    23             
    24         }
    25         
    26         return factory;
    27     }
    28     
    29     public abstract User initUser();
    30     public abstract SqlUser initSqlUser();
    31     public abstract Product initProduct();
    32     public abstract SqlProduct initSqlProduct();
    33     
    34 }
    View Code
     1 package register;
     2 import register.product.Product;
     3 import register.product.SqlProduct;
     4 import register.user.SqlUser;
     5 import register.user.User;;
     6 
     7 public class SqlFactory extends Factory{
     8 
     9     public User initUser(){
    10         return new SqlUser();
    11     }
    12     
    13     public Product initProduct()
    14     {
    15         return  new SqlProduct();
    16     }
    17     
    18     public SqlUser initSqlUser()
    19     {
    20         return new SqlUser();
    21     }
    22     
    23     public SqlProduct initSqlProduct()
    24     {
    25         return new SqlProduct();
    26     }
    27 }
    View Code

    register.user包
    user.java

     1 package register.user;
     2 
     3 public interface User {
     4   public abstract void setUser_id(String user_id);
     5   public abstract String getUser_id();
     6   public abstract void setPassword(String password);
     7   public abstract String getPassword();
     8   public abstract void setName(String name);
     9   public abstract String getName();
    10   public abstract void setSex(String sex);
    11   public abstract String getSex();
    12   public abstract void setBirth(long birth);
    13   public abstract long getBirth();
    14   public abstract void setDescription(String deString);
    15   public abstract String getDescription();
    16   
    17 }
    View Code

    abstractUser.java

     1 package register.user;
     2 
     3 public abstract class AbstractUser implements User {
     4 
     5     private String user_id;
     6     private String password;
     7     private String name;
     8     private String sex;
     9     private long birth;
    10     private String description;
    11     
    12     
    13     public void setUser_id(String user_id)
    14     {
    15         this.user_id = user_id;
    16     }
    17     
    18     public String getUser_id()
    19     {
    20         return this.user_id;
    21     }
    22     
    23     public void setPassword(String password)
    24     {
    25         this.password = password;
    26     }
    27     
    28     public String getPassword()
    29     {
    30         return this.password;
    31     }
    32     
    33     public void setName(String name)
    34     {
    35         this.name = name;
    36     }
    37     
    38     public String getName()
    39     {
    40         return this.name;
    41     }
    42     
    43     public void setSex(String sex)
    44     {
    45         this.sex = sex;
    46     }
    47     
    48     public String getSex()
    49     {
    50         return this.sex;
    51     }
    52     
    53     public void setBirth(long birth)
    54     {
    55         this.birth = birth;
    56     }
    57     
    58     public long getBirth()
    59     {
    60         return this.birth;
    61     }
    62     
    63     public void setDescription(String description)
    64     {
    65         this.description = description;
    66     }
    67     
    68     public String getDescription()
    69     {
    70         return this.description;
    71     }
    72     
    73 }
    View Code

    sqlUser.java

     1 package register.user;
     2 import register.Factory;
     3 import db.DBConnect;
     4 
     5 public class SqlUser extends AbstractUser 
     6 {
     7     
     8     
     9     public boolean checkRPwd(String password, String rpassword)
    10     {
    11         if( password != null && rpassword != null )
    12         {
    13             if( password.equals(rpassword) )
    14             {
    15                 return true;
    16             }else{
    17                 return false;
    18             }
    19         }else{
    20             return false;
    21         }
    22     }
    23     
    24     public boolean saveUser(User user)
    25     {
    26         String User_id = user.getUser_id();
    27         String Password = user.getPassword();
    28         String Name = user.getName();
    29         String Sex = user.getSex();
    30         long Birth = user.getBirth();
    31         String Description = user.getDescription();
    32         String str = "insert into users values('" + User_id +"', '"+ Password +"', '"+ Name +"', '"+ Sex +"', " +
    33                 "'"+ Birth +"', '"+ Description +"')";
    34         
    35         try{
    36             DBConnect dbcon = new DBConnect();
    37             dbcon.excuteUpdate(str);
    38             return true;
    39         }catch(Exception e)
    40         {
    41             e.printStackTrace();
    42             return false;
    43         }
    44     }
    45     
    46     
    47     
    48     public User getUser(String ID)
    49     {
    50         User user = Factory.getInstance().initUser();
    51         String str = "select * from users where USER_ID = '"+ID+"'";
    52         
    53         try{
    54             DBConnect dbConnect = new DBConnect();
    55             dbConnect.excuteQuery(str);
    56             if(dbConnect.next())
    57             {
    58             
    59         
    60                 user.setUser_id(dbConnect.getString(1));    
    61                 user.setPassword(dbConnect.getString(2));
    62                 user.setName(dbConnect.getString(3));
    63                 user.setSex(dbConnect.getString(4));
    64                 user.setBirth(dbConnect.getLong(5));
    65                 user.setDescription(dbConnect.getString(6));        
    66                 
    67                 
    68             }
    69         }catch(Exception e)
    70         {
    71             e.printStackTrace();
    72         }
    73         
    74         return user;
    75     }
    76 
    77     public int checkUser(String ID, String password)
    78     {
    79         int index = -1;
    80         User user = getUser(ID);
    81         if(user != null)
    82         {
    83             if( user.getPassword().equals(password) )
    84             {
    85                 index = 0;
    86             }else{
    87                 index = 2;
    88             }
    89         }else{
    90             index = 1;
    91         }
    92         return index;
    93     }
    94     
    95 }
    View Code

    jsp文件

    index

     1 <%@ page contentType="text/html; charset=utf-8" %>
     2 <%@page import="java.util.Iterator"%>
     3 <%@ page import="register.product.SqlProduct" %>
     4 <%@ page import="register.product.Product" %>
     5 <%@ page import="register.Factory" %>
     6 
     7 <html>
     8 <title>登录系统</title>
     9 <body>
    10 <h2>Hello World!</h2>
    11 
    12 <%
    13 String user_id = (String)session.getAttribute("user_id");
    14 if(user_id == "" || user_id == null)
    15 {
    16     response.sendRedirect("login.jsp");
    17 }
    18 
    19 
    20 
    21 Product product = null;
    22 SqlProduct sqlProduct = Factory.getInstance().initSqlProduct();
    23 Iterator iterator = sqlProduct.getProduct();
    24 
    25 String product_name = "";
    26 float price;
    27 String description = "";
    28 %>
    29 <table>
    30     <tr>
    31         <td>用户登录系统</td>
    32         <td>
    33         <%
    34         if("".equals(user_id))
    35             {
    36         %>
    37         <a href="login.jsp">登录</a>38         <a  href="register.jsp">注销</a>
    39         <%
    40             }else{
    41                 
    42         %>
    43         <%=user_id %>你好,<a href="logout.jsp">退出</a>
    44         <%    
    45             }
    46         %>
    47         
    48         
    49         </td>
    50     </tr>
    51     <%
    52     while( iterator.hasNext() )
    53     {
    54         product = (Product)iterator.next();
    55         product_name = product.getProduct_name();
    56         price = product.getPrice();
    57         description = product.getDescription();
    58     %>
    59     <tr>
    60         <td>产品名称:<%=product_name %></td>
    61         <td>价格:<%=price %></td>
    62     </tr>
    63     <tr>
    64         <td cospan="2">说明:<%=description %></td>
    65     </tr>
    66     <% }
    67     %>
    68 
    69 </table>
    70 </body>
    71 </html>
    View Code

    login.jsp

     1 <%@ page language="java" contentType="text/html; charset=utf-8"%>
     2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     3 <html>
     4 <head>
     5 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
     6 <title>login</title>
     7 </head>
     8 <body>
     9 
    10 <form action="do_login.jsp" method="post">
    11 <table>
    12     <tr>
    13         <td colspan="2" align="center">登录</td>
    14     </tr>
    15     <%
    16     String info = request.getParameter("info");
    17     String errMsg = "";
    18     if("1".equals(info))
    19     {
    20         errMsg = "用户名错误";
    21     }else if("2".equals(info))
    22     {
    23         errMsg = "密码错误";
    24     }
    25     if( !"".equals(errMsg))
    26     {
    27     %>    
    28         <tr>
    29         <td colspan="2" align="center" color="red"><%=errMsg %></td>
    30     </tr>
    31     <%
    32     }
    33     %>
    34     <tr>
    35         <td>用户名</td>
    36         <td><input type="text" name="user_id"></td>
    37     </tr>
    38     <tr>
    39         <td>密码</td>
    40         <td><input type="password" name="passwd"></td>
    41     </tr>
    42     <tr>
    43         <td colspan="2" align="center"><input type="submit" value="登录"></td>
    44     </tr>
    45     
    46     <tr>
    47         <td colspan="2" align="right">
    48         <a href="register.jsp">注册</a>
    49         </td>
    50     </tr>
    51 </table>
    52 </form>
    53 
    54 
    55 </body>
    56 </html>
    View Code

    do_login.jsp

     1 <%@ page import="register.user.User"%>
     2 <%@ page import="register.user.SqlUser"%>
     3 <%@ page import="register.Factory"%>
     4 <%
     5 String user_id = request.getParameter("user_id");
     6 String passwd = request.getParameter("passwd");
     7 
     8 SqlUser sqlUser = Factory.getInstance().initSqlUser();
     9 int index = sqlUser.checkUser(user_id, passwd);
    10 if( index != -1 )
    11 {
    12     if(index == 1)
    13     {
    14         response.sendRedirect("login.jsp?info=1");
    15     }else if(index == 2)
    16     {
    17         response.sendRedirect("login.jsp?info=2");
    18     }else{
    19         session.setAttribute("user_id", user_id);
    20         response.sendRedirect("index.jsp");
    21     }
    22 }else{
    23     response.sendRedirect("login.jsp");
    24 }
    25 %>
    View Code

    register.jsp

     1 <%@ page language="java" contentType="text/html; charset=utf-8"%>
     2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     3 <html>
     4 <head>
     5 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
     6 <title>register</title>
     7 </head>
     8 <body>
     9 
    10 <form action="do_register.jsp" method="post">
    11 <table>
    12 
    13     <tr>
    14         <td colspan="2" align="center">注册</td>
    15     </tr>
    16     <tr>
    17         <td>用户名</td>
    18         <td><input type="text" name="user_id"></td>
    19     </tr>
    20     <tr>
    21         <td>姓名</td>
    22         <td><input type="text" name="name"></td>
    23     </tr>
    24     <tr>
    25         <td>密码</td>
    26         <td><input type="password" name="passwd"></td>
    27     </tr>
    28     <tr>
    29         <td>确认密码</td>
    30         <td><input type="password" name="rpasswd"></td>
    31     </tr>
    32     <tr>
    33         <td>性别</td>
    34         <td>
    35             <input type="radio" value="1" name="sex">36             <input type="radio" value="2" name="sex">37         </td>
    38     </tr>
    39     <tr>
    40         <td>生日</td>
    41         <td>
    42 43             <select name="year">
    44             <%
    45             int i=0;
    46             for(i=1970; i < 2017; i++)
    47             {
    48             %>            
    49                 <option value="<%=i%>"><%=i %></option>
    50                 <%
    51             }
    52             %>
    53             </select>
    54             
    55 56             <select name="month">
    57             <%            
    58             for(i=1; i < 13; i++)
    59             {
    60             %>
    61                 <option value="<%=i %>"><%=i %></option>
    62                 <%
    63             }
    64                 %>
    65             </select>
    66 67             <select name="day">
    68             <%            
    69             for(i=1; i < 32; i++)
    70             {
    71             %>
    72                 <option value="<%=i%>"><%=i%></option>
    73                     <%
    74             }
    75                 %>
    76             </select>
    77         </td>
    78     </tr>
    79     <tr>
    80         <td>自我介绍</td>
    81         <td><input type="text" name="description"></td>
    82     </tr>
    83     <tr>
    84         <td colspan="2"><input type="submit" value="注册"></td>
    85     </tr>
    86     <tr>
    87         <td colspan="2" align="right"><a href="login.jsp">登录</a></td>
    88     </tr>
    89     
    90 </table>
    91 </form>
    92 
    93 
    94 
    95 </body>
    96 </html>
    View Code

    do_register.jsp

     1 <%@ page import="register.DateFormat"%>
     2 <%@ page import="register.user.User"%>
     3 <%@ page import="register.user.SqlUser"%>
     4 <%@ page import="register.Factory"%>
     5 <%
     6 //获取传递过来的参数
     7 String user_id = request.getParameter("user_id");
     8 String passwd = request.getParameter("passwd");
     9 String rpasswd = request.getParameter("rpasswd");
    10 String sex = request.getParameter("sex");
    11 String name = request.getParameter("name");
    12 
    13 String year = request.getParameter("year");
    14 String month = request.getParameter("month");
    15 if(month.length() ==1)month = "0"+month;
    16 String day = request.getParameter("day");
    17 if(day.length() ==1)day = "0"+day;
    18 String date = year+"-"+month+"-"+day;
    19 
    20 String description = request.getParameter("description");
    21 
    22 
    23 //初始化user实例
    24 User user = Factory.getInstance().initUser();
    25 //初始化sqlUser实例
    26 SqlUser sqlUser = Factory.getInstance().initSqlUser();
    27 
    28 
    29 //将日期转为Long型
    30 long birth = DateFormat.getDate(date);
    31 if( sqlUser.checkRPwd(passwd, rpasswd) )
    32 {
    33     
    34     user.setUser_id(user_id);
    35     user.setPassword(passwd);
    36     user.setName(name);
    37     user.setSex(sex);
    38     user.setBirth(birth);
    39     user.setDescription(description);
    40     
    41     
    42     if(sqlUser.saveUser(user))
    43     {
    44         session.setAttribute("user_id", user_id);
    45         response.sendRedirect("index.jsp");
    46         
    47     }else{
    48         response.sendRedirect("register.jsp");
    49     }
    50     
    51     
    52 }else{
    53     response.sendRedirect("register.jsp");
    54 }
    55 
    56 
    57 
    58 %>
    View Code

    logout.jsp

    <%
    session.removeAttribute("user_id");
    response.sendRedirect("index.jsp");
    %>
    

      

  • 相关阅读:
    Representation Data in OpenCascade BRep
    Render OpenCascade Geometry Surfaces in OpenSceneGraph
    Render OpenCascade Geometry Curves in OpenSceneGraph
    OpenCascade Shape Representation in OpenSceneGraph
    Geometry Surface of OpenCascade BRep
    Geometry Curve of OpenCascade BRep
    Tyvj2017清北冬令营入学测试
    Spfa算法模板
    洛谷1016 旅行家的预算
    洛谷1290 欧几里得的游戏
  • 原文地址:https://www.cnblogs.com/achengmu/p/8196163.html
Copyright © 2011-2022 走看看