Login based on struts2 (143 Roccaipo)

Source: Internet
Author: User

To add struts to your project:
1: Click on Project Right click to select MyEclipse
2: Select Add Struts capadilities
3: Select 2.1
1. Build a package in SRC, create a new class in the package and inherit the Actionsupport interface, and rewrite the Execute () method inside.
The code is:
Package classwork;

Import Com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings ("Serial")
public class Loginaction extends Actionsupport {
Private String UserID;
Private String passwork;

Public String GetUserID () {
return UserID;
}

public void Setuserid (String userID) {
This.userid = UserID;
}

Public String getpasswork () {
return passwork;
}
public void Setpasswork (String passwork) {
This.passwork = passwork;
}
@Override
Public String Execute () throws Exception {
TODO auto-generated Method Stub
if (Userid.equals ("AAA") &&passwork.equals ("123"))
Return "Success";
else return "error";

}
}
2. Create a new action in Struts.xml (note: The string "Success", "Error" returned in Execute () in class is the same as the value of name in the action)
<package name= "Default" extends= "Struts-default" >
<!--User Login-
<action name= "Userlogin" class= "Classwork.loginaction" >
<result name= "Success" >/main.jsp</result>
<result name= "Error" >/error.jsp</result>
</action>
</package>
3.1 Editing the login interface in the JSP
<body>
<form action= "Userlogin.action" method= "POST" >
User name: <input type= "text" name= "UserID" ><br>
Password: <input type= "text" name= "Passwork" ><br>
<input type= "Submit" value= "Login" >
</form>
</body>
3.2 Edit Login success and Failure page in JSP (note: The Save format changes to UTF-8, the login success and failure page name is the same as "main.jsp" and "error.jsp" in the action.)

Landing Success Page:

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
<%@ taglib uri= "/struts-tags" prefix= "s"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<title>my JSP ' main.jsp ' starting page</title>

<meta http-equiv= "Pragma" content= "No-cache" >
<meta http-equiv= "Cache-control" content= "No-cache" >
<meta http-equiv= "Expires" content= "0" >
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "This is my page" >
<!--
<link rel= "stylesheet" type= "Text/css" href= "Styles.css" >
-


<body>
Login Successful! Welcome <s:property value= "UserID"/> into the main interface
</body>
Login Failure Page:
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<title>my JSP ' error.jsp ' starting page</title>

<meta http-equiv= "Pragma" content= "No-cache" >
<meta http-equiv= "Cache-control" content= "No-cache" >
<meta http-equiv= "Expires" content= "0" >
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "This is my page" >
<!--
<link rel= "stylesheet" type= "Text/css" href= "Styles.css" >
-


<body>
Logon failure
</body>
4. Deploy the project and enter it in the browser: http://localhost:8080/myclass/userlogin.jsp
The result is:

Enter the correct user name and password:

The result is:

Enter the wrong password:

The result is:

Login based on Struts2 (Count 143 Roccaipo)

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.