Java Web actual combat detailed tutorial (XIX) login function

Miss Zhu 2022-02-13 08:38:37 阅读数:952

java web actual combat detailed

         The login function of the website , There are usually two request responses , As shown in the figure below .
 Insert picture description here
         Blue corresponds to the first request 、 Respond to , It means requesting the server to get the login page .
         Red corresponds to the second request 、 Respond to , It means that the account number is entered on the login page 、 password , Click the login button , Put the user name 、 The password is sent to the back end Java Program ,JAVA Procedure passed JDBC Access the database to query .

         The following is the specific development :

First , Complete the function of sending a request back to the login page :

1. Develop in the database user surface , And add test data , As shown in the figure below :
 Insert picture description here

2.Java Well planned in the project User、UserController、UserDao class , and login.jsp:
 Insert picture description here

3. Entity classes are very simple , Properties and data tables user One to one correspondence .

package entity;
public class User {

private int id;
private String username;
private String password;
public int getId() {

return id;
public void setId(int id) { = id;
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;

4. The controller class , Need to design a type attribute , Through value showLogin、doLogin The decision is to display the login page and perform login processing .

public class UserController extends HttpServlet {

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

String type = request.getParameter("type");
if (type.equals("showLogin")) {

showLogin(request, response);
} else if (type.equals("doLogin")) {

doLogin(request, response);
// A little 
private void showLogin(HttpServletRequest request, HttpServletResponse response) {

try {

request.getRequestDispatcher("WEB-INF/login.jsp").forward(request, response);
} catch (ServletException e) {

// TODO Auto-generated catch block
} catch (IOException e) {

// TODO Auto-generated catch block


<%@ page language="java" contentType="text/html; charset=utf-8"
<%@ page import="java.util.*,entity.*"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<link rel="stylesheet" href="">
<style> #main {
 width: 600px; margin: 120px auto; } #mes {
 color: red; text-align: center; margin-bottom: 20px; } </style>
<div id="main">
<div id="mes">
String mes = (String) request.getAttribute("mes");
if (mes == null) {
mes = "";
<form class="form-horizontal" action="user">
<input type="hidden" name="type" value="doLogin" />
<div class="form-group">
<label class="col-sm-2 control-label"> account number </label>
<div class="col-sm-10">
<input type="text" class="form-control" placeholder=" Please enter your account number " name="username">
<div class="form-group">
<label class="col-sm-2 control-label"> password </label>
<div class="col-sm-10">
<input type="password" class="form-control" placeholder=" Please input a password " name="password">
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary"> Sign in </button>

6. test :
visit user?type=showLogin, You can return to the following interface :
 Insert picture description here

secondly , Complete login verification function :

1.UserController Add doLogin Method :

private void doLogin(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {

String username = request.getParameter("username");
String password = request.getParameter("password");
User user = new User();
UserDao userDao = new UserDao();
User u =;
if (u != null) {

} else {

request.setAttribute("mes", " Username or password incorrect ");
request.getRequestDispatcher("WEB-INF/login.jsp").forward(request, response);

2.UserDao Add search Method :

public User search(User user) {

User u = null;
// 1. Import jar Package to lib Under the folder 
try {

// 2. The load driver 
// 3. Establishing a connection 
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/school_qgc_1", "root", "123456");
// 4. establish SQL actuator 
Statement stat = conn.createStatement();
// 5. perform sql sentence , Get the result set 
ResultSet rs = stat.executeQuery("select * from user where username='" + user.getUsername()
+ "' and password='" + user.getPassword() + "'");
// 6. Processing result set 
while ( {

u = new User();
// 7. Close the connection 
} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block
} catch (SQLException e) {

// TODO Auto-generated catch block
return u;

3. Login failed. The current effect is to forward it back to the login page , And use red letters on the page to prompt :
 Insert picture description here
4. have access to jQuery Make optimization , When typing text in the account text box , Trigger prompt message disappears , Enhance user experience :

<script src=""></script>
<script> $(document).ready(function() {
 $("#mes").text(""); }) }) </script>

         At present, only the login function has been completed , Permission verification has not been performed on it , That is, the system can still access stu, Achieve access to system resources , We will in the next article , Realize the control of authority .

copyright:author[Miss Zhu],Please bring the original link to reprint, thank you.