What you must know about developing servlet components

1.Tomcat concept:

    tomcat is a servlet container under the ApacheSoft organization for extending the web server. This software needs to be run and started in the jre environment.

2.myeclipse configure Tomcat environment

Click on the window to select Preferences, and this time it appears:


Perform the second step configuration:


Finally, what if your Tomcat version is relatively high, as long as this is enough


3. Test: Whether Tomcat is configured successfully

Start the Tomcat server in MyEclipse, and then enter in any browser: http://localhost:port (Tomcat's port number, the default is 8080), the following figure appears, indicating that the configuration is successful


4.What is Servlet?

    It is a component specification developed by Sun for extending web servers.

    Component: It is a module that can implement some functions and needs to be deployed in a certain environment to run.

    Container: A program that can run a component.

5. How to develop a component?

    1. Write a Servlet component that conforms to the specification;

    2. Refer to the third-party jar package and write it into a class file;

    3. It needs to be packaged into the following structure, but in myeclipse we do not need to manually create the structure, it is directly generated, the file structure is as shown in the figure:

    

    Note: There is also a Classes: the stored class bytecode files are hidden by default; files with the same structure can also be found in the webapps of the Tomcat server.

   4. Deploy the packaged structure to the webapps folder in Tomcat

    5. Start Tomcat and enter in the browser: http://ip:port:appName/url to be accessed

    Note: APPName is usually the name of your project, of course not absolutely.

6. General requirements for creating components

    1. Create a class class naming rule XXXServlet needs to inherit HttpServlet and need to rewrite the service method

    2. To configure the web.xml file, you only need:

    

7. Running Tomcat, common error codes: the status code returned by the servlet container to the browser (emphasis)

    1. 404 : The accessed path does not exist

    Reason: wrong appname, wrong url, wrong Servlet-name

    2. 405 : Method not allowed

    Reason: the service name is inconsistent, the parameter type in the method does not conform to the specification

    3. 500 : It is not a Servlet component, does not inherit HttpServlet, the content in Servlet-class is wrongly written

8.Http protocol:

    is the w3c organization (World Wide Web Consortium) is a communication protocol that defines the data format between browsers and web applications

    Contains: request (request) and response (response)

    First, we open the monitoring port, click window->show view->other, search for TCP/IP, and the figure appears:

    

The monitoring window appears, right-click the blank of the upper part of the window, select Properties, and the


Start the service, the port number of the browser at this time will be the monitoring port, and the instance is to display the current system time on the browser through the Servlet component. The result is as shown in the figure:


Requests and responses are explained in detail:

    1. Request: The browser sends data to the server

        The request data contains:

            Request line: request method + request path + protocol type + version

            Message headers: They are all stored in key-value pairs, such as content-type message headers to store character encoding sets

            Entity content: Only post requests have data

    2. Response: The server sends data to the browser

        The response data contains:

            Status line: protocol type + version number + status code + status information

            Message header: also stored in the form of key-value pairs

            Response data: data to be sent to the browser after business logic operations

9.Servlet handles Http protocol:

    User access web page request and response process diagram (from this picture, it can be seen that the picture is stolen ^-^):

    

    The servlet container will encapsulate the received information and encapsulate it on the object of the type HttpServletRequest, and we can get the data we want from the request object.

    For example: version number, data submitted by the user, character set

    When the servlet container responds to the browser, it will also encapsulate some data into an object of type HttpServletResponse, send it to the browser, and it will be automatically parsed

    For example: bound message header, character set, status code

Note: For every request and response, the servlet container will create a request and response object    

10. Parameter processing: (how to get data from the browser to the background)

    Method of requesting object: getParameter (parameters to be passed)

    Get the value of the browser page, the parameter should pass the value of the name attribute, the return is a string type, there is no name value on the page, return null

11. Commonly used request methods:

    The default is the get request method. Features: The submitted data will be displayed in the address bar, and the maximum length of the data that can be submitted is 4k, which is relatively unsafe.

    Post request method: The data will not be displayed on the address bar, the data size is not limited, and it is relatively safe.

12. Deal with Chinese garbled characters

    1. When the server accepts the data from the browser, there may be a problem with the Chinese argument

        get request solution: only re-encoding, the specific operation is:

String username = request.getParameter("username");
byte[] b = username.getBytes("ISO-8859-1");//First, according to the browser encoding, turn the string into bytes, my browser encoding set is ISO-8895-1
username = new String(b,"utf-8");//The encoding corresponding to the console

        Note: This method also applies to post requests

       post request:

request.setCharacterEncoding("utf-8");

     2. When the browser accepts the data from the server, there may also be a problem of Chinese garbled characters

         Use the response object to set the message header:

response.setContentType("text/htm;charset=utf-8");
    Note: If you write the text wrong, the browser will have the effect of saving the file when the response is executed, so don't make a mistake

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325501687&siteId=291194637