Servlet learning notes

xiaoxiao2021-03-06  98

2004-9-16 Thursday sunny

SERVLET's foundation concept

1. What? A: 1) Modular program, run on the server side, enhance the request / response orientation service; 2) Application example: a. Access the remote object; b. Track a lot of information; c. Multi-user cooperation

2. HTTP and Servlets Answer: 1) Servlet is an alternative to CGI in the HTTP protocol; 2) HTTPSERVLET class for developing HTTP-based servlet

3. HTTPSERVLET A: 1) Inherit the abstract class javax.servlet.GenericServlet, implement interface java.io.serializable; 2) servlet based on HTTP protocol

4. Service Method Answer: 1) When the service receives a request for the servlet, the server generates a new thread and calls service. The service method checks the HTTP request type (GET, POST, PUT, DELETE, etc.) and calls Doget, Dopost, DOPUT, DODELETE, etc. accordingly. 2) Doget / Dopost method receives HTTPSERVLETREQUEST and HTTPSERVLETRESPONSE objects. 3) 99% time, just pay attention to get and / or post requests; 4) There is no DoHead method.

5. Return response: 1) PrintWriter out = response.getwriter // Used to return text data to the client 2) servletoutputStream out = response.getOutputStream // Used to return binary data to the client

6. Support Servlet's Web Server Answer: 1) J2EE Application Server includes: Web Container and EJB Container; 2) Web Container Servlet Engine provides operational support for servlets;

2004-9-17 Friday sunny

Handling form data with servlet

1. Property of the FORM element Answer: 1) Action: The URL used to specify the servlet to process the formal data, or you can specify the email to which the Form data will be sent; 2) Method: Specify the data to the HTTP server; 3 ENCTYPE: Specifies how data is encoded before transfer, and the MultiPart / Form-Data encoding provides each field as a separate part of the MIME compatible document.

2. Resolution Request Answer: 1) For all requests: a. GetParameterNames: Get in the form in an enumeration, each item can be converted to String; b. GetParameter: Returns the parameter name in the form (case sensitive) corresponding Value (there is no such parameter, return null; no value, return empty string); c. GetParameterValues: Returns the array of parameter names (case sensitive) corresponding to the string array (there is no such parameter, return null; only one value The return value is a single element group);

SERVLET lifecycle

1. SERVLET's lifecycle Answer: 1) Loading through Web Container (J2EE components are passively loaded into the container) and instantiate the servlet object; 2) Call the init () method (only in the entire life cycle ); 3) Call the service () method (can be called multiple times throughout the lifecycle); 4) Call the destroy () method (only in the entire lifecycle is called once); 2. Init method Answer: 1) The init method is called for the first time, instead of each user request calls this method. 2) Unless it is removed by the DESTROY method; 3) INIT method ends, servlet accepts client request; 3. Init method Real Example: 1) When writing receiving servletconfig as a parameter, It should always be called Super.init; 2) Init method to accept servletconfig as parameters, use the following method: a. GetInitParameter: Returns the value corresponding to the specified parameter name, if the parameter does not exist, return null; b. GetinitParameterNames : Returns the value of the value corresponding to the specified parameter name, if the parameter does not exist, the returned emptive;

Monday, 2004-9-20 Monday

3. Service Method A: 1) When the server receives a request for the servlet, the server generates a new thread and calls Service. The service method checks the HTTP request type, call DOGET, DOPOST, DOPUT, DODELETE accordingly. 2) BevletResponse from the Container (ServletRequest);

4. Servlets Multithreading Security Answer: 1) There are fewer resources, fast processing speed, and improve efficiency. 2) Some coding recommendations: a. For variables and methods define appropriate access methods, such as simple value operations do not have multi-threaded security issues; b. Synchronize all instance variables of important data; c. Create access to class variables Access method.

