Recently, I am working on the school science innovation project. In the project, I am mainly responsible for the development of Android client, while my classmate who is in charge of back-end development is still trying to build it. Therefore, I take advantage of this gap to learn how to connect the client and the background.

This article mainly introduces how to use Tomcat and Struts to build a simple server, client and server specific docking method refer to my article “Okhttp3 basic usage details”.

1 Tomcat installation and configuration

Tomcat is an open source and free JSP Server, which can realize the loading of JavaWeb programs. It is a necessary environment for configuring JSP (Java Server Page) and Java system.

  • The installation

    Download Tomcat 7.0 from the Tomcat official website and decompress the package

  • Configure the Tomcat server in Eclipse

    Open: Preferences(Preferences) -> Server ->Runtime Environments -> Add Tomcat7.0 ->Apply

    After the configuration is complete, you can view the newly configured Server in the Server

2 Use the Struts framework

Apache Struts 2 is an open source Web application architecture for developing Java EE web applications. Work with Tomcat.

  • The installation

    Download Struts from the Apache Struts website (using version 2.3 below) and unzip the struts2-blank. War file

  • Use Struts in your project

    ** (1) ** Create a Web Project/Dynamic Web Project in Eclipse

    ** (2) ** Copy the.jar file from struts2-blank in Web Content-> WEB-INF ->lib

    ** (3) ** Add filter Content to web.xml of Web Content (filter Content is also retrieved from struts2-blank)

        <filter>
            <filter-name>struts2</filter-name>
            <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>struts2</filter-name>
            <url-pattern>/ *</url-pattern>
        </filter-mapping>
    Copy the code

    Ps: If there is no web. XML file, set GDDS in Java EE Tools for this project

    (4) Create struts. XML in the project SRC folder, which contains various actions

    The action tag is written as follows:

    <action name="Name" class="The name of the class" method="Method name"></action>
    Copy the code

    (5) Create a new class, UserAction. Java, to encapsulate various methods

  • Deploy the newly created project to the server

    Right-click the Tomcat server and select Add and Remove to deploy the new Web application to the directory.

3 Starting the Server

Right-click the server -> Start, pay attention to check the console status, check whether an error is reported.

4 Common request methods

  • Get the client request

    HttpServletRequest request = ServletActionContext.getRequest();
    Copy the code
  • The server responds to the client

    HttpServletResponse response = ServletActionContext.getResponse();
    Copy the code
  • Writes to the response object

    PrintWriter writer = response.getWriter();
    writer.write("Content");
    Copy the code
  • Read file stream (String)

    ServletInputStream is=request.getInputStream();
    StringBuilder sb=new StringBuilder();
    	int len=0;
    	byte[] buf=new byte[1024];
    	while((len = is.read(buf))! = -1) {
    		sb.append(new String(buf,0,len));
    	}
    Copy the code
  • Reading a File stream

    ServletInputStream is=request.getInputStream();
    String dir=ServletActionContext.getServletContext().getRealPath("files");
    File file=new File(dir,"cat.png");
    FileOutputStream fos=new FileOutputStream(file);
    int len=0;
    	byte[] buf=new byte[1024];
    		while((len = is.read(buf))! = -1) {
    			fos.write(buf,0,len);
    		}
    	fos.flush();
    	fos.close();
    Copy the code
  • The client retrieves the file from the server

    Fileutils. copyFile(client target folder, server folder);Copy the code

5 Common Problems

  • Unable to deploy the Web application to the server directory

    May make the JDK version too high, lowering the JDK version (Project Properties -> Project Facet)

  • The Tomcat version matches the Dynamic Web Module version

    Tomcat 7.0 matches Dynamic Web Module 3.0

  • Class package introduction problem

    Manually introduce the Struts-related JAR packages in the root lib package