SCWD knowledge point summary

xiaoxiao2021-03-06  68

Servlet container model

l Three basic levels:

1, context (all global resources in the application)

2, session

3, application in different regions in distributed environments

l ServletContext:

It is included in the 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.MALFORMEDUREXCEPTION

InputStream GetResourseAasstream (String Path)

String getMimetyPE (String file)

RequestDispatcher getRequestDispatcher (String Path)

Void SetaTribute (String Name, Object Value)

Object getattribute (String name)

l The difference between context parameters and attributes:

1. The parameters can only be set in the container or web.xml and the attribute is set by servlet or container.

2, the parameter only returns String and the attribute returns Object

3, attribute name name with package structure

l ServleContextListener:

Void ContextInitialized (ServletContextevent E)

Void ContextDestroyed (ServletContextEvent E)

Its effect is to initialize all data before calling a servlet request.

The logo appears before the servlet declaration.

l ServletContextEvent:

ServletContext GetServletContext ()

l ServletContextAttributeListenr:

Void Attributeadded (ServletContextttributeEvent E)

Void AttributeRemoved (ServletContextttributeEvent E)

Void AttributeReplaced (ServletContextAttributeEvent E)

l ServletContextAttributeEvent:

String getName ()

Object getValue ()

l httpsession:

The session sends the first request to be established, which does not automatically track user operations, access with the HTTPSERVLETREQUEST class:

HttpSession GetSession (Boolean)

Then use the HTTPSession class to use session data:

Object getattribute (String name)

Void SetaTribute (String Name, Object Value)

Void Removettribute (String Name)

l HttpSessionListener:

The following methods must be implemented: Void SessionCreate (HttpSessionEvent E)

Void sessionDestryed (httpsessionever e)

l HttpSessionEvent:

HttpSession GetSession ()

l httpsessionattributeListener:

Void Attributeadd (HttpSessionBindingEvent E)

Void AttributeRemoved (HttpSessionBindingEvent E)

Void AttributeReplaced (HttpSessionBindingEvent E)

l HttpSessionBindingEvent:

HttpSession GetSession ()

String getName ()

Object getValue ()

l HttpSessionActiVationListener:

Void SessionDidActive (HttpSessionEvent SE)

The above method is responsible for reading the contents of the file in the session while transferring the session and writing it to the new server.

Void sessionWillPassivate (HttpSessionEvent SE)

The above method is responsible for writing the contents in the file into the session before transferring the conversation.

l HttpSessionBindingListener:

It and the difference between HTTPSESSIONATTRIBUTELISTENER is 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 evenet)

Void valueunbound (httpsessionBindingEvent evenet)

l can be distributed

1. Variables should be done differently: 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 sign between the description of the application and the context parameter

l filter

Life cycle

When the filter is intercepted response object, if the output stream is closed by servlet, the filter will not change the information of the output stream, therefore, encourage the FLUSH () refresh stream for servlet, not using Close () to close the stream.

2. Filter

Filter classes must be:

1. Implement proper interface

2. Define method

3. Declared in the deployment descriptor

3. Create a filter

Implement Javax.Servlet.Filter interface

Implementation:

Public void init (FilterConfig Config)

Public void Dofilter

ServletRequest Req, ServletResponse Resp, Filterchain Chain

Public void destroy ()

Current filter calls 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 (must appear before the servlet flag)

Map to servlet or JSP:

1.

2.

/ *. jsp

the term:

Clustering, Listener (Receiver), Context Object (context)

Scope, Distributable, SESSION (session)

Event (event), SYNCHRONIZATION, Filter (Filter)

Abnormal processing

l problem notice

Senderror:

Senderror () method provided by HTTPSERVLETRESPONSE class

Public Void Senderror (int Sc)

Public Void Senderror (int Sc, String MSG)

Three things will happen using the senderror () method:

1. Send an error response to the client using the specified status code

2. Replace the response body of the server error page with the HTML format containing the specific message.

3. Set the content type to text / HTML and do not modify the cookie and other titles.

SetStatus

Public void setStatus (int statuscode)

SetStatus (int StatusCode) cannot generate an automatic error response page, while Senderror can.

SetStatus does not submit 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 has been submitted, ignore the setstatus (...) call.

Generally used for non-error scenarios (SC_OK / SC_MOVED_TEMPORARILY)

Senderror is generally used for error scenarios.

l Error page

SetStatus (...) and senderror (...) defaults to generate an error page that is formatted by the server.

a) static error page

404

/ERRR/404.html (must start with a slash)

b) 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 the enderror (...).

A dynamic page or servlet can be used for multiple error codes. Each is declared in DTD. Each error code must have its own

l Pass the error

Error properties must be set before the forwarding request.

l Record the message

