jsp登录注册

2021-06-27 06:04

阅读:13098

标签:row   email   ret   private   final   charset   send   return   this   

只帖源代码,,,,不讲解。

1、Login.jsp

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

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>
      title>登录title>
  head>
  
  body>
      form action="LoginServlet" method="post">
          账号:input type="text" name="account"/>br/>
          密码:input type="password" name="password"/>br/>br/>          
          input type="submit" name="submit" value="登录" />          
          a href="Register.jsp">注册a>
      form>
  body>
html>

2、Register.jsp

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

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>
     title>注册title>
  head>
  
  body>
    form action="RegisterServlet" method="post">
        账号:input type="text" name="account">br/>
        密码:input type="password" name="password">br/>
        性别:input type="text" name="sex">br/>
        邮箱:input type="text" name="email">br/>br/>          
        input type="submit" name="submit" value="注册">
    form>
  body>
html>

3、User类,构造函数封装数据

package com.cn.jspT2;

public class User {
    private String account;
    private String password;
    private String sex;
    private String email;
    public String getAccount() {
        return account;
    }
    public void setAccount(String account) {
        this.account = account;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    
    public User(String account, String password, String sex, String email) {
        super();
        this.account = account;
        this.password = password;
        this.sex = sex;
        this.email = email;
    }
    public User() {
        super();
    }

}
    

4、DBHelper类

package com.cn.jspT2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * 获取数据库操作的连接对象
 * 关闭数据库操作的各种资源
 * @author 晏先政
 *
 */
public class DBHelper {
    private static final String className = "com.mysql.jdbc.Driver";
    private static final String url = "jdbc:mysql://localhost:3306/jsp?characterEncoding=utf8&useSSL=true";
    private static final String uname = "root";
    private static final String upass = "211599100yxz";
    
    /**
     * 获取数据库连接对象的方法
     */
    public static Connection getConn(){
        Connection conn = null;
        try{
            Class.forName(className);
            conn = DriverManager.getConnection(url,uname, upass);
        } catch(Exception e){
            e.printStackTrace();
        }
        
        return conn;
    }
    
    /**
     * 关闭数据库连接对象
     */
    public static void closeConn(Connection conn){
        try{
            if(conn!=null){
                conn.close();
            }
        } catch(Exception e){
            e.printStackTrace();
        }
    }
    
    /**
     * 关闭数据库操作对象
     */
    public static void closeStmt(Statement stmt){
        try{
            if(stmt!=null){
                stmt.close();
            }
        } catch(Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 关闭数据库操作对象
     */
    public static void closePstmt(PreparedStatement pstmt){
        try{
            if(pstmt!=null){
                pstmt.close();
            }
        } catch(Exception e){
            e.printStackTrace();
        }
    }
    
    /**
     * 关闭数据库操作对象
     */
    public static void closeRs(ResultSet rs){
        try{
            if(rs!=null){
                rs.close();
            }
        } catch(Exception e){
            e.printStackTrace();
        }
    }
}

5、UserDao类,操作数据库

package com.cn.jspT2;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDao {
    private Connection conn = null;
    private PreparedStatement pstmt = null;
    private ResultSet rs = null;
    public void addUser(User user){
        try {
            conn = DBHelper.getConn();
            String sql = "insert into user_info(account,password,sex,email) values(?,?,?,?)";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, user.getAccount());
            pstmt.setString(2, user.getPassword());
            pstmt.setString(3, user.getSex());
            pstmt.setString(4, user.getEmail());
            pstmt.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            DBHelper.closePstmt(pstmt);
            DBHelper.closeConn(conn);
        }
    }
    public Boolean queryUser(String account,String password){
        try {
            conn = DBHelper.getConn();
            String sql = "select * from user_info where account=? and password=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, account);
            pstmt.setString(2, password);
            rs = pstmt.executeQuery();
            if(rs.next()) {
                return true;
            }else{
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            DBHelper.closeRs(rs);
            DBHelper.closePstmt(pstmt);
            DBHelper.closeConn(conn);
        }
        return false;
    }
}

6、LoginServlet

package com.cn.jspT2;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        String account = request.getParameter("account");
        String password = request.getParameter("password");
        UserDao userDao = new UserDao();
        Boolean user = userDao.queryUser(account, password);
        if(user){
            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw = response.getWriter();
            pw.print("");
        }else{
            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw = response.getWriter();
            pw.print("");
        }
        
        
    }
}

7、RegisterServlet

package com.cn.jspT2;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RegisterServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request, response);    
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        String account = request.getParameter("account");
        String password = request.getParameter("password");
        String sex = request.getParameter("sex");
        String email = request.getParameter("email");
        User user = new User();
        user.setAccount(account);
        user.setPassword(password);
        user.setSex(sex);
        user.setEmail(email);
        UserDao userdao = new UserDao();
        userdao.addUser(user);
        response.setContentType("text/html;charset=utf-8");
//        response.setCharacterEncoding("utf-8");
        PrintWriter pw = response.getWriter();
        pw.print("");
        response.sendRedirect("Login.jsp");

    }
}

 

jsp登录注册

标签:row   email   ret   private   final   charset   send   return   this   

原文地址:http://www.cnblogs.com/Vito-Yan/p/7147634.html


评论


亲,登录后才可以留言!