Layers of progressive Struts1 (i) introduced to struts by the servlet

Source: Internet
Author: User

Before we say Struts1, we're going to use the servlet to write a program to understand the cause and purpose of struts: a simple additions and deletions, we look at how the use of the servlet implementation, in order to figure out, we start from the most basic to write, and gradually progressive.

First edition

In this version we use the most conventional approach, delete and check each feature to use their 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 the convenience of stating that the four servlet content is basically the same here, take Adduserservlet and Delservlet for 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 {  
      
    @Override
    protected 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);  
      
    @Override
    protected 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 {  
    @Override
    protected 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);  
    @Override
    protected void DoPost (HttpServletRequest request, httpservletresponse response)  
            throws Servletexception, IOException {  
            doget (request, response);  
    }  

Usermanager

Imitate 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  
    }  
}

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.