SCWD Square Summary 2004-9 Servlet Container Model Three Basic Hierarchy: 1, Context (All Global Resources in Applications) 2, Session 3, Applications Different Areas in Distributed Environments ServletContext: It is included in servletconfig Object. All servlets have servletContext getServletContext () This method is initially defined in servletconfig, which is later implemented by GenericServlet. Use the context-param flag in the DTD to specify the name / value pair, which must be placed on the beginning of the web-app flag. Its method: String getInitParameter (String name) Enumeration getInitParameterName () URLgetResource (Stringpath) throws java.net.MalformedURLExceptionInputStream getResourseAsStream (String path) String getMimeType (String file) RequestDispatcher getRequestDispatcher (String path) Void setAttribute (String name, Object value) Object GetAttribute (String Name) The difference between context parameters and properties: 1, parameters can only be set in the container or web.xml and the attribute is set by servlet or container 2, and the parameter returns String and the attribute returns Object3, the attribute name name Package Structure ServleContextListener: Void ContextInitialized (ServletContext E) Void ContextDestroyed (ServletContext E) It is its function to initialize all data before calling the servlet request.
ServletContextEvent: ServletContext getServletContext () ServletContextAttributeListenr: Void attributeAdded (ServletContextAttributeEvent e) Void attributeRemoved (ServletContextAttributeEvent e) Void attributeReplaced (ServletContextAttributeEvent e) ServletContextAttributeEvent: String getName () Object getValue () HttpSession: sending a first session request to the client It is created, it does not automatically track user operations, use the HTTPServletRequest class to access: httpsession getsession (boolean) then use session data with the HTTPSession class: Object GetAttribute (String Name, Object Value) VoID removeAttribute (String name) HttpSessionListener: must implement the following method: void sessionCreate (HttpSessionEvent e) void sessionDestryed (HttpSessionEvent e) HttpSessionEvent: HttpSession getSession () HttpSessionAttributeListener: void attributeAdded (HttpSessionBindingEvent e) void attributeRemoved (HttpSessionBindingEvent e) void attributeReplaced (HttpsessionBindingEvent E) httpsessionBindingEvent: httpsession getsession () string getName () Object getValue () httpsessionactivationListener: The method above the Void SessionDidActive (HttpSessionEvent SE) is responsible for reading the file content in the session while transferring the session and writing it to the new server. The method of Void SessionWillPassivate (HTTPSESSIONEVENT SE) is responsible for writing the contents in the file into the session before transferring the conversation. httpsessionBindingListener: It is the difference between HttpSessionAttributeListener to observe from an object binding angle, and HttpSessionAttributeListener is observed from the perspective of the session. When the session is timeout or invalid, the object will be bind from the session. At this point, HttpSessionBindingListener will be notified and HttpSessionTtributeListener will not. Void valueBound (httpsessionBindingEvent Event) Void valueunbound (httpsessionBindingEvent evenet) Distributed environment 1. Variables should be taken in: instance variables or static variables should be avoided. 2. ServletContext should avoid storage status. 3. HTTPSession objects must also be transferable. 4. Documents must also handle the path relative to the context.
Place an empty flag
4. Define the deployment descriptor (before you must appear before servlet flag)
Pass errors must set the error attribute before the forwarding request. Record Message GenericServlet class provides two methods to enable the servlet to write errors to the log file: Public void log (String MSG) Write an error message to the servlet log file. Public void log (String Msg, Throwable T) Write an error message and the throwable object. Report Message PrintStackTrace () method Returns Void. In order to pass the error message to the client, senderror (...), but Before the message can be delivered, you must capture the record and write it to the PrintStream or PrintWriter. Throwable class provides: void printstackTrace () Void PrintStackTrace (PrintStream P) Void PrintStackTrace (PrintWriter P) servlet exception method throws an exception: 1. 2. capture the servlet specification thrown clear that the server process server process exceptions must be IOException ServletException RuntimeException subclasses ServletException:. (exception subclass) ServletException () ServletException (String message) ServletException (Throwable rootCause) ServeltException (String message Throwable rootcause) UnavailableException: (ServletException subclavils) Permanently not available: servlet is destroyed or not properly configured in some way. Temporarily not available: Due to the problem of system category. Java server page JSP model JSP model two objectives: 1 . Allow and encourage separation of the Presentation layer to Java code. Make developers to write a web with dynamic content with maximum capabilities and flexibility. Standard servlet extends java.servlet.httpservlets. Servlets are converted from JSP. Servlet must take advantage of javax.servlet.jsp.jsppage interface method: void jspinit () Void Jspdestroy () javax.servlet.jsp.httpjsppage interface Added the VOID _JSPSEQUEST REQ, HTTPSERVLETREQUEST REQ, HTTPSERVLETRESPONSE RES process as follows: 1. Translate page (.jsp to .java) 2. Compile JSP (.java to .class) 3. Loading class 4. Create an instance 5. Call jspinit () when the page is first initialized. 6. Call _jspdestroy () 7. Call _jspdestroy () 7. Call _jspdestroy () .jsp Despdestroy () .jsp Down Implicit Note: JSP Syntax <% -%> Disclaimer: Java Variables and Methods You can access by JAVA Variables and Methods JSP Syntax <%! Declaration;%> XML Syntax
3. Can consist of multiple parts or expressions ScripletJsp syntax <% code fragment%> XML syntax