Implementation features:
1, user login, cancellation
2, use the session record user login information
3, in the JSP display has logged in the user information
Implementation principle:
After landing by judging whether the user name and password is consistent with the storage, if consistent, the user information into the session store, if inconsistent on the prompt information, and return to the landing page.
Display information on the page fixed from the session to find the user login information, found on the display of user information, not found on the Display landing box.
Logoff is very simple, that is, empty session information.
Main documents:
1, Loginaction:struts2 action class, for processing Java end of the main landing and log out logic.
2, login.jsp: User login page, user input username and password, if the login failed to display failure information.
3, page.jsp: After the successful landing display user information.
4, struts.xml:struts configuration file.
LOGINACTION:STRUTS2 action class for handling 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.HttpSession;
Import Org.apache.commons.lang.StringUtils;
Import Org.apache.struts2.ServletActionContext;
Import com.opensymphony.xwork2.Action;
/** * @author Lu Yu 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 is a business logic for you to look up user information by user name, for example: use mobile number login Citizen user = Prmservice.queryegovcitizenbymobile (This.loginname); .... */if (user = null | | user.getpwd () = null | |!user.getpwd (). GetValue (). Equals (This.password)) {//Login failed This.msg = "User does not exist or password is wrong!"
";
return action.input;
else {//login succeeded/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 the 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 () {//empty 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" class= "luju.me.site.common.action.LoginAction"
method= "Login" >
<result name= "Input" > login.jsp</result>
<result name= "Success" type= "redirect" >/page.jsp</result>
</ action>
<action name= "loginout" class= "luju.me.site.common.action.LoginAction" method= "Loginout"
>
<result name= "Success" type= "redirect" >login.action</result>
</action>
</package>
LOGIN.JSP: User login page, user input username and password, if login failure display failure information.
<%@ page language= "java" pageencoding= "UTF-8"%> <%@ taglib uri=
"Http://java.sun.com/jsp/jstl/core" Prefix= "C"%>
PAGE.JSP: Display user information after successful landing.
<%@ 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.actio n "/>" > Logoff </a> </c:if> <c:if test= "${!islogin}" > <form name= "login_form" method= "POST" actio n= "<c:url value="/common/login.action "/>" > <span> <label> Mobile phone number:</label> <input name= " LoginName "id=" LoginName "type=" text "value=" "/> </span> <span> <label> password:</label> <inp UT type= "password" name= "password" id= "password" value= ""/> </span> <span> <input tYpe= "Submit" value= "Landing"/> </span> </form> </c:if>
The above is a small series to introduce the Java Web user login instance code, I hope to help!