Implement functions:
1. User login and log out
2. Use session to record user login information
3. Display logged-in user information in JSP
Implementation principle:
After logging in, determine whether the user name and password are consistent with the storage. If it is consistent, put the user information in the session to store it; if it is inconsistent, prompt the information and return to the login page.
On the display information page, the user login information is fixed from the session. If it is found, the user information will be displayed. If it is not found, the login box will be displayed.
Logging out is very simple, it is to clear the session information.
Main documents:
1. LoginAction: Struts2's Action class is used to handle the main login and logout logic of the JAVA side.
2. login.jsp: User login page, user enters user name and password, and if login fails, the failed information will be displayed.
3. page.jsp: Display user information after logging in successfully.
4. struts.xml: struts configuration file.
LoginAction: Struts2's Action class, used to handle the main login and logout logic of the JAVA side
package luju.me.teach.struts2.login; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.commons.lang.StringUtils; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.Action; /** * @author Lu Ju http://luju.me * */ public class LoginAction { private String loginname; private String password; private String msg; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getLoginname() { return loginname; } public void setLoginname(String loginname) { this.loginname = loginname; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } /** User login*/ public String login() { if(StringUtils.isBlank(this.loginname)) { return Action.INPUT; } /* Here you write the business logic for finding user information through username, for example: log in Citizen user = prmService.queryEGovCitizenByMobile(this.loginname); .... */ if(user == null || user.getPwd() == null || !user.getPwd().getValue().equals(this.password)) { //Login failed this.msg = "The user does not exist or the password is wrong! "; return Action.INPUT; } else { //Login successfully//Set session this.getSession().setAttribute("_USER_INFO_LOGIN_NAME_", this.loginname); this.getSession().setAttribute("_USER_INFO_USER_ID_", user.getId().getValue()); this.getSession().setAttribute("_USER_INFO_USER_INFO_", user); //Set cookie this.getResponse().addCookie(new Cookie("_USER_INFO_LOGIN_NAME_", this.loginname)); this.getResponse().addCookie(new Cookie("_USER_INFO_USER_ID_", user.getId().getValue())); return Action.SUCCESS; } } /** * Logout*/ public String loginout() { //Clear session this.getSession().invalidate(); return Action.SUCCESS; } public HttpSession getSession() { return ServletActionContext.getRequest().getSession(); } public HttpServletRequest getRequest() { return ServletActionContext.getRequest(); } public HttpServletResponse getResponse() { return ServletActionContext.getResponse(); } }struts.xml: struts configuration file
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="common" namespace="/common"> <action name="login" method="login"> <result name="input">login.jsp</result> <result name="success" type="redirect">/page.jsp</result> </action> <action name="loginout" method="loginout"> <result name="success" type="redirect">login.action</result> </action> </package> </struts>
login.jsp: User login page, user input user name and password, and if login fails, the failed information is displayed.
<%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Welcome to log in</title> </head> <body> <div id="login"> <span>${msg}</span> <form name="form1" method="post" action="<c:url value="/common/login.action" />" > <span> <label>Username: </label> <input name="loginname" id="loginname" type="text" value="admin" /> </span> <span> <label>Password: </label> <input type="password" name="password" id="password" value="123"/> </span> <span> <input type="submit" value="Login" /> </span> </form> </div> </body> </html>page.jsp: Display user information after logging in successfully.
<%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <% boolean isLogin = false; String loginName = (String)request.getSession().getAttribute("_USER_INFO_LOGIN_NAME_"); if(loginName != null && !"".equals(loginName)){ isLogin = true; } request.setAttribute("isLogin",isLogin); request.setAttribute("loginName",loginName); %> <c:if test="${isLogin}"> Hello: ${loginName} <a href="<c:url value="/common/loginout.action" />">Login</a> </c:if> <c:if test="${!isLogin}"> <form name="login_form" method="post" action="<c:url value="/common/login.action" />" > <span> <label>Mobile number: </label> <input name="loginname" id="loginname" type="text" value="" /> </span> <span> <label>Password: </label> <input type="password" name="password" id="password" value=""/> </span> <span> <input type="submit" value="Login" /> </span> </form> </c:if>The above is the Java Web user login example code introduced to you by the editor. I hope it will be helpful to everyone!