Struts2 Getting Case

Getting Case: Log in judgment;

Compare two ways: servlet and struts2

struts2 packet structure:

First, using servlet implementation Log

1. Basic jsp page to determine the login form submission, with the two results pages, log on success and failure logon page, the code is as follows:

 1 《login.jsp》
 2 <body>
 3      <form action="${pageContext.request.contextPath }/login"  method="post">
 4            username:<input type="text" name="username">
 5            password:<input type="password" name="password">
 6            <input type="submit" name='login'>
 7      </form>
 8 </body>
 9 <body>
10 《failer.jsp》
11 <h1> 失败</h1>
12 </body>
13 《success.jsp》
14 <body>
15 <h1> 成功!</h1>
16 </body>
Three jsp page

 

2.servlet write, create a loginServlet.java, pay attention to set the response submitted by the suffix and suffix of the same page!

 1 public class loginServle extends HttpServlet {
 2      private static final long serialVersionUID = 1L;
 3        
 4     public loginServle() {
 5         super();
 6     }
 7      protected void doGet(HttpServletRequest request,  HttpServletResponse response) throws ServletException,  IOException {
 8            String name=request.getParameter("username");
 9            String pass=request.getParameter("password");
10            if("tom".equals(name)&&"123".equals(pass)) {
11                 response.sendRedirect(request.getContextPath()+"/success.jsp");
12            }else {
13                 response.sendRedirect(request.getContextPath()+"/failer.jsp");
14            }
15      }
16      protected void doPost(HttpServletRequest request,  HttpServletResponse response) throws ServletException,  IOException {
17            doGet(request, response);
18      }
19 }
loginServlet.java

 

3. To confirm that the configuration of web.xml is correct (show only the main code)

 1 <servlet>
 2     <description></description>
 3     <display-name>loginServle</display-name>
 4     <servlet-name>loginServle</servlet-name>
 5     <servlet-class>com.loginServle</servlet-class>
 6   </servlet>
 7   <servlet-mapping>
 8     <servlet-name>loginServle</servlet-name>
 9     <url-pattern>/login</url-pattern>
10   </servlet-mapping>
web.xml

 

Second, the use struts2 realize Login

1. The same with the above three jsp page

2. Import struts2 a jar (the main bag 13, selected files struts2 Lib inside apps in Example Lib items copied to the file folder of the project)

3. web.xml configuration information is written manually filter (interceptor) and the name of the source code path, and then write conditions interceptor mapping, the bit is provided here / *, see the following codes:

 1 <servlet>
 2     <description></description>
 3     <display-name>loginServle</display-name>
 4     <servlet-name>loginServle</servlet-name>
 5     <servlet-class>com.loginServle</servlet-class>
 6   </servlet>
 7   <servlet-mapping>
 8     <servlet-name>loginServle</servlet-name>
 9     <url-pattern>/login</url-pattern>
10   </servlet-mapping>
struts2 of web.xml

 

4. the struts.xml configuration, this is the interceptor to intercept a request message mapped to a specific Action class, servlet like and on the basic structure and action is also Result package, the particular function, described later

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3      "-//Apache Software Foundation//DTD Struts Configuration  2.3//EN"
 4      "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 <struts>
 6     <package name="default" namespace="/"  extends="struts-default">
 7     <action name="struts2" class ="com.strutsAction"  method="show"></action>
 8     <action name="login" class ="com.loginAction"  method="login">
 9       <result name="success">/success.jsp</result>
10       <result name="failer">/failer.jsp</result>
11     </action>
12     </package>
13 </struts>
struts.xml

 

5.Action creation, this is just a simple java class, not inherited, but the members of the class name and parameter names to the same request, in order to extract information.

 1 public class loginAction {
 2      private String username;
 3      private String password;
 4      
 5      public String getUsername() {
 6            return username;
 7      }
 8      public String getPassword() {
 9            return password;
10      }
11      public void setUsername(String username) {
12            this.username = username;
13      }
14      public voidthe setPassword (String password) {
 15             the this .password = password;
 16       }
 . 17       public String login () {
 18 is             // how to obtain the parameters of the request, are not here !!! the servlet 
. 19             System.out.println ( "access to the login () method " );
 20 is             System.out.println (username +" "+ password);
 21 is             IF (" Tom ".equals (username) &&" 123 " .equals (password)) {
 22 is                  System.out.println (" password correct " );
 23                  return " Success " ;
24            }else {
25                 return "failer";
26            }
27      }
28 }
loginAction

 

Results page: login: failer.jsp success.jsp

 

 

 

 

 

 

Evaluation: struts packaged servlet, perform its function, the basic web site development, the basic request is:... 1 acquires data in the servlet; 2 according to the service logic operation is completed; jump page 3; the same for these a routine thing, the same as its underlying code, so the direct use of the same code, create a framework to facilitate the development of simplified, direct use interface is finished, save time.

Guess you like

Origin www.cnblogs.com/yidiandianwy/p/11586646.html