The GenericServlet class provides two ways to write errors to log files:

Public void log (String MSG) writes an error message to the servlet log file.

Public void log (String Msg, Throwable T) Write an error message and the throwable object to the servlet log file .l report message

The PrintStackTrace () method returns Void. In order to pass the error message to the client,

Senderror (...) must be called, but the record must be captured and written to the PrintStream or PrintWriter before the message can be delivered:

Void PrintStackTrace ()

Void PrintStackTrace (PrintStream P)

Void PrintStackTrace (PrintWriter P)

l servlet

Method throws an exception:

Let servlet capture

2. Turn to the server

The specification clearly indicates that the exception of the server must be the subclass of IOException ServleTexception runtimeException.

ServletException: (Subclass of Exception)

Servletexception ()

ServletException (String Message)

ServletException (throwable rootcause)

ServelTexception (String Message, Throwable Rootcause)

UNAVAILABLEXCEPTION: (Subclass of ServletException)

Permanently unavailable: 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

Two objectives of the JSP model:

1. Allow and encourage the separation of the Presentation layer and Java code

2. Make developers to write a web with dynamic content with maximum capabilities and flexibility.

Standard servlet extends java.servlet.httpservlet

Servlets converted from JSP must take advantage of javax.servlet.jsp.jsppage interface

method:

Void jspinit ()

Void jspdestroy ()

Javax.servlet.jsp.httpjsppage interface increased

Void _JspService (httpservletRequest Req, httpservletResponse res)

The process is as follows:

1. Translate page (.jsp to .java)

2. Compile JSP (.java to .class)

3. Load the class

4. Create an instance

5. Call JSPinit () at the first initialization of this page.

6. Request call for each JSP _jspdestroy ()

7. Call _jspdestroy when the server destroys the JSP.

JSP element

l implicit note:

JSP syntax <% - Comment -%>

l Declaration: Java variables and methods that can be accessed

JSP syntax <%! Declaration;%>

XML Syntax

Declaration;

Rules for 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.

l expression

JSP syntax

<% = expression%>

XML syntax

Expression;

Expression rules:

1. It is usually not ended with a semicolon.

2. Severage from left to right.

3. Can consist of multiple parts or expressions

l scriplet

JSP syntax

<% Code Fragment%>

XML syntax

Code Fragment;

l statement

Processing flower brackets

l

1. The incline instruction contains 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 / isrrorPage / ContentType

/ PageEncoding

XML syntax

rule:

1) Use Page multiple times in a translation unit

2) 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.

l implicit object

PiCATION object:

Web.xml:

Can be obtained 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 it 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:

Binded to pageContext, getAttribute (String Name) accesses data.

OUT object:

It belongs to the javax.servlet.jsp.jspwriter class, which is a buffer version of Java.io.PrintWriter.

Buffer size can set <% @ page buffer = "16kb"%>

Page object:

It is equivalent to this THIS in Java.

Exception object:

Is an instance of java.lang.throwable, if the ErrorPage property of the PAGE instruction is used, then the Exception object is not available to the page. E.g:

<% @ Page isrrorpage = "true"%>

Implicit object data type scope

Application java.servlet.ServletContext application

Config javax.servlet.ServletConfig page

Request Javax.Servelt.http.httpservletRequest request

Response javax.servlet.http.httpservletResponse request

Session javax.servlet.http.httpsession session

PageContext javax.servlet.jsp.pageContext page

Out javax.servlet.jsp.jspwriter page

Page Java.Lang.Object Page

Exception java.lang.throwable page

<% Exception.PrintStackTrace (New PrintWriter (out);%>

Expression <% = ...%> is the value when processing the HTTP or HTTP server processing request.

Scriptlet <% ...%>

Execute it when processing the request, embedded in the _jspservice (...) method

Statement <%! ...%>

Initialization when calling JSP JSPinit () method

Directive <% @ ...%>

It is called when translated.

l operation

JSP: incrude

oral

Page Specifies the relative URL of the target resource

Param logo determines the parameter value required to pass

The Flush flag is equal to TRUE or FALSE defaults to false requirements set to True

Include has two methods:

1. An instruction with the file attribute contains a static file 2. Use operations to make a statically or dynamic output of a file into the response

JSP: Forward:

No code after the JSP: Forward module is processed.

JSP: Plugin:

TYPE = "bean | applet"

Code = "classfilename.class"

>

Text Message for User

JSP: Usebean

Scope = "Page | Session | Application"

{

Class = "package.class" |

TYPE = "package.class" |

Class = "package.class" type = "package.class" |

Beanname = "{package.class | <% = expression%>}"

TYPE = "package.class"

}

/> |

At the same time, Class and Beanname are invalid.

JSP: setProperty

JSP: getProperty

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

New Post(0)