JSP (Java Server Page)
1. JSP Simple Introduction
The JSP full name is Java Server Pages, which, like Servle technology, is a technology JSP for developing dynamic Web resources as defined by Sun. The biggest feature of this technology is that writing JSPs is like writing HTML, but: it's more than HTML, HTML can only provide static data to the user, while JSP technology allows the nesting of Java code in the page, providing users with dynamic data compared to Servlets, servlets are difficult to typeset data, while JSP can generate dynamic Data with Java code. And it's easy to format the data.
2. JSP Application
Servlets are used as controller components in Web applications, and JSP technology is used as a data display template. The original reason is that the data of the program is usually beautified and then output: Let the JSP use Java code to generate dynamic data, and landscaping will cause the page difficult to maintain. Having the servlet produce both data and nested HTML code inside the data will also result in poor program readability and difficult maintenance. So the best way is based on the characteristics of the two technologies, so that they are responsible for each, the servlet is responsible for responding to the request to produce data, and the data through the forwarding technology to the JSP, the data display JSP to do that the servlet is no longer responsible for displaying the page, and by JSP to complete! And the servlet focuses on the content of the control layer!
3. JSP principle
When a client requests a JSP page, the server checks to see if the servlet corresponds to the presence of the JSP and, if so, invokes the servlet's service () method directly to process the request. If it does not exist, the server compiles the JSP into. Java, compiles. Java into A. class, and then calls the service () method. When this JSP page, the second processing request, directly call the straight service () method.
4. JSP code block
<%...%>: Code snippet. The code fragment will appear in the original as it is, without any change. Normal Java code can appear in the code snippet; <%=...%>: expression. The expression is output in the "<%=a%>", for example: Out.print (a) <%!...%>: declaration. The declaration corresponds to the properties and methods in the "<%" JSP annotations are--...--%>, and the contents of the comments are ignored by the JSP compilation system! The HTML comment:<!--...--, because the content that ultimately responds to the client is still HTML, so the content in the HTML comment is still invisible.
5. JSP hidden (suppressed, built-in) objects
A hidden object is a variable that can be used directly in a JSP page without creating it REQUESTJAVAX.SERVLET.HTTP.HTTPSERVLETREQUESTRESPONSEJAVAX.SERVLET.HTTP.HTTPSERVLETRESPONSECONFI Gjavax.servlet.ServletConfigPage Current Servlet Instance Thissessionjavax.servlet.http.HttpSessionapplicationjava.servlet.ServletContex Texceptionjava.lang.ThrowableOutjavax.servlet.jsp.JspWriterpageContextjavax.servlet.jsp.PageContext
6. JSP four domain objects
The PageContext object is a PageContext type, is itself a domain object, can also manipulate all domain objects, and can also get additional hidden object operations for all domains (four domains): You can manipulate all domain objects using the PageContext object, in the GetAttribute () , SetAttribute (), RemoveAttribute () Three methods add one more parameter, int scope to specify the range. Constants that contain four int types in the PageContext class represent four ranges: Request_scope:request range session_scope:session range application_scope:application range
7. PageContext object Get Hidden Object
Get other hidden objects: You can use PageContext to get other hidden objects. JspWriter getout (): Gets the Out hidden object; ServletConfig getservletconfig (): Gets the config hidden object; object GetPage (): Gets the page hidden object ; ServletRequest () getrequest (): Gets the request hidden object; Servletresponse () GetResponse: Gets the response hidden object; HttpSession getsession ( ): Gets the session hidden object; ServletContext getservletcontext (): Gets the application hidden object; jspexception getexception () : Gets the Jspexception object after the exception hidden object is converted
8. JSP directives
<%@ directive Name Property name = "Value" Property name = "Value"%> generally put JSP instructions at the top of the JSP file, but this is not necessary JSP has three major instructions: page, include, taglib, most commonly used, but also the most complex is the page directive Note: The Tomcat compilation system compiles the JSP according to the instruction information of the JSP to generate the Java file. In the generated Java file, there is no instruction information! ---------------page directive-->import property---------------import= "java.util.*,java.text.*" <%@ page language= "Java" Import= "java.util.*" pageencoding= "UTF-8"%>errorpage= "error.jsp" You can also configure the Web. XML with Error-page, if it is configured and can process, then use errorpageiserrorpage= "false" is a exception switch, to True there is iselignored= "false" Will not ignore el,contenttype= "text/html; Charset=utf-8 "UTF-8" as in Response.setcontenttype (): Indicates what encoding to use when saving and reading JSP pages when there is no contenttype, pageencoding= At the same time, there are contenttype. Autflush and buffer Generally, we're not going to set it up. Properties that are commonly used for default values are: Import pageencoding--------------- Iweb.xml Configuring the error page---------------<error-page><error-code>500</error-code><location>/ Error500.jsp</location></error-page><error-page><exception-type>java.lang.exception </exception-type><location>/error.jsp</location></error-page>---------------IThe nclude directive---------------include directives represent static inclusions! The include directive has only one property: file, which specifies the page to include, for example: <% @include file= "b.jsp"%>. Two pages are merged into a single file and then compiled into a servlet (Java file) * * Dynamically included is a servlet generated for two JSP pages, then 1.jsp "true" will call 2.jsp "_jspservice" () Methods such as 1.jsp<jsp:include page= "2.jsp" >---------------taglib directives---------------JSP pages using a third-party tag library, you need to use the TAGLIB directive to "Guide Package". For example: <%@ taglib prefix= "C" uri= "Http://java.sun.com/jsp/jstl/core"%> where prefix represents the label prefix, this name can be random. The URI is defined by a third-party tag library, so you need to know the URI of the third-party definition
JSP Dynamic Tags
JSP dynamic tags are the JSP's own label, not provided by a third party, so the use of JSP dynamic tags without using the taglib Instruction "guide package." The format of the JSP Dynamic label is: <jsp:xxx ...> Whether it's a custom JSP tag, or a JSP's own dynamic label, and a third-party label, the call to a set of methods, including the dynamic tag include tag, is dynamically included, Unlike the include directive, the include tag is the same as the function of the Requestdispatcher.include () method: <jsp:include page= "1/jsp"/> Forward Dynamic label forward tag is the function of request forwarding! The forward tag acts in the same way as the Requestdispatcher.forward () method <jsp:forward page= "1.jsp"/>usebean, SetProperty, GetProperty Dynamic label <jsp:userbean id= "P1" class= "Com.itheima.domain.Person"/><jsp:setproperty property= "*" Name= "P1"/><%=p1%>
EL1. JSP2.0 Introduction
JSP2.0 to separate HTML from CSS, to separate HTML from JavaScript, and to replace Java code blocks with tags. The benefits of labeling are available to non-Java personnel. jsp2.0– Pure Label page, that is: does not contain <% ...%>, <%! ...%>, and <%= ...%>
2. What is El?
EL (expression Language) is an expression language that corresponds to <%=...%>. We know that in the JSP, the expression is output, so the EL expression is also output in the format of the El Expression: ${...}, for example: ${1+2} When using an El expression, the Iselignored property of the page directive is required to be false. When the JSP is translated into. Java, the EL expression is not ignored if you want an EL expression to be ignored by the JSP compiler, you can add "\" before the El expression, for example: \${1+2}
3. Hidden objects in El
In an El expression, an object that can be used without creating it is called an El hidden (hidden, built-in) object. There are 11 hidden objects in El, all of which are similar to map----------------manipulate objects----------------1). Manipulate list and array: ${list[0]}, ${arr[0]};2). Manipulate Bean's properties: ${person.name}, ${person[' name '}, corresponding to the Person.getname () method; 3). Operation map Value: ${map.key}, ${map[' key '}, corresponding to Map.get (key)---------------- Implicit object----------------Implicit object name Type PageContextjavax.servlet.jsp.PageContext and JSP implicit objects are exactly the same PAGESCOPEJAVA.UTIL.MAP data in a page range Requestscop Ejava.util.MapsessionScopejava.util.MapapplicationScopejava.util.Mapheaderjava.util.MapheaderValuesjava.util.Mapparamjava . util. Mapparamvaluesjava.util.mapintiparamjava.util.mapcookiejava.util.map<string,cookie>key: Is the name of the Cookie, Value is the cookie object itself----------------an implicit object Description----------------1). Param:param is the map<string,string> type! The Param object can be used to get arguments, as is the case with the Request.getparameter () method, noting that when using El to get a parameter, if the argument does not exist, an empty string is returned instead of NULL. This is different from using the Request.getparameter () Method 2). Paramvalues:paramvalues is a map<string, string[]> type, which can be used when a parameter name corresponds to more than one parameter value 3). Headervalues:headervalues is the map<string,string[]> type. When a request header name corresponds to more than one value, the object 4 is used.Initparam:initparam is the map<string,string> type. It corresponds to the <context-param> parameter 5 in the Web. xml file. Cookie:cookie is the map<string,cookie> type, where key is the name of the cookie, and the value is the cookie object itself----------------operation of all domains (four domains)-------------- --You can use the PageContext object to manipulate all domain objects, add more than one parameter to the getattribute (), SetAttribute (), RemoveAttribute () three methods, and int scope to specify the range. Constants that contain four int types in the PageContext class represent four ranges: Request_scope:request range; session_scope:session range; application_ Scope:application Range Example: Pagecontext.setattribute ("User", User,pagecontext.requestscope);---------------- Get other hidden Objects----------------can use PageContext to get other hidden objects. JspWriter getout (): Gets the Out hidden object; ServletConfig getservletconfig (): Gets the config hidden object; object GetPage (): Gets the page hidden object ; ServletRequest getrequest (): Gets the request hidden object; Servletresponse getResponse (): Gets the response hidden object; HttpSession getsession () : Gets the session hidden object; ServletContext getservletcontext (): Gets the application hidden object; jspexception getexception () : Gets an example of the Jspexception object converted by the exception hidden object, how to invoke: PageContext.request.contextPath
Javaweb's Jsp/el (eight)