Chapter 1
Servlet
Getting started
1. HTTP servlet basics: httpservlet is used as an abstract class to create your own HTTP servle and extends the genericservlet class. The subclass of the httpservlet class must override at least one of the two methods: doget () and dopost (). Servlet format: public class classname extends httpservlet {public void doget (httpservletrequest request, response) throws servletexception, ioexception {} public void doget (httpservletrequest request, response) throws servletexception, ioexception {
}
}
Ø servlet lifecycle: 1) instantiate: The servlet container creates the Instance Object of the servlet class.
2)
Initialization: The servlet container calls the servlet Init () method and usually requests resources back.
I nit () method
3)
Service: A container is used to respond to the client's request to the servlet.
4) Destruction: resources are usually released when called before the servlet instance is released. Destory () 5) unavailable: Release instances in memory.
2. httpservletrequest interface: ø obtain the Chinese parameter: request. setcharacterencoding ("GBK") ø request parameter: String STR = request. getparameter (""); string [] arr = request. getparametervalues ("") ø returned Session Object: httpsession session = request. getsession (true/false); ø bind a key-Value Pair: request. setattribute ("attrname", attrvalueobject); ø obtain the bound object: Object OBJ = request. getattribute ("attrname"); ø internal jump: request. getrequestdispatcher ("/url "). forward (request, response); ø get all cookies: Cookies [] arr = request. getcookies (); 3. httpservletresponse interface: ø set the Chinese parameter: response. setcontenttype ("text/html; charset = GBK"); ø get the response body output value: printwriter out = response. getwriter (); ø external redirection: response. sendredirect ("url"); ø rewrite the URL to write the sessionid to the URL: response. encodeurl ("url"); ø add COOKIE: Response to the response header. addcookie (cookie) ø set the response header: response. sethead ("refalsh", 2) ø refresh to URL in two seconds: 4. httpsession interface: ø bind a key-Value Pair: Session. setattrbute ("attrname", attrvalue); ø active and invalid: Session. invalidate (); 5. servletcontext interface: ø bind a key-Value Pair: context. setattrbute ("attrname", attrvalue); ø obtain the absolute position of the file on the disk: String filepath = context. getrealpath ('/WEB-INF/password.txt ")
Chapter 2
JSP1. JSP page structure: l static web page: HTML content l command: <% @ %> there are three types: Page command, clude command, taglib command such: <% PAGE import = "" %>, <% @ include file = "file name" %> L expression: <% = %> it cannot contain symbols, such "; "L sciptlet: Java code <%> L Declaration: <%! %> L action: <JSP: usebean/> there are six L Annotations: there are two types: 1. <! --> And <% -- %> 2. implicit object: the JSP implicit object is a group of class instances loaded by web containers. It does not use "new" to obtain instances as common Java objects do, the object that can be used directly on the JSP page. The implicit objects provided by JSP are divided into four main categories,
| Scope communication object |
3. jsp standard action Introduction: L <JSP: usebean id = "" class = "" Scope = ""/> where: ID is used to create a reference name for the bean. The bean class specified by class. Scope specifies the bean range. The default value is Pagel <JSP: setproperty name = "" property = "" value = "" Param = ""/>, where: name specifies the ID of the bean used in usebean. Property specifies the name of the bean whose value you want to set. Value specifies the explicit value Param for the property to be set to specify the HTML tag used to input and assign values to the property. L <JSP: getproperty name = "property ="/> name specifies the ID of the bean used in usebean. Property specifies the name of the bean whose value you want to set. L <JSP: Forward page = "url"/> L <JSP: Include page = "" Flush = "true"/>. Page indicates the URL of the current page to be embedded. The flush attribute is used to clear data stored in the buffer before embedding other responses. <JSP: param> the element can be used together with the include action. For example: <JSP: Include page = "url" Flush = "true">
<JSP: Param name = "paramname" value = "paramvalue"/> </jsp: Include> 4. jsp standard tag library, which has four types. The core tag library is used as the core tag library as follows:
L
General tags: Three
L loop Tag: Two L International and formatted tag libraries
5.
Filter
L The lifecycle filter must implement the javax. servlet. Filter interface to import all the methods in the filer interface.
The lifecycle of a filter is similar to that of a servlet, including the following phases:
1. instantiation: 2. Initialization: The init () method is called to initialize the filter.
3.
Filter: The dofilter () method is called whenever the user submits a request or the web resource sends a response.
4. Destroy: Call the destroy () method of the filter to clear and release resources. L filter definition and filter ing: Before using a filter, you must define the Filter and Its ing in the web. xml file. Format: <filter> <filter-Name> myfilter </filter-Name> <filter-class> classname </filter-class> <init-param>
<Param-Name> parameter name </param-Name>
<Param-value> parameter value </param-value> </init-param> </filter> <filter-mapping> <filter-Name> myfilter </fiter-Name> <URL-pattern>/* </url-pattern> <filter-mapping> where: <filter-Name> specify the name of the filter <filter-class> specify the name of the class. <Param-Name> specify the parameter name <param-value> specify the parameter value <URL-pattern> specify the URL of the servlet, JSP page, or HTML page. To apply the filter to each resource in the Web application program, use the/* l filter format in the <URL-pattern> element: public class filtername extends httpservlet implements filter {public void Init (filterconfig) throws servletexception {} public void dofilter (servletrequest request, response, filterchain) {} public void destroy () {}}