JSP built-in objects
JSP creates a lot of built-in objects dynamically at compile time, so if the developer knows, they can be used directly in JSP pages. We refer to these built-in objects as JSP built-in nine large objects.
If you need to get the following nine built-in objects directly, you can do this:
Write an error handling page, then request to view the translated JSP file.
public void _jspservice (HttpServletRequest request, httpservletresponse response) throws Java.io.IOException, servletexception { PageContext pagecontext = null; HttpSession session = NULL; Throwable exception = org.apache.jasper.runtime.JspRuntimeLibrary.getThrowable (request); if (Exception! = null) { response.setstatus (httpservletresponse.sc_internal_server_error); } ServletContext application = null; ServletConfig config = null; JspWriter out = null; Object page = this; JspWriter _jspx_out = null; PageContext _jspx_page_context = null; ......}
Because the JSP script is written in JSP and the JSP output expression will be translated by default in _jspservice () then the nine objects defined in this method can be used arbitrarily by the developer.
JSP Nine main objects |
Servlet type |
Request |
HttpServletRequest |
Response |
HttpServletResponse |
Session |
HttpSession |
Config |
ServletConfig |
Application |
ServletContext |
Out |
JspWriter |
Page |
Object |
PageContext |
PageContext |
exception |
Throwable |
1 out Object
Use both out and response output character streams to output data to the page.
<% out.write ("jack<br/>"); Response.getwriter (). Write ("lucy<br/>");%>
The output is Lucy Jack. Because the above two are character streams with their own buffers, the jspwriiter buffer data is flushed to the buffer of the response character stream after the JSP has finished executing, so the output data for the Out object is later. If you need to output in advance, you need to make a forced flush of buffer data.
<% out.write ("jack<br/>"); Out.flush (); Response.getwriter (). Write ("lucy<br/>");%>
2 output data at the same time using the byte stream of jspwriiter and response.
<% out.write ("jack<br/>"); Out.flush (); Response.getoutputstream (). Write ("lucy<br/>". GetBytes ());%>
The above code runs the result jack then throws an exception getwriter () has already been called for this response. You cannot use both a byte stream and a character stream in a JSP.
3. How to use JSP to achieve the download of pictures.
<%@ page language= "java" import= "java.util.*,java.io.*" pageencoding= "UTF-8"%><% //Get Picture Resources InputStream in = Application.getresourceasstream ("/imgs/0004.jpg"); Specify the processing mode response.setheader ("Content-disposition", "attachment;filename=0004.jpg"); Get byte output stream byte [] b = new byte[1024]; int len = 0; Read side write outputstream output = Response.getoutputstream (); while (len = In.read (b))! =-1) { output.write (B,0,len); } Release Resources In.close ();%>
To avoid using out objects in the page JSP, you need to remove all the template elements from the JSP, including the carriage return and line breaks in the page.
4. Use the Write method and the Println method of the out implicit object.
<% String str1 = "Data"; String str2 = null; int a = n; Out.write (STR1);//Data out.write (STR2);//Do not show out.write (a);//A out.write ("Two PageContext objects
The main description of the PageContext class is the context of the JSP page, which can obtain information about the servlet, or it can pass the context of the current JSP to the specified class to implement the action on the JSP page.
1. Get all the data in the JSP
<% Out.write ((pagecontext.getrequest () = = Request) + "<br/>") Out.write ((pagecontext.getresponse () = = Response) + "<br/>") Out.write ((pagecontext.getsession () = = session) + "<br/>") Out.write (( Pagecontext.getservletconfig () = = config) + "<br/>") Out.write ((pagecontext.getservletcontext () = = Application ) + "<br/>"), Out.write ((pagecontext.getpage () = = page) + "<br/>"), Out.write ((pagecontext.getexception () = = exception) + "<br/>") Out.write ((pagecontext.getout () = = out) + "<br/>");%>
Think: Why does Sun need to get the other eight big objects through PageContext?
Because later if you need a common Java class to process the JSP page data, you can simply pass the PageContext class to the past. For example: custom labels.
2. Common domain
We will be able to use the Setattribvute ()/getattribute () method to store and retrieve data called domain objects.
Domain Object |
Life cycle |
Page |
Valid in the current page |
Request |
Request Forwarding |
Session |
Default Half Hour |
Application |
When the server shuts down |
3. Set and get different domain properties
<%--sets properties for different domain objects--%><% pagecontext.setattribute ("page", "This is the current page"); Pagecontext.setattribute ("name", "Lucy", Pagecontext.request_scope);p agecontext.setattribute ("Age", "38", Pagecontext.session_scope); String likes[] = {"Football", "Sleep", "basketball"}; Pagecontext.setattribute ("likes", likes,pagecontext.application_scope);%><%--Get domain Properties--%><%= Pagecontext.getattribute ("page")%><br/><%= pagecontext.getattribute ("name", Pagecontext.request_ SCOPE)%><br/><%= Pagecontext.getattribute ("Age", Pagecontext.session_scope)%><br/><%= Arrays.tostring ((string[]) pagecontext.getattribute ("likes", Pagecontext.application_scope))%><br/>
Summarize:
The specified domain can be displayed when using PageContext to set and get domain properties, and if no domain is specified, the default is the page field.
Problem:
To set properties and get properties in real development is a program developed by different programmers, so what should I do if the developer doesn't know exactly which domain the property name is stored in when getting it?
Solution: You can use the following statement
<%= pagecontext.findattribute ("test")%>
The statement defaults from Pageàrequestàsessionàapplication to find the desired property one by one if it finds a direct return.
So the statement is the underlying implementation principle of the El expression.
Java Learning Note-JSP3 (34)