用户登陆与验证的简单实现

来源:互联网 发布:作家出版社知乎 编辑:程序博客网 时间:2024/05/17 02:47

用户登陆页面title.jsp

<%@ page language="java" contentType="text/html;charset=gb2312"%>
<html>
  <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  </head>
  <body>
    <% boolean isLog = false;
       try {
         isLog=((String)session.getAttribute("isLog")).equals("1");
       } catch(Exception e) {
         //out.println(e);
       }
    %>
    <%if(isLog){%><a href="logout.jsp" target="_top">注销</a><%}%>
    <%
      if(isLog) {
    %> 欢迎光临!
    <%=session.getAttribute("name")%>,您是第
    <%=session.getAttribute("userLogCount")%>次登陆,你上次登陆的时间是:
    <%=session.getAttribute("userLastLogTime")%>
      <% }
        else { %>
          <form name="form1" method="post" action="login.jsp">
            用户名<input type="text" name="userId" >
            密码<input type="password" name="password">
            <input type="submit" name="Submit" value="确定">
          </form>
        <% } %>
  </body>
</html>

用户登陆处理页面login.jsp

<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.lang.*"%>
<html>
  <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  </head>
  <body>
    <%
      request.setCharacterEncoding("gb2312");
      String id= request.getParameter("userId");
      String psw= request.getParameter("password");
      Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
      Connection con = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=guestbook","sa","12345678");
      Statement statement = con.createStatement();
      String isCorrect="select * from user_info where userId='"+id+"' and password='"+psw+"'";
      ResultSet result=statement.executeQuery(isCorrect);
      session.setAttribute("isLog",new String("0"));
     
      if(!result.next()) {
        response.sendRedirect("title.jsp");
        result.close();
        statement.close();
        con.close();
      } else {
        session.setAttribute("name",result.getString("name"));
        session.setAttribute("id",result.getString("userId"));
        session.setAttribute("email",result.getString("email"));
        session.setAttribute("isLog",new String("1"));
        int count=result.getInt("userLogCount");
        session.setAttribute("userLogCount",new Integer(count));
        count++;
        session.setAttribute("userLastLogTime",result.getString("userLastLogTime"));
        java.util.Date time1 = new java.util.Date();
        String sqltime = new Timestamp(time1.getTime()).toString();
        statement.execute("update user_info set userLogCount="+count+",userLastLogTime='"+sqltime+"' where userId='"+id+"'");
        statement.close();
        con.close();
        response.sendRedirect("title.jsp");
      }
    %>
  </body>
</html>

用户注销页面logout.jsp

<%@ page language="java" contentType="text/html;charset=gb2312"%>
<html>
  <head>
    <title>CyberCreations</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  </head>
  <body>
    <center>
      <%
        session.invalidate();
        response.sendRedirect("title.jsp");
      %>
    </center>
  </body>
</html>