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) {

this.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 .

@WebServlet("/user")
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
e.printStackTrace();
} catch (IOException e) {

// TODO Auto-generated catch block
e.printStackTrace();
}
}

5.login.jsp:

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.util.*,entity.*"%>
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<style> #main {
 width: 600px; margin: 120px auto; } #mes {
 color: red; text-align: center; margin-bottom: 20px; } </style>
</head>
<body>
<div id="main">
<div id="mes">
<%
String mes = (String) request.getAttribute("mes");
if (mes == null) {
mes = "";
}
out.print(mes);
%>
</div>
<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>
</div>
<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>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary"> Sign in </button>
</div>
</div>
</form>
</div>
</body>
</html>

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();
user.setUsername(username);
user.setPassword(password);
UserDao userDao = new UserDao();
User u = userDao.search(user);
if (u != null) {

response.sendRedirect("stu");
} 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 
Class.forName("com.mysql.jdbc.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 (rs.next()) {

u = new User();
u.setId(rs.getInt("id"));
u.setUsername("username");
}
// 7. Close the connection 
conn.close();
} catch (ClassNotFoundException e) {

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

// TODO Auto-generated catch block
e.printStackTrace();
}
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="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<script> $(document).ready(function() {
 $("[name='username']").keyup(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. https://en.javamana.com/2022/02/202202130838345281.html