码迷,mamicode.com
首页 > 编程语言 > 详细

一次javaweb登陆实验

时间:2017-10-22 23:40:44      阅读:498      评论:0      收藏:0      [点我收藏+]

标签:nic   har   window   put   out   red   getc   param   try   

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
  <head>
    <title>My JSP ‘login.jsp‘ starting page</title>
  </head>
  
  <body>
      <form action = "first.jsp" method = "post">
      <p>账 号:<input type = "text" name = "ID"></p>
      <p>密 码:<input type = "password" name = "ipwd"><br><br>
      <input type = "button" value = "注册" onclick ="window.location.href = ‘sign.jsp‘">
       <input type = "submit" value = "登陆">
       </p></form>
  </body>
</html>

注册:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<html>
  <head>
    <script>
    function validate(){
        var ID = document.form1.newID.value;
        var pwd = document.form1.newpwd.value;
        var pwd1 = document.form1.newpwd1.value;
        if(ID.length>10) alert("账号长度过长,不得超过10位");
        else if(pwd!=pwd1) alert("两次密码不一致!");
        else document.form1.submit();
    }
      </script>
  <title>注册</title>
  </head>
  
  <body>
      <form action = "insert.jsp" name = "form1">
      账 号:<input type = "text" name = "newID"><br>
      密 码:<input type = "password" name = "newpwd"><br>
      确 认:<input type = "password" name = "newpwd1"><br><br>
      <input type = "button" value = "返回" onclick ="window.location.href = ‘login.jsp‘">
      <input type = "button" value = "提交" onclick ="validate()"/>
      </form>
  </body>
</html>

登陆成功:

<%@ page language="java" import="java.sql.*" pageEncoding="UTF-8"%>
<html>
  <head>
  
  </head>
  
  <body><%
  out.print("登陆成功!");
    %>
    <a href = "login.jsp">退出</a>
  </body>
</html>

登陆处理:

<%@ page language="java" import="java.sql.*" pageEncoding="UTF-8"%>
<html>
  <head><title>everything</title>
  </head>
  
  <body>
    <%
    String driverName = "com.mysql.jdbc.Driver";
    String dbName = "students";
    String url1 = "jdbc:mysql://localhost:3306/"+dbName;
    String url3 = "&useUnicode = true & characterEncoding = UTF-8";
    try{
    Class.forName(driverName);
    Connection conn = DriverManager.getConnection(url1,"root","123456");
    String sql = "select pwd from user where ID = ?";
    PreparedStatement pstmt = conn.prepareStatement(sql);
    String ID = request.getParameter("ID");
    pstmt.setString(1,ID);
    ResultSet rs = pstmt.executeQuery();
    String pwd = "";
    while(rs.next()) 
    pwd = rs.getString("pwd");
    if(pwd.equals("")) {out.print("用户不存在");}
    else {
    String Dpwd = request.getParameter("ipwd");
    rs.close();
    pstmt.close();
    conn.close();
    if(pwd.equals(Dpwd)){ %>
        <jsp:forward page = "access.jsp"></jsp:forward>    
    <% }else {out.print("密码错误");%>
    <%}}%>
       <a href = "login.jsp">返回登陆</a>
     <%}catch(SQLException e) {
    e.printStackTrace();
    }%>
  </body>
</html>

注册处理:

<%@ page language="java" import="java.sql.*" pageEncoding="UTF-8"%>
<html>
  <head>
    
  </head>
  
  <body>
    <%
    String driverName = "com.mysql.jdbc.Driver";
    String dbName = "students";
    String url1 = "jdbc:mysql://localhost:3306/"+dbName;
    try{
    Class.forName(driverName);
    Connection conn = DriverManager.getConnection(url1,"root","123456");
    String newID = request.getParameter("newID");
    String newpwd = request.getParameter("newpwd");
    String sql = "insert into user(ID,pwd) values(?,?)";
    String bbl = "select ID from user where ID = ?";
    PreparedStatement bbls = conn.prepareStatement(bbl);
    bbls.setString(1,newID);
    ResultSet rs = bbls.executeQuery();
    String ID = "";
    if(!rs.next()) {
        PreparedStatement psmt = conn.prepareStatement(sql);
        psmt.setString(1, newID);
        psmt.setString(2,newpwd);
        int n = psmt.executeUpdate();
        psmt.close();
        conn.close();%>
        <%if(n!=0){
            out.print("注册成功!");%>
            <a href ="login.jsp">返回登陆</a>
            <%
        }else {
            out.print("注册失败!");
            }
        }
    else{ out.print("用户已存在!");%>
        <a href = "sign.jsp">返回注册</a>
    <%}   
        rs.close();
         bbls.close();
        }catch (SQLException e) {e.printStackTrace();}
    %>
  </body>
</html>

 

一次javaweb登陆实验

标签:nic   har   window   put   out   red   getc   param   try   

原文地址:http://www.cnblogs.com/gride-glory/p/7712195.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!