Java's servlet

Source: Internet
Author: User

in the work of the journey, also can not forget to study, so take time to see some Java video, let's talk about Servlet .

1. What is a servlet?

is a server-side execution that responds to client requests. Java class. The main function is to interactively browse and modify data to generate dynamic Web content.

2.Servlet life cycle

servlet life cycle means Servlet object in server memory from creation to invocation, to destruction of the entire process.

( 1 ) Servlet all phases of the life cycle

instantiation and loading: Servlet Container Creation servlet the instance.

Initialize: With the servlet Container Invocation Init () method.

Request Processing: If a customer requests servlet , the container calls Service () method.

destroy: Called before destroying the instance Destroy () method.

Garbage collection: Destroying instances and marking them as garbage

( 2 ) Servlet life cycle of 3 a method

Init () Method: Servlet call this method when instantiating

Service () method: This method is called by the client request and response.

Destroy () method: Free memory, close Web The server calls this method.

3. configuration of the Servlet

Servlet is in Web. XML registered and configured in the deployment profile.

The basic structure of the configuration:

<web-app> <servlet>  <!--servlet name--<servlet-name>servletname</servlet-name>   <!--called Java class--<servlet-class>servletpackge.servlet</servlet-class> </servlet> <servlet-mapping>   <!--servlet name--<servlet-name>servletname</servlet-name>   <!--servlet class run path--<url-pattern>/servlet/servletname</url-pattern> </servlet-mapping> </web-app>

          4. processing doget dopost basic structure of the request

servlet mainly to handle requests sent by clients, Doget and the DoPost is essential, take a look at the structure using both methods.    

The following is the import of the corresponding package import java.io.IOException; Import Java.io.PrintWriter; Import javax.servlet.ServletException; Import Javax.servlet.http.HttpServlet; Import Javax.servlet.http.HttpServletRequest; Import Javax.servlet.http.HttpServletResponse; public class Servletdemo extends HttpServlet {          ///For processing a GET request sent by the client public          void Doget (HttpServletRequest request, HttpServletResponse response) throws Servletexception, IOException {//                     write the contents of the request         }       //used to process the POST request sent by the client Public          void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {                       //write the contents of the request           }}

in fact, learning Ajax has been learned, this edge of understanding is much simpler. But the mastery is not very strong, because the practice is too little, then in practice to exercise their own.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java's servlet

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.