Layer-by-layer progressive struts1 (1) introduced by Servlet struts

Source: Internet
Author: User

Before talking about struts1, let's first use servlet to write a program to understand the reason and purpose of struts: A simple addition, deletion, modification, and query. Let's take a look at how servlet is implemented, in order to illustrate the image, we start from the most basic, and then gradually move forward.

First version

In this version, we use the most common method. Each function of adding, deleting, modifying, and querying uses its own servlet. The content is as follows:

File tree


Index. jsp

<Form action = "Servlet/adduserservlet" method = "Post"> Name: <input type = "text" name = "username"/> <br/> <input type = "Submit" value = "Submit"/> </form>

Servlet

For convenience, the content of the four servlets here is basically the same. Here we use adduserservlet and delservlet as an example:

Adduserservlet

package com.tgb.struts1.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class AddUserServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {String username=request.getParameter("username");UserManager userManager=new UserManager();userManager.add(username);request.getRequestDispatcher("/add_success.jsp").forward(request, response);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}

Deluserservlet
package com.tgb.struts1.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class DelUserServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {String username=request.getParameter("username");UserManager userManager=new UserManager();userManager.del(username);request.getRequestDispatcher("/del_success.jsp").forward(request, response);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}

Usermanager imitates the logical processing layer and Data Processing Layer
package com.tgb.struts1.servlet;import java.util.ArrayList;import java.util.List;public class UserManager {public void add(String username){System.out.println("userManager------->add,username="+username);}public void del(String username){System.out.println("userManager------->del,username="+username);}public void modify(String username){System.out.println("userManager------->modify,username="+username);}public List query(String username){System.out.println("userManager------->query,username="+username);List userList=new ArrayList();userList .add("a");userList.add("b");return userList;}}
Configuration File

  <servlet>    <servlet-name>AddUserServlet</servlet-name>    <servlet-class>com.tgb.struts1.servlet.AddUserServlet</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>AddUserServlet</servlet-name>    <url-pattern>/servlet/AddUserServlet</url-pattern>  </servlet-mapping>  <servlet>    <servlet-name>DelUserServlet</servlet-name>    <servlet-class>com.tgb.struts1.servlet.DelUserServlet</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>DelUserServlet</servlet-name>    <url-pattern>/servlet/DelUserServlet</url-pattern>  </servlet-mapping>    <servlet>    <servlet-name>ModifyUserServlet</servlet-name>    <servlet-class>com.tgb.struts1.servlet.ModifyUserServlet</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>ModifyUserServlet</servlet-name>    <url-pattern>/servlet/ModifyUserServlet</url-pattern>  </servlet-mapping>  <servlet>      <servlet-name>QueryUserServlet</servlet-name>    <servlet-class>com.tgb.struts1.servlet.QueryUserServlet</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>QueryUserServlet</servlet-name>    <url-pattern>/servlet/QueryUserServlet</url-pattern>  </servlet-mapping>
Summary

Using this servlet method, it is easy to find that there are multiple problems:

  • Configuration File Redundancy
  • Servlet File redundancy. As the number of functions increases, more and more servlet files make it difficult to sort and classify servlet files.
  • Code redundancy and addition, deletion, modification, and query functions require a considerable amount of repeated code, which is difficult to maintain
Version 2

In view of the problems found above, we continue to modify the above program: for the main servlet Problems of the first version, and then use the method of intercepting the URL address to reduce the number of servlets.

File tree


Index. jsp

<Form action = "Servlet/adduser. do "method =" Post "> Name: <input type = "text" name = "username"/> <br/> <input type = "Submit" value = "Submit"/> </form>

Servlet

package com.tgb.struts1.servlet;import java.io.IOException;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.swing.text.html.ListView;import org.apache.taglibs.standard.extra.spath.Path;public class TestServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//get uri and pathString uri=request.getRequestURI();String path=uri.substring(uri.indexOf("/",1),uri.indexOf("."));//get usernameString username=request.getParameter("username");System.out.println(path);UserManager userManager=new UserManager();//forward to path with pathif ("/servlet/addUser".equals(path)) {userManager.add(username);request.getRequestDispatcher("/add_success.jsp").forward(request, response);}else if ("/servlet/delUser".equals(path)) {userManager.del(username);request.getRequestDispatcher("/del_success.jsp").forward(request, response);}else if ("/servlet/modifyUser".equals(path)) {userManager.modify(username);request.getRequestDispatcher("/modify_success.jsp").forward(request, response);}else if ("/servlet/queryUser".equals(path)) {List userList= userManager.query(username);request.setAttribute("userList", userList);request.getRequestDispatcher("/query_success.jsp").forward(request, response);}else {throw new RuntimeException("not found this order!");}}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}

Usermanager

Same as the first version

Configuration File

  <servlet>    <servlet-name>TestServlet</servlet-name>    <servlet-class>com.tgb.struts1.servlet.TestServlet</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>TestServlet</servlet-name>    <url-pattern>*.do</url-pattern>  </servlet-mapping>

Summary

The second version overcomes the problem of too many servlet files, configuration redundancy, and code redundancy, but the problem with this version is also obvious: the code in testservlet is duplicated, the biggest problem is: the loss of flexibility, in testservlet, too many strings are used, and other functions cannot be added flexibly. Therefore, the Code has been "written to death.

Third Edition

In this version, we try to streamline the second version to increase its flexibility.

File tree


Index. jsp is the same as Version 2

Servlet

package com.tgb.struts1.servlet;import java.io.IOException;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.swing.text.html.ListView;import org.apache.taglibs.standard.extra.spath.Path;public class TestServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//get uriString uri=request.getRequestURI();String path=uri.substring(uri.indexOf("/",1),uri.indexOf("."));String username=request.getParameter("username");System.out.println(path);UserManager userManager=new UserManager();String forward=null; Action action=null;if ("/servlet/addUser".equals(path)) {action=new AddUserAction();}else if ("/servlet/delUser".equals(path)) {action=new DelUserAction();}else if ("/servlet/modifyUser".equals(path)) {action=new ModifyUserAction();}else if ("/servlet/queryUser".equals(path)) {action=new QueryUserAction();}else {throw new RuntimeException("not found this order!");}forward=action.execute(request, response);request.getRequestDispatcher(forward).forward(request, response);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}

Action

package com.tgb.struts1.servlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public interface Action {public String execute(HttpServletRequest request,HttpServletResponse response); }

Adduseraction

package com.tgb.struts1.servlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class AddUserAction implements Action {public String execute(HttpServletRequest request,HttpServletResponse response) {String username=request.getParameter("username");UserManager userManager=new UserManager();userManager.add(username);return "/add_success.jsp";}}

Usermanager

Same as the first version

Configuration File

Same as Version 2

To sum up the classes using and implementing interfaces in this version, the servlet code repetition problem is simplified to a certain extent, at the same time, the code written in the servlet is "too dead" to some extent, but there are still many problems in scalability. Summary from the above three versions, let's Abstract: What do we need to do to solve the redundancy and flexibility problems in controlling page jumps? Obviously, based on the second version, we know that we can use URL truncation to control the direction of redirection. Combined with the third version, we know that in the simplified page redirection class, the interface can simplify the code. Combined with what we learned before, we need to make the program more flexible, and of course we need to write the redirection information to the configuration file. Let's abstract the redirection process: redirect to request interception, redirect to request analysis and comparison, process request data and logic, and jump to the specified page ...... Well, these are basically the basic content of the struts1 framework. For more related blogs, go to the Summary of progressive struts1 (8).

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.