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