软件工程概论—用户登录界面

1、程序所需的代码:

    1)<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
        <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
        <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Insert title here</title>
        </head>
        <body>
        <center>
        <form action="loginResult.jsp" method="post" >
        <br><br><br><br><br><br>
        <table>
    <tr><td>用户名:</td><td><input type="text" name="userName" id="user"><span id="userInfo" ></span></td></tr>
    <tr><td>密码:</td><td><input type="password" name="userPwd" id="pwd"><span id="pwdInfo"></span></td></tr>
    <tr><td></td><td><input type="submit" value="登     录"></td>
    
        </table>
        </form>
       </center>
       </body>
       </html>

    2)  Connection conn = null;
          PreparedStatement pstmt = null;
          ResultSet rs = null;
          String driverName = "com.mysql.jdbc.Driver";
          String userName = "root";
          String userPwd = "196521";
          String dbName = "test2";
          String url1 = "jdbc:mysql://localhost:3306/" + dbName;
          String url2 = "?user=" + userName + "&password=" + userPwd;
          String url3 = "&useUnicode=true&characterEncoding=UTF-8";
          String url = url1 + url2 + url3;
          request.setCharacterEncoding("UTF-8");
          Class.forName(driverName);
          conn = DriverManager.getConnection(url);
          String sql = "select * from userInfo where username=?";
          pstmt = conn.prepareStatement(sql);
           String user = request.getParameter("userName");
          String password = request.getParameter("userPwd");
          pstmt.setString(1, user);
          rs = pstmt.executeQuery();
         if(rs.next()) {
                sql = "select * from userInfo where username=? and password=?";
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1, user);
                pstmt.setString(2, password);
                rs = pstmt.executeQuery();
                 if(rs.next()) {
                         %><center><h1>用户 <%=rs.getString("username")%>登陆成功!</h1></center><%
    
                 }
                 else {
                        %><center><h1>密码错误!</h1></center><%
                }
       } 
       else {
              %><center><h1>用户名不存在!</h1></center>
      <%}
       if(rs != null) {
          rs.close();
     }
       if(pstmt != null) {
          pstmt.close();
     }
       if(conn != null) {
          conn.close();
    }
    %>

2、相应的截图:

1)2)

3)

原文地址:https://www.cnblogs.com/th1314/p/6444932.html