5. SingLethReadModel interface A: 1) If you want to prohibit multi-thread access, you can let servlet use the SingLethReadModel interface: Public class YourServlet Extends Httpservlet Implements SingleThreadModel {...} 2) Use this interface, the system will guarantee that there will be no multiple request threads Also access a single instance of the servlet. But still need to synchronize access to class variables or shared fields stored outside the servlet. 3) If servlet is frequently accessed, the synchronization of servlet access will seriously affect performance (delay).

6. DESTROY method A: 1) The server decides to delete the servlet's Destroy method before you have loaded the Servlet instance; The access count is written to the disk and performs other similar enders.

7. Processing Service Wire Answer when servlet: 1) In the Destroy () method: If there is a service (through a synchronized instance method, the number of current threads is greater than 0), then the closed state is TRUE (through a synchronization Instance method implementation). Then the number of cyclic waits for the number of service threads is 0. 2) In the service () method: If the closed state is True, the specific logic method is not executed, and exits directly. Tuesday, 2004-9-21 Tuesday

Resource access

1. Distributed Java Technical Answer: 1) JDBC; a. Implement Data and Client Separation; b. Different kinds of databases can be applied by simple configuration. 2) RMI (RMI uses the protocol of the Internet InterNTer ORB Protocol); 3) CORBA (core technology is ORB: Corresponding Your request is converted to another physical address to request another different language object. Pure Java is not available CORBA);

2. Forward results to the visual page answer: 1) Use JavaBean (used to load a set of values, compliance with certain protocols) package results; 2) There is a context in each application in each JVM; 3) ServletContext is in the object of ServletConfig; 4) ServletContext.getRequestDispatcher (String Path): Returns a RequestDispatcher 5) Transfer requests via RequestDispatcher's Forward () or Include () method.

3. Forward requests to new resources Answer: 1) Two transfer requests for Request Dispatcher a. Forward: Several requests from a servlet to other resources (servlet, jsp, html); b. Include: including static Or dynamic content; 2) Get two ways of request dispatcher: a. ServletRequest.getRequestDispatcher () // relative path B. servletContext.getRequestDispatcher () // absolute path 3) Four resource range a. Javax.servlet.ServletContext: Within the entire application; b. Javax.servlet.http.httpsession: session period; c. Javax.servlet.servletRequest: a request period; d. Javax.servlet.jsp.pageContext: A JSP page;

Wednesday, 2004-9-22 Wednesday

Persistent status in servlets

1. Advantages and Disadvantages in the HTTP protocol: A: Advantages: You can serve many clients; shortcomings: information cannot be shared between multiple requests (Solved by cookie and session);

2. Cookies Answer: 1) Cookies is a short text message sent to the browser in the web server, and then returns to this text information without changing the browser when accessing the same Web site or domain. 2) Users can decide whether to accept cookies. 3) A cookie consists of the following: a. Name; b. Single value; c. Some operating properties: path or domain, validity period, and version number. 4) Each site, the browser usually only accepts 20 cookies, with a total of 300, and each cookie is limited to 4KB.

3. Set the cookie Answer: 1) Create a cookie: cookie c = new cookie ("cookiename", "cookievalue"); 2) Set the validity period: C.setMaxage (int LETIME); 黙 I think negative, only on the current session, It cannot be stored on disk; if 0 value means deleting cookies; effective values ​​is seconds. 3) Place cookie: Response.addcookie (c) in the response header: cookie: cookie; cookie; for (int i = 0; i " cookie.getname () " " cookie.getValue ());} 4 . Sessions Answer: 1) A client and a server-side connection information all sets, issued by brower, servlet calls from the server side; 2) Provide a way to confirm the user or store information about the user in multiple page requests. 3) Three ways to achieve session tracking: a. Cookie; b. URL rewriting; c. Hidden table

5. Set sessions: 1) Creating sessions: httpsession session = request.getSession (true); // true means that if there is no existence, create a new session 2) Bind an object with a specified name to session: Public Void SetAttribute (String name, Object value); 3) Remove all values ​​related to the specified name: Public void Removettribute (String Name); 4) Returns all attribute name public enumeration getttributenames (); 5) Return to specify name object Public Object GetaTribute String name;

