Struts2+spring+ibatis Integrated User Management Example II: Login and Logout

Source: Internet
Author: User
Tags reset

Original address: http://blog.csdn.net/ruantao1989/article/details/8143979

Thanks to the original author

The process of logging in and out of the two operations are:

1.jsp submits the action,action to struts to find the desired instance, when spring helps inject the instance;

2. Injected Loginaction instances (Spring has injected other required instances into the servise) to find the implementation method in the

3. After the implementation of the specific implementation, but also to the session operation, logging login, logout status.


1. Login JSP

According to the session to determine whether you are logged in,

Sign in again: User name, user logout button, logon time

[HTML] View Plain copy print? <%         String s =  (String) Session.getattribute (" Username ");       if (null != s)//Determine if the session has been assigned, and then display the following information in the case of login           {    %>        Hello        <%=s %> classmates.            <input type= "button"  name= "Lo"   value= "User logoff"            onclick= "Dolink (' LogOut ')"  />            <br>            

<%  
	String s = (string) session.getattribute ("username");
    if (null! = s)//Determines whether the session has been assigned, and then displays the following information in the case of login  
    {  
%>   
   		<%=s%> Hello, classmate.
   		<input type= "button" Name= "Lo" value= "user logoff" onclick=
		"Dolink (' LogOut ')"/>
		<br>
        

Action and methods that are called according to the parameter control

[JavaScript] view plain copy print?       <!--control the action and method of the call according to the parameters---<script type= "text/javascript" > var link;           function Dolink (link) {document.getElementById ("MyForm"). Action = link;       document.getElementById ("MyForm"). Submit (); } </script>

<!--control the action and method of the call according to the parameters---
<script type= "Text/javascript" >
	var link;
	function Dolink (link) {
		document.getElementById ("MyForm"). Action = link;
		document.getElementById ("MyForm"). Submit ();
	}
</script>
Form

[HTML] View Plain copy print? <form id= "MyForm"  action= "Login"  method= "POST" >       < Input type= "button"  name= "B2"  value= "register"             onclick= "window.open (' register.jsp ', ' new ', ' height=250,width=400,top=100,left=400 ')"  />                  <input type= " Button " name=" B1 " value=" User Management "           onclick=" Dolink (' Queryall ') " />       <br>        <br>              username:<input  type= "text"  name= "username"  />       <br>       password:<input type= "password"  name= "password"  />      <br>       <input type= "Submit"  name= " Sub " value=" Login " />           <input type = "Reset"  name= "reset"  value= "reset"  />   </form>  

<form id= "MyForm" action= "Login" method= "POST" >
	<input type= "button" Name= "B2" value= "register"
		onclick= " window.open (' register.jsp ', ' new ', ' height=250,width=400,top=100,left=400 ') '/>
	
	<input type= ' button ' name = "B1" value= "User Management"
		onclick= "Dolink (' Queryall ')"/>
	<br>
	<br>
	
	username:<input Type= "text" name= "username"/>
	<br>
	password:<input type= "password" name= "password"/>
	<br>
	<input type= "Submit" Name= "sub" value= "Login"/> 
	<input type= "reset" name= "reset" value= " Reset "/>
</form>



2.loginAction

This action just login and log out of the thing

A service instance is aggregated inside, which is injected by spring,

In addition to the method to invoke the service, there is a session to manage the login status:

[Java] View Plain copy print? public class loginaction extends actionsupport    {        private String username;       private String  password;       private ILogin service;                public loginaction ()        {        }          public String  Login ()        {            boolean flag = false;           flag =  service.login (GetUserName (),  getpassword ());            if (true == flag)            {               Map<String, Object>  attibutes = actioncontext.getcontext (). getsession ();//log User login information                 attibutes.put ("username",  username);               attibutes.put ("Password",  password);//Login session             }            return flag==true? success:error;       }        public  String logout ()        {             map<string, object> attibutes = actioncontext.getcontext (). GetSession ();           attIbutes.remove ("username");           return SUCCESS;        }              / /setter&getter}  

public class Loginaction extends Actionsupport 
{
    private String username;
    private String password;
    Private Ilogin service; 
    
    Public loginaction ()
    {
    } public

	String login ()
	{
		Boolean flag = false;
		Flag = Service.login (GetUserName (), GetPassword ());
		if (true = = flag)
		{
			map<string, object> attibutes = Actioncontext.getcontext (). getsession ();// Log User login information
			attibutes.put ("username", username);
			Attibutes.put ("password", password);//login Session 
		}
		return flag==true? Success:error;
    } 
	Public String LogOut ()
	{ 
		map<string, object> attibutes = Actioncontext.getcontext (). getsession ();
		Attibutes.remove ("username");
	    return SUCCESS;
	}
    
    Setter&getter}


3.loginService

It only uses a method in DAO that is queried from the database and put back to the action

[Java] View Plain copy print? public class loginservice implements ilogin   {        private User_SSI user;       private IUserDao dao;               public boolean login (String  username, string password)        {            user.setusername (username);            user.setpassword (password);           List< User_ssi> l = dao.queryusername (User.getusername ());            for (Int i=0;i<l.size (); i++)             {               String Pwd = l.get (i). GetPassword ();                if (User.getpassword (). Equals (PWD))                      return true;                }            return false;       }              //setter&getter  

public class Loginservice implements Ilogin
{
	private user_ssi User;
    Private Iuserdao DAO;
	
	public boolean login (string username, string password)
    {
    	user.setusername (username);
    	User.setpassword (password);
    	list<user_ssi> L = dao.queryusername (User.getusername ());
    	for (int i=0;i<l.size (); i++)
    	{
    		String pwd = L.get (i). GetPassword ();
    		if (User.getpassword (). Equals (PWD)) 
    			return true;
    	return false;
    }
	
    Setter&getter




4. Login Results jsp

Jump to the following page after action processing

Success can go back to continue, or log out directly

[HTML] View Plain copy print? <%            String sa =  (String) Session.getattribute ("username");         if (null != sa  && !sa.equals ("")  )  //null is to prevent Nullpointer, if session is set successfully, it is not null value          {    %>     &NBSP;&NBSP;&NBSP;&NBSP;&LT;H2 >  login Success  , welcome   : "<%=sa%>"    login  

<%     
    String sa = (string) session.getattribute ("username");  
    if (null = = sa &&!sa.equals (""))//null is to prevent Nullpointer if the session is set successfully, it is not a null value  
    {  
%>  
    

Failure countdown two seconds after return landing page log in again

[HTML] view plain copy print? <body> lt;% Response.AddHeader ("Refresh", "2;     Url=index.jsp ");      >

  <body>
<%  
	response.addheader ("Refresh", "2; Url=index.jsp ");  
%> 
    

[HTML] view plain copy print? <body>% Response.setheader ("Refresh", "3; Url=index.jsp ");            Countdown Jump T;

    <body>  
 <%   
    response.setheader ("Refresh", "3; Url=index.jsp "); Countdown Jump 
 %>         
    

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.