Java Web notes

Source: Internet
Author: User

Java Web notes

Get: Mark in the address bar and put it in the request line to display get? Username =... Password =...

Post: the binary upload/download request line does not display information. Transmission of large amounts of data. Data is concealed. text is not pst and cannot be cached in records in the browser.

 

**************************************** ****************************

The Post password is used to send a large amount of data.

By default, the data transmission method is Get.

Use the method property to show the transfer method. Servlet class for Request Processing

Fixed format:

Protect void doxxx (HttpServletRequest request, httpservletrespneuron respneuron) throw IOException ,...{}

 

The parameter name cannot start with jsp.

 

Servlet usage:

Servlet is a java-based web component and a java-based class independent of a third-party platform. Generally, it can be compiled, loaded, and run by the Web server, and finally generate dynamic resource content.

Why Servlet?

Servlet is written in the java language. The cross-platform features of java show that servlet has good portability and complete servlet API standards. Almost all mainstream servers support servlet.

Compared with the traditional CGI (Common Gateway Interface), Servlet has powerful functions, and Processes requests using threads rather than processes, thus saving a lot of resource overhead, therefore, Servlet is more lightweight and efficient in processing requests.

Inherit from Javax.. servlet and javax. servlet. http

 

 

 

Servlet Interface

 

The core of the Servlet API is the Servlet interface, which must be implemented by all Servlet classes. It defines five methods, three of which are called by the Servlet container.

 

The servlet is instantiated when the servlet container or web server is started. The servlet constructor is called at this time;

After the servlet instance is instantiated, call the init method of the servlet instance to initialize the servlet. After processing, inject the servlet into the servlet container;

When the client requests a servlet from the web server or servlet container, the web server or servlet container first finds the corresponding servlet in the servlet container according to the requested servlet name, if the servlet does not contain the servlet corresponding to this name, the client will respond to the request and the request does not exist;

If the requested servlet exists in the servlet container, call the servlet service method to generate dynamic resources and respond to the client. (Remember, the servlet has only one instance, that is, a singleton );

When the web server exits or the servlet container is destroyed, the destroy method of servlet is called, and the last unique sevlet instance will be GC.

Two other methods:

Public ServletConfig getServletConfig ()

----- This method returns the ServletConfig object passed to the Servlet object when the container calls the init () method.

Public String getServletInfo ()

----- Return String, which includes Servlet Information, such as the author and version. This method is rarely used. return null.

HttpServlet class

Added a new service () method in the following format:

Protected void service (HttpServletRequest,

HttpServletResponse)

Throws ServletException, IOException;

This method is a method by which Servlet requests the customer to provide services.

You can write a Servlet to override this method.

HttpServletRequest Interface

This interface extends the ServletRequest interface;

Provides a view of HTTP request objects;

Defines how to obtain information such as HTTP request headers and cookies from the request object.

HttpServletResponse Interface

This interface extends the ServletResponse interface;

Provides a method for sending responses to HTTP;

Defines how to set the response, such as the HTTP response header and cookie information;

This method is a method by which Servlet requests the customer to provide services.

Httpservlet is an abstract class that can be used to inherit objects but cannot generate objects, but the inherited classes can be new objects.

++ ++

Servlet Container Processing request process

 

1) the user clicks a link and points to a servlet instead of a static page.

2) The container "sees" that the request is a Servlet, so it creates two objects: HttpServletRequest and HttpServletResponse.

3) The container finds the correct Servlet Based on the URL in the request, creates or allocates a thread for the request, and passes the request and response object to the Servlet thread.

4) The container calls the Servlet service () method. The service () method calls the doGet () or doPost () method based on different types of requests. Assume that the doGet () method is called.

5) The doGet () method generates a dynamic page and inserts the page into the response object. Note that the container also has a reference to the response object!

6) when the thread ends, the container converts the response object to an HTTP response, sends it back to the customer, and then deletes the request and response object.

 

If the request is post, The Dopost method is used.

Use the DoGet method in Get mode (that is, rewrite it)

After the Servlet is created on the server, the server calls the service method to pass the servlet object and then creates the request, response

 

**************************************** * ********************** Use the Tomcat management platform to manage the Web application lifecycle:

 

Generally, Web applications start with the Servlet container, run with the Servlet container, and terminate with the termination of the Servlet container.

Generally, modifications to the web. xml file of the application do not take effect immediately when the application is running. You must restart the application to take effect.

As a specific implementation of Servlet containers, Tomcat provides a management platform that allows you to manually manage the lifecycle of a single Web application when running Tomcat.

 

 

(DEMO) steps:

1. Modify /Conf/tomcat-user.xml, add the following User:

 

2. start Tomcat

3. http: // localhost: 8080/manager/html

 

 

Servlet initialization steps:

1: servlet container loading servlet class

2: The Servlet container creates a servletCnfig object that contains the specific servlet Initialization Configuration Information and is associated with the ServletContext object of the Web application,

3: The servlet creates a Servlet object.

4: servlet container calls Servlet to initialize the init () method of the image

**************************************** *********************

How Servlet works

The Prime Minister is a client request, and the servlet calls the service method to respond to it.

2: The service method matches the request method. Choose to call doGet () or call the doPost () method to enter the corresponding method and call the method at the logic layer to respond to the client.

3: The servlet interface and GGenericServlet have the Doget () method and the doPost () method. These methods are defined in HttpServlet but error information is returned. Therefore, each time a servlet method is defined, both must implement the doGet or doPost method.

Servlet destruction stage

1: when the file Web application is terminated, the servlet will first call the Destroy () method of the object and then Destroy it.

2: In the destroy () method, write a statement about the file transfer stream to close the database link to release all the resources occupied.

ServletContext is a public space that can be accessed by all users.

**************************************** **********

Upload files:
Upload through forms on HTML pages using files

 

**************************************** *********************

RequestDispatcher

In Servlet, you can use javax. servlet. RequestDispatcher to process request forwarding.

The requestDispatcher object is created by the servlet container to encapsulate a server resource identified by the path. The forwarding actions include:

 

1) public void forward (ServletRequest request, ServletResponse response) throws ServletException, IOException;

This method must be called before the response is submitted to the client. Otherwise, an IllegalStateException is thrown. After forward enters, the original Servlet will terminate the execution.

2) public void include (ServletRequest request, ServletResponse response) throws ServletException, IOException;

After the include object is called, the original servlet will continue after the related operations are executed.

Obtain the RequestDispatcher object

Obtain from the ServletRequest object:

Public RequestDispatcher getRequestDispatcher (String path );

// Path: Path of the target component

Obtain from the ServletContext object:

Public RequestDispatcher getRequestDispatcher (String path );

Public RequestDispatcher getNamedDispatcher (String name );

 

ServletRequest and servletContext have the same method: getRequestDispatcher. What is the difference?

Differences:

The getRequestDispatcher method of servletRequest. The path parameter can start with a slash (/), representing the path of the current context root. If you do not need to start with a slash, it is relative to the current Servlet Path.

The getRequestDispatcher method of servletContext. The path parameter must start with a slash.

 

 

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.