6. Cookies and sessions: 1) cookies can be decided by the user to be needed, sessions cannot be; 2) cookies is a possibility of loading sessionid; 3) cookies is stored on the client, and sessions is stored on the server side; 4) cookies Construction, can be taken out by Request, returned by Response

2004-9-23 Thursday sunny

filter

1. What is a filter? A: Similar to servlet, filter is some web application components, which can be bound to a web application file. However, different from other web application components is that the filter is "chain" during processing of the container. This means that they will access an entry request before the servlet processor, and the outgoing response information is returned to the customer before accessing these response information. This access allows the filter to check and modify the request and response.

2. Filters can be used: A: 1) Create a prototype for a new feature of a web application (can be added to the web application or remove from the web application without rewriting the base application code); 2 ) Add new features to the past code. 3. What is the location of the filter placed in a container? A: The filter can be accepted before the Web resource it is applying to the web resource (which can be a servlet, a JSP page, or even a static content such as a servlet, a JSP page, even an HTML page). Intercept the output request before the customer.

4. Filter survival period A: Filter has four phases (similar to servlet): 1) instantiate; 2) Initialization (call the init () method); 3) Filter (call the Dofilter () method); 4) Destroy (call the destroy () method);

5. Filter class and interface A: All filters must implement Javax.Servlet.Filter interface: 1) Container call init () method initializing filter instance: public void init (filterconfig config) THROWS servleTexception 2) DOFILTER () the method comprises filter logic: public void doFilter (ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException 3) destroy () method of the container in the destruction of the filter instance before calling: public void destroy (); 4) FilterChain of doFilter () The code after the method constitutes a later processing filter call.

6. Configure the filter and answer: Use and elements: xsltfilter // filter name filters.smartxslfilter < / filter-class> // Specific filter class // Initialization parameter xsltfile / xi/stockquotes.xsl < / init-param> // Apply filter to each web resource in the web application Logger / * < / url-pattern>

2004-9-24 Friday sunny

Web application lifecycle events and listeners (new features after servlet v2.3)

1. What is an event listener? Answer: 1) Event notifications in ServletContext, HttpSession (Since V2.3), and ServletRequest (Since V2.4); 2) Implement one or more Servlet event listener interfaces Type; 3) Controlling the life cycle in ServletContext, HttpSession (Since V2.3), and ServletRequest; 2. Servlet Context Event Listener Answer: 1) Management Resources in JVM Layer or saved state 2) there are two types of event listeners:.. a ServletContextListener (hereinafter method of the listener) contextDestroyed (ServletContextEvent sce) contextInitialized (ServletContextEvent sce) b ServletContextAttributeListener (hereinafter method of the listener) attributeAdded ( ServletContextAttributeEvent Scab) AttributeRemoved (ServletContextttributeEvent Scab) AttributeReplaced (ServletContextAttributeEvent Scab)

3. HTTP session event listener A: 1) Manage status or resources from the same client or user to a series of requests issued by a web application; 2) There are two types of event listeners: a. HttpsessionListener the following is a method for the listener) sessionCreated (HttpSessionEvent se) sessionDestroyed (HttpSessionEvent se) b. HttpSessionAttributeListener (hereinafter method of the listener) attributeAdded (HttpSessionBindingEvent se) attributeRemoved (HttpSessionBindingEvent se) attributeReplaced (HttpSessionBindingEvent se)

4. Servlet Requost Event Loop A: 1) Managing the Status of the entire Request Lifecycle 2) There are two types of event listeners a. ServletRequestListener (below is the method of the listener) RequestDestroyed (ServletRequestevent SRE) RequestInitialized (ServletRequestEvent Sre) b. ServletRequestAttributeListener (hereinafter method of the listener) attributeAdded (ServletRequestAttributeEvent srae) attributeRemoved (ServletRequestAttributeEvent srae) attributeReplaced (ServletRequestAttributeEvent srae)

5. The regulations of the listener class answer: 1) The implementation class must be configured in the deployment descriptor;

转载请注明原文地址:https://www.9cbs.com/read-96261.html

New Post(0)