Java learning notes-JSP3 (34), learning notes jsp3

Source: Internet
Author: User

Java learning notes-JSP3 (34), learning notes jsp3
JSP built-in object

JSP dynamically creates many built-in objects during compilation. If you know, you can use these objects directly on the JSP page. We call these built-in objects the nine built-in JSP objects.

If you need to obtain the following nine built-in objects directly, you can do this:

Compile an error handling page and 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 JSP scripts and JSP output expressions are translated in _ jspService () by default, the nine objects defined in the above method can be used by developers at will.

JSPNine objects

ServletType

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 streams to output data to the page.

<%     out.write("jack<br/>");    response.getWriter().write("lucy<br/>");%>

The output is lucy jack. Because both of the above are slave streams with their own buffer, the JSPWriiter buffer data is refreshed to the buffer of the Response stream after the JSP execution is complete, so the output data of the out object is later. If you need to output data in advance, you need to forcibly refresh the data in the buffer zone.

<%      out.write("jack<br/>");     out.flush();     response.getWriter().write("lucy<br/>");%>

2. Use the JspWriiter and response byte streams to output data at the same time.

<%     out.write("jack<br/>");    out.flush();    response.getOutputStream().write("lucy<br/>".getBytes());%>

The above code runs the result of jack and then throws the getWriter () has already been called for this response exception. In JSP, you cannot use both byte streams and delimiter streams.

3. Use JSP to download images.

<% @ Page language = "java" import = "java. util. *, java. io. * "pageEncoding =" UTF-8 "%> <% // get image resource InputStream in = application. getResourceAsStream ("/imgs/0004.jpg"); // specify the processing method response. setHeader ("content-disposition", "attachment?filename=0004.jpg"); // get byte output stream byte [] B = new byte [1024]; int len = 0; // write OutputStream output = response while reading. getOutputStream (); while (len = in. read (B ))! =-1) {output. write (B, 0, len) ;}// release the resource in. close (); %>

To avoid using the out object in the page JSP, You need to delete all the template elements of the JSP, including the carriage return and line feed on the page.

4. Use the write method and println method of the out implicit object.

<% String str1 = "data"; String str2 = null; int a = 65; out. write (str1); // data out. write (str2); // do not display out. write (a); // A out. write ("

2. pageContext object

The PageContext class mainly describes the context environment of the JSP page. It can obtain servlet Information or pass the Context Environment of the current JSP to the specified class to perform operations on the JSP page.

1. Get all the data in 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 retrieve the other eight objects through pageContext?

If you need a common java class to process JSP page data in the future, pass the PageContext class directly. For example, custom tags.

2. Common Domains

We will be able to use the setAttribvute ()/getAttribute () method to store and obtain data objects called domain objects.

Domain object

Lifecycle

Page

Valid on current page

Request

Request forwarding

Session

The default value is half an hour.

Application

When the server is shut down

3. Set and obtain attributes of different domains

<% -- Set attributes for different domain objects -- %> <% pageContext. setAttribute ("page", "this is current page"); pageContext. setAttribute ("name", "lucy", PageContext. REQUEST_SCOPE); pageContext. setAttribute ("age", "38", PageContext. SESSION_SCOPE); String likes [] = {"football", "sleep", "basketball"}; pageContext. setAttribute ("likes", likes, PageContext. APPLICATION_SCOPE); %> <% -- get domain attributes -- %> <% = 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/>

 

Summary:

The specified domain that can be displayed when you set and obtain the domain attributes using pageContext. If no domain is specified, this domain is the page domain by default.

Problem:

In actual development, setting properties and obtaining properties are programs developed by different programmers respectively. If the developer does not know the domain in which such property names are stored, so what should we do?

Solution: You can use the following statement

<% = PageContext. findAttribute ("test") %>

By default, this statement finds the required attributes one by one from page à request à session à application. If yes, it returns directly.

Therefore, this statement is the underlying implementation principle of EL expressions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.