SCWD surely summary

xiaoxiao2021-03-06  70

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. The flag appears before the servlet declaration.

 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  Filter between the application and context parameters 1. Lifecycle When the filter is intercepted response object, if the output flow is closed by servlet, the filter will not change the output. The information of the stream, therefore, it is encouraged the Flush () refresh stream for servlet, not the flow of Close (). 2. Filter filter class must: 1. Implement the appropriate interface 2. Define method 3. Declare in the deployment descriptor 3. Create a filter implementation Javax.Servlet.Filter interface implementation method: public void init (FilterConfig config) PUBLIC Void Dofilter (ServletRequest Req, ServletResponse Resp, Filterchain Chain) Public Void Destroy () Current filter Call CHAIN.DO FILTER (REQ, RESP) can be transferred to the next filter. The Destroy () method is called by the web container.

4. Define the deployment descriptor (before you must appear before servlet flag) < /> Method for mapping to servlet or JSP: 1. 2. / *. jsp terminology: Clustering (Cluster), Listener, Context Object (context) Scope (scope), Distributable, session EVENT, SYNCHRONIZATION (Synchronization), Filter Processing Exception  Problem Notification Senderror: SendError () Method for HTTPSERVLETRESPONSE Class PUBLIC VOID Senderror (int Sc) Public Void Senderror (int Sc, String MSG) uses the senderror () method will have three things: 1. Use the specified status code to send an error response 2. Replace the Servlet response with the server error page with the HTML format containing the specific message. Text .3. Set the content type into Text / HTML, do not modify cookies and other titles .SetStatus Public Void SetStatus (int status) setStatus (int StatusCode) cannot produce an automatic error response page, while SendError (...) can. SetStatus (...) Will not submit a response, he will cause the container to clear the response buffer. The container sets the local title, keeps the cookie and other titles. If the response is already submitted, ignore the call of setstatus (...). Generally used for non-error scenarios (SC_OK / SC_MOVED_TEMPORARILY) Senderror is generally used for error scenario.  Error page SetStatus (...) and senderror (...) By default, a server formatted error page. A) Static Error page 404 / error/404.html (must start with a slash) ) Dynamic Error page javax.servlet.error. Status_code returns an Integer object that specifies the error status code .javax.servlet.error.Message Returns a String message, usually specified by the second parameter passed to Enderror (...). A dynamic page or servlet can be used for multiple errors Code. Each must be declared in DTD. Each error code must have its own

 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 Declaration; The rules of writing declarations: 1. You can declare static or instance variables, new methods or internal classes. 2. Each variable declaration must end with a semicolon. 3. The variables and methods that can be used through the Import statement can be used without the additional declarations required. 4. After the declaration method and variables, the subsequent Java code is available. 5. It typically contains code that will exceed the _jspservice (...) method exceeding the servlet.  Expression JSP Syntax <% = expression%> XML syntax expression; expression rules: 1. Normally not end with a semicolon. 2. Severage from left to right.

3. Can consist of multiple parts or expressions  ScripletJsp syntax <% code fragment%> XML syntax code fragment;  Statement Processing Parentheses  Instruction 1. Include instructions contain a static file JSP syntax: <% @ include file = "relativeURL"%> XML syntax: The path is relative to the context as "/". The path starts on a directory or file name, then the path is relative to the JSP page. 2. The Page instruction definition will be applied to the properties of the entire JSP page. Jsp Syntax <% @ page Attributes%> language / extends / import / session / buffer / autoFlush / isThreadSafe / info / errorPage / isErrorPage / contentType / pageEncodingxml Syntax rules: 1) a translation unit can be Using Page2 multiple times) In addition to import, a translation unit can only use a PAGE attribute 3) Page instructions can be placed anywhere in the JSP page. 3. Taglib instruction Specifies the prefix of the flag library and custom flags used in the JSP page. JSP syntax: <% @ Taglib URI = "Uriforliabrary" prefix = "tagprefix"%> The prefix is ​​unique and cannot be used: JSP JSPX Java Javax Servlet Sun SUNW.  Include Object Plication Object: Web.xml: can get as follows: Application.GetinitParametr ("paraName"); pageContext object: method: getOut () getException () getpage () getRequest GetResponse () getsession () getServletConfig () getServletContext () Forward () include () handlepageException () It has a page scope, which means that the object can only be accessed through the _jspservice () method. Config object: Access the config attribute through the GetAttribute (String Name) of PageContext. Request object: This request is also within the scope even if ForWard () include (), etc. is used. Response object: Bind to PageContext, can be accessed in _jspservice (...). If the buffer of the PAGE instruction is not set to NONE, HTTP status code, and the title can be changed after sending to the client. Session object: Bind to pageContext, getAttribute (String Name) accesses data. OUT object: belongs to the javax.servlet.jsp.jspwriter class, which is a buffer version of Java.io.PrintWriter.

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

New Post(0)