2020-03-28

注册登录

package controller;

import java.io.IOException;

import java.sql.SQLException;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import domain.User;

import model.UserModel;

/**

* Servlet implementation class LoginServlet

*/

@WebServlet("/LoginServlet")

public class LoginServlet extends HttpServlet {

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

     request.setCharacterEncoding("UTF-8");

  try {

   String username = request.getParameter("username");

   String password = request.getParameter("password");

   User user = new User();

   user.setUsername(username);

   user.setPassword(password);

   UserModel userModel = new UserModel();

   User existUser = userModel.login(user);

   if(existUser == null) {

    //登录失败

    request.setAttribute("msg", "用户名或密码错误!");

    request.getRequestDispatcher("/login.jsp").forward(request, response);

   }else {

    //

    response.sendRedirect("/web03/success.jsp");

   }

  } catch (SQLException e) {

   e.printStackTrace();

   throw new RuntimeException();

  }

}

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

    pageEncoding="ISO-8859-1"%>

Insert title here

登录页

   

   

   

   

   

   

   

   

   

   

   

   

用户名
密码

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

    pageEncoding="ISO-8859-1"%>

Insert title here

注册页

  用户名:

  密码:

  性别:

 

个人介绍:

    是否确认登录?

package domain;

public class User {

     private Integer uid;

     private String username;

     private String password;

     private String nickname;

public Integer getUid() {

return uid;

}

public void setUid(Integer uid) {

this.uid = uid;

}

public String getUsername() {

return username;

}

public  void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public String getNickname() {

return nickname;

}

public void setNickname(String nickname) {

this.nickname = nickname;

}

}

package model;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;

import org.apache.commons.dbutils.ResultSetHandler;

import org.apache.commons.dbutils.handlers.BeanHandler;

import domain.User;

import utils.JDBCUtils;

public class UserModel {

     /**

      *

      * @param user

      * @return

      * @throws SQLException

      */

public User login(User user) throws SQLException{

QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());

    User existUser = queryRunner.query("select * from user where username=? and password=?", new BeanHandler(User.class),user.getNickname(),user.getPassword());

return existUser;

}

}

你可能感兴趣的:(2020-03-28)