JSP通过数据库语句实现注册和登录功能

近日一国内小伙在写JSP课设时,想将实现注册登录并设置session的源码放到CSDN上,一起来看看吧。

登录验证页面

登录验证的灵魂就是通过给定号密查询,然后通过看结果集里有无数据实现登录验证功能
<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<%
    request.setCharacterEncoding("UTF-8");
    String login_Email=request.getParameter("login_Email");
    String login_Password=request.getParameter("login_Password");
    String login_User_type=request.getParameter("login_User_type");
    boolean flag=false;    //判断登录成功与否
    Connection con;
    PreparedStatement sql;
    ResultSet res;
    
    try{
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        String uri="jdbc:sqlserver://localhost:1433;DatabaseName=JSP_CourseDesign_ZSH" ;
        String user="TSD";
        String password="1234";
        con= DriverManager.getConnection(uri,user,password);
        String condition="Use JSP_CourseDesign_ZSH select * from 用户信息表 where UEmail=? and UPassword=? and UType=?";
        sql=con.prepareStatement(condition);
        sql.setString(1,login_Email);
        sql.setString(2,login_Password);
        sql.setString(3,login_User_type);
        res=sql.executeQuery();
        
        if (res.next())
            flag=true;

        con.close();
    }
    catch (Exception e){
        out.print(e);
    }

    if (flag)
    {
        //设置session并跳转
        session.setAttribute("Email",login_Email);
        session.setAttribute("Password",login_Password);
        session.setAttribute("User_Type",login_User_type);
        response.sendRedirect("../Page/Shou_Ye.jsp");
    }
    else
    {
        response.sendRedirect("fail.jsp");
    }
%>
</body>
</html>

注册实现页面

<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<%
    request.setCharacterEncoding("UTF-8");
    String login_Email=request.getParameter("login_Email");
    String login_Password=request.getParameter("login_Password");
    String login_User_type=request.getParameter("login_User_type");
    Connection con;
    PreparedStatement sql;
    //ResultSet res;    --executeUpdate()不返回ResultSet
    //Boolean flag=false;

    try{
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        String uri="jdbc:sqlserver://localhost:1433;DatabaseName=JSP_CourseDesign_ZSH" ;
        String user="TSD";
        String password="1234";
        con= DriverManager.getConnection(uri,user,password);
        String condition="Use JSP_CourseDesign_ZSH Insert Into 用户信息表 values ('?', '?','?')";
        sql=con.prepareStatement(condition);
        sql.setString(1,login_Email);
        sql.setString(2,login_Password);
        sql.setString(3,login_User_type);
        sql.executeUpdate();

        //设置session
        session.setAttribute("Email",login_Email);
        session.setAttribute("Password",login_Password);
        session.setAttribute("User_Type",login_User_type);

        con.close();
    }
    catch (Exception e){
        out.print(e);
    }

    response.sendRedirect("../Page/Shou_Ye.jsp");  //重定向到首页
%>

</body>
</html>

你可能感兴趣的:(JSP)