Java servlet Componentsprinciples and programming of Java servlet componentsconfiguration and life cycle of Java Servletthe life cycle of a servlet:1, when the Servlet object is created, the Init method is called to initialize2, any request from the client will call the service method services3, the Destory method is called to destroy the servlet before it is destroyed.servlet principleThe Servlet:server server let mini program runs on the server side of the small program. The request is a thread to access the target servlet! Servlet operating principle: 1, the user sends a GET request over the HTTP protocol, accesses the Web container 2,web container to encapsulate the HTTP request as a Request object, and creates a response object 3 to determine if the servlet is created, if not, initializes the target Ser Vlet 4, invoking the Servlet's service method, and sending request and response as arguments 5,service will automatically call Doget/dopost Method 6 based on the type of HTTP request, destroying request and respons E object, and return the data via HTTP to the user request commonly used methods: Request.getmethod (); Gets the request type REQUEST.GETREMOTEADDR (); Gets the client IP request.getrequestu RL (); Gets the URL of the request Request.getrequesturi (); Gets the requested resource response Common method: Response.setcharactorencoding ("Utf-8"); Set encoding response . Getwrite (); Get character output streamabout ServletA Servlet is a small program that runs on a server.This word is created in the context of Java applets, which is a small program that is sent as a separate file to a Web page, which is typically used to run on the client side, resulting in services such as user operations or mapping based on user interaction. The first to support Servlet technology is the JavaSoft Java Web Server. Since then, some other Java-based WEB servers have started to support the standard Servlet API. The main function of a Servlet is to interactively browse and modify data to generate dynamic Web content. This process is: 1) The client sends the request to the server side; 2) the server sends the request information to the SERVLET;3) Servlet generates the response content and passes it to the server. The response content is dynamically generated, usually depending on the client's request; 4) The server returns the response to the client.servlet Process Processing1, the client sends the request to server 2, the server gets the URI in the URL (/contextpath/servletpath) 3, gets the ServletContext object 4 according to the application name in the URI, The ServletContext object is used to match servletpath and Url-pattern5, the corresponding class full name is obtained according to the matching result 6, the one-time load class is encapsulated into the ServletConfig object 7, the object is called Service () method 8 According to the polymorphism, Returns the result to the userservlet life cycleThe server only creates a single instance of each servlet, and each user request raises a new thread. When you create a servlet instance, the init () method is called, and a thread is created for each user request, which invokes the service () method that created the instance earlier, and the service () method invokes the appropriate Doxxx method based on the type of HTTP request received. such as Doget (), and finally call the Destroy () method to destroy.the parameters of the Doget and DoPost methods are both HttpServletRequest and HttpServletResponse objects. Where the HttpServletRequest object represents the client's request, the servlet obtains the client's request data through this object, and the HttpServletResponse object represents the servlet's response to the client request. Sevlet uses this object to return data to the client. servlet BenefitsAn efficient traditional CGI program runs in a process, and the servlet runs in a threaded fashion. The servlet provides a number of utility routines, such as parsing and decoding HTML form data automatically, reading and setting HTTP headers, processing cookies, tracking session state, and so on. Powerful can do a lot of traditional CGI programs difficult to do things. The portability good Servlet API has the perfect standard.servlet Interactiondynamic interaction between the browser and the Web serverThe method property specifies how data is transferred, which can be either get or post various input controls and accept user input. Submit button, click this button will transfer the data inside the form to the server get method to send the data to the server-side program after the URL, such as:Http://url?name1=abc&name2=defThe Post method sends the data behind the request header of the HTTP request.Servlet InterfaceThe interface for the Javax.servlet package is:ServletConfigused to encapsulate configuration information for the current servlet in Web. XMLgetinitparameter ()---can get initialization parametersWeb. XML <init-param> <param-name/> <param-value/> </init-param>ServletContextrepresents the context in which the current servlet runs.The Web container creates a corresponding ServletContext object for each Web projectSetAttribute () getattribute () RemoveAttribute ()ServletRequestRequest sent for servlet, ServletRequest package setattribute () getattribute () removeattribute () GetParameter () Common sub-Interface HttpServletRequestServletresponseThe Servlet responds to the client, described with ServletresponseGetwriter (): Gets the response content sent by the PrintWriter output stream to the customer setContentType: Sets the response type and character set information for the response content Sendredirect: Set the re-request path Sub-Interface HttpServletResponseservlet Structurecreation of a servlet:1, write a class, inherit HttpServlet, overwrite Service/doget/dopost method2, create a servlet directlyservlet Deployment1, configuring in Web. XML <servlet> <servlet-name> name </servlet-name> <servlet-class> Package name + class name &L t;/servlet-class> </servlet> <servlet-mapping> <servlet-name> name </servlet-name> <url-pattern> virtual url:/root directory </url-pattern> </servlet-mapping> 2, configuring @WebServlet using annotations (name= "" , urlpatterns={"url", "url"}) @WebServlet ("url")servlet Session TechnologyMastering the servlet jump modeforward Jump: After a user sends a request, the server gets the request and processes it, jumps inside the server to the specified request path, and responds to a process in the browser. redirect Jump (redirect): After the user sends the request, the server obtains the request and processes, responds the result to the client browser, the browser detects the response request has a special header information location, and points to an address, the browser will automatically send a second request to the server, and responds to the client. creation of a cookie getsthe session creation processSession Creation Process: Session is created using Raikhan, when the user gets the session, the server will first to the client to obtain a cookie name value of Jsessionid cookie, if found, Gets the corresponding value and returns the corresponding session on the server side, and returns to the user if the current session is valid. Conversely, when the server does not find the Name=jsessionid cookie in the browser, the server invokes GetSession to create the session and writes the session ID to the browser cookieservlet ScopesScope is the memory space that can hold a named property mainly includes the life cycle and the shared scopeservlet Download file ideas:* 1, get file on server side path and file name * 2, need to tell the browser to download the way the file runs * 3, create input stream and output stream, buffer * 4, input stream read file, output stream response file
Java Servlet Learning Notes