Struts Principle Primer-Login Program

Source: Internet
Author: User

One: Create a Web project


Second: Write the login program

<body>
<!--"/loginstruts/login.do" project name plus configured resource Name--
<form action= "/loginstruts/login.do" method= "POST" >
u:<input type= "text" name= "username" ><br/>
p:<input type= "password" name= "password" ><br/>
<input type= "Submit" value= "Login" >
</form>
</body>

Three: Writing UserForm

public class UserForm extends actionform{
Private Stringusername;
Private Stringpassword;
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;
}
}

Note: The wording here is consistent.

Four: Write action

public class Loginaction extends Action {
@Override
Public Actionforward Execute (actionmapping mapping, Actionform form,
HttpServletRequest request, HttpServletResponse response)
Throws Exception {
To cast a form into a UserForm object
UserForm userform= (UserForm) Form;
System.out.println ("User name =" +userform.getusername ());
To verify
if ("123". Equals (Userform.getpassword ())) {
Request.setattribute ("username", userform.getusername ());
Return Mapping.findforward ("OK");
}else{
Return Mapping.findforward ("err");
}
}
}

Five: Write filters to deal with coding problems

public class MyFilter1 extends HttpServlet implements Filter {


public void DoFilter (ServletRequest arg0, Servletresponse arg1,
Filterchain arg2) throws IOException, Servletexception {
//test whether to enter the filter
System.out.println ("has entered the filter");
Set the Received encoding
Arg0.setcharacterencoding ("Utf-8");
Arg2.dofilter (arg0, arg1);
Set encoding for response processing
Arg1.setcontenttype ("Text/html;charse=utf-8");
}
public void init (Filterconfig arg0) throws Servletexception {
TODO auto-generated Method Stub
}
}

VI: Writing the struts-config.xml core configuration file

<?xml version= "1.0" encoding= "Utf-8"?>
<! DOCTYPE struts-config Public "-//apache software foundation//dtd struts Configuration 1.3//en" "/http Struts.apache.org/dtds/struts-config_1_3.dtd ">
<struts-config>
<!--configuration Forms--
<form-beans>
<!--name is table sole name, the recommended class name is lowercase--
<!--type is used to specify the full path of the form class--
<form-bean name= "UserForm" type= "Com.cloud.form.UserForm" ></form-bean>
</form-beans>
<!--Configure action--
<action-mappings>
<!--Configure a specific action--
<!--path represents the resource name Http//localhost:8080/path? -
<!--name is used to correlate a form to remove elements from the form--
<!--Specify the full path of the action class--
<action path= "/login" name= "UserForm" type= "Com.cloud.action.LoginAction" >
<!--Configuring a jump relationship--
<forward name= "OK" path= "/web-inf/wel.jsp"/>
<forward name= "Err" path= "/web-inf/err.jsp"/>
</action>
</action-mappings>
</struts-config>

Seven: Write Login success and failure page

wel.jsp

<body>
Ok;<%=request.getattribute ("username"). toString ()%><br/>
<a href= "/loginstruts/" > re-login </a>
</body>

err.jsp

<body>
Err
</body>

VIII: Writing the Web. XML project configuration file

<?xml version= "1.0" encoding= "UTF-8"?>
<web-app version= "2.5"
Xmlns= "Http://java.sun.com/xml/ns/javaee"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<!--configuring Struts-config paths--
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!--Configure Filters--
<filter>
<filter-name>MyFilter1</filter-name>
<filter-class>com.cloud.filters.MyFilter1</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFilter1</filter-name>
<!--for all filters--
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>


Note: Struts here need to download their own package import project, the case using STRUTS1, if there are errors and suggestions welcome to make friends together.

qq:1327880701

Copyright Notice: Bo Master original articles, reproduced please indicate the source. Http://blog.csdn.net/dzy21

Struts Principle Primer-Login Program

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.