jsp实现的简单注册登录

2 篇文章 0 订阅
1 篇文章 0 订阅

register.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'register.jsp' starting page</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>
 
  <body>
    <center>
        <form action="jsp/regist_check.jsp" method="post">
           <table border="1" width="600" height="200" background="red">
              <tr>
               
                <td colspan="2" align="center"><font class="red">用户注册:</font></td>
               
              </tr>
              <tr>
                 <td>用户ID:</td>
                 <td><input name="id" type="text"/></td>
              </tr>
               <tr>
                 <td>用户姓名:</td>
                 <td><input name="name" type="text"/></td>
              </tr>
               <tr>
                 <td>用户密码:</td>
                 <td><input name="password" type="password"/></td>
              </tr>
              <tr>
                 <td>
                     <input name="提交" value="提交" type="submit">
                 </td>
                 <td>
                    <input value="重置" type="submit">
                 </td>
              </tr>
          
           </table>
       
        </form>  
    </center>
  </body>
</html>

 

regist_check.jsp

 

<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'regist_check.jsp' starting page</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>
 
  <body>
    <center>
     <h1><font color="red">注册操作</font></h1>
         <%!
         public static final String DBDRIVER="com.mysql.jdbc.Driver";
         public static final String DBURL=
         "jdbc:mysql://localhost:3306/songwenhong";
         public static final String DBUSER="root";
         public static final String DBPASS="123456";        
       %>
       <%
         Connection conn=null;
         PreparedStatement ps=null;
         boolean flag=false;
        
      
        %>
       
        <%try{
            Class.forName(DBDRIVER);
            conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
            String sql="insert into user(userid,name,password) values(?,?,?)";
           
            ps=conn.prepareStatement(sql);
            ps.setString(1,request.getParameter("id"));
            ps.setString(2,request.getParameter("name"));
            ps.setString(3,request.getParameter("password"));
            ps.executeUpdate();
            flag=true;
           }catch(Exception e){
               e.printStackTrace();
           }finally{
               try{
                 ps.close();
                 conn.close();
               }catch(Exception e){
                  e.printStackTrace();
               }
           }
         %>
         <%if(flag){ %>
             <jsp:forward page="login.jsp"></jsp:forward>
         <%
            }else{
          %>
            <jsp:forward page="login_fail.jsp"></jsp:forward>
          <%
             }        
           %>
        
    </center>
  </body>
</html>

 

login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'login.jsp' starting page</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>
 
  <body>
    <form action="jsp/login_check.jsp" method="post">
        <table border="0">
           <tr>
              <td colspan="2">用户登录</td>
           </tr>
           <tr>
              <td>登录ID:</td>
              <td>
                 <input type="text" name="id">
              </td>
           </tr>
           <tr>
              <td>密码:</td>
              <td>
                 <input type="password" name="password">
              </td>
           </tr>
           <tr>
              <td><input type="submit" value="提交"/></td>
              <td>
                 <input type="reset" name="ID" value="重置">
              </td>
           </tr>
       
        </table>
    </form>
  </body>
</html>

 

login_check.jsp

<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'login_check.jsp' starting page</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>
 
  <body>
    <center>
      <h1>登录操作</h1>
      <%!
         public static final String DBDRIVER="com.mysql.jdbc.Driver";
         public static final String DBURL=
         "jdbc:mysql://localhost:3306/songwenhong";
         public static final String DBUSER="root";
         public static final String DBPASS="123456";        
       %>
       <%
         Connection conn=null;
         PreparedStatement ps=null;
         ResultSet rs=null;
         boolean flag=false;
         String name=null;
        %>
        <%
           try{
                Class.forName(DBDRIVER);
                conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
                String sql="select name from user where userid =? and password=?";
                ps=conn.prepareStatement(sql);
                ps.setString(1,request.getParameter("id"));
                ps.setString(2,request.getParameter("password"));
                rs=ps.executeQuery();
                if(rs.next()){
                   flag=true;
                   name=rs.getString(1);
                }
            }catch(Exception e){
               e.printStackTrace();
            }finally{
               try{
                  rs.close();
                  ps.close();
                  conn.close();
               }catch(Exception e){
              
               }
            }
         %>
         <%
            if(flag){
          %>
               <jsp:forward page="login_success.jsp">
               <jsp:param value="<%=name %>" name="uname"/>
               </jsp:forward>
          <%
            }else{
           %>
               <jsp:forward page="login_fail.jsp"></jsp:forward>
           <%
              }
            %>
    </center>
  </body>

<html>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数仓民工

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值