struts1的一个简单登陆功能

struts1的一个简单登陆功能

Login.jsp:

1   <form action="<%= request.getContextPath() %>/login.do" method="post">

2         username:<input type="text" name="username" /><br>

3         password:<input type="password" name="password" />

4         <input type="submit" value="login" />

5     </form>

web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>

 2 <web-app version="2.5" 

 3     xmlns="http://java.sun.com/xml/ns/javaee" 

 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 

 6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 

 7   <servlet>

 8   <!-- 这里的servlet-name和下面的servlet-name成对应关系 -->

 9       <servlet-name>action</servlet-name>

10       <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

11       <!-- 以下的读取参数,就是加载配置文件 -->

12       <init-param>

13           <param-name>config</param-name>

14           <param-value>/WEB-INF/struts-config.xml</param-value>

15       </init-param>

16       <load-on-startup>0</load-on-startup>

17   </servlet>

18   

19   <!-- 所有的.do请求 进这里来,当有do请求过来的时候,根据servlet-name查找到相应的servlet. -->

20   <servlet-mapping>

21       <servlet-name>action</servlet-name>

22       <url-pattern>*.do</url-pattern>

23   </servlet-mapping>

24   

25 </web-app>

struts-config.xml

 1 <?xml version="1.0" encoding="ISO-8859-1" ?>

 2 

 3 <!DOCTYPE struts-config PUBLIC

 4           "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"

 5           "http://struts.apache.org/dtds/struts-config_1_3.dtd">

 6           

 7 <struts-config>

 8     <form-beans>

 9         <form-bean name="loginForm" type="cn.itcast.LoginForm"></form-bean>

10     </form-beans>    

11     <action-mappings>

12         <action path="/login" type="cn.itcast.LoginAction" name="loginForm">

13             <forward name="loginSuccess" path="/LoginSuccess.jsp"></forward>

14             <forward name="loginFailure" path="/LoginFailure.jsp"></forward>

15         </action>

16     </action-mappings>    

17 </struts-config>

LoginForm.java

 1 package cn.itcast;

 2 

 3 import org.apache.struts.action.ActionForm;

 4 

 5 public class LoginForm extends ActionForm {

 6     private String username=null;

 7     private String password=null;

 8     public String getUsername() {

 9         return username;

10     }

11     public void setUsername(String username) {

12         this.username = username;

13     }

14     public String getPassword() {

15         return password;

16     }

17     public void setPassword(String password) {

18         this.password = password;

19     }

20     

21 }

LoginAction.java

 1 package cn.itcast;

 2 

 3 import javax.servlet.http.HttpServletRequest;

 4 import javax.servlet.http.HttpServletResponse;

 5 

 6 import org.apache.struts.action.Action;

 7 import org.apache.struts.action.ActionForm;

 8 import org.apache.struts.action.ActionForward;

 9 import org.apache.struts.action.ActionMapping;

10 

11 public class LoginAction extends Action {

12 

13     @Override

14     public ActionForward execute(ActionMapping mapping, ActionForm form,

15             HttpServletRequest request, HttpServletResponse response)

16             throws Exception {

17         LoginForm loginForm=(LoginForm) form;

18         if(loginForm.getUsername().equals("itcast")&&loginForm.getPassword().equals("123456")){

19             return mapping.findForward("loginSuccess");

20         }else{

21             return mapping.findForward("loginFailure");

22         }        

23     }

24 }

 

你可能感兴趣的:(struts1)