JSP (3): Understanding of built-in objects and four domain objects

Source: Internet
Author: User

Origin: In JSP development, a number of objects are used frequently. such as Httpsession,servletcontext,servletcontext,httpservletrequet. So when Sun design jsp, when the JSP page is loaded, it will automatically help developers to create these objects, developers only need to directly use these objects to invoke the method! These created objects are called built-in objects, and there are nine of them.

Built-in Object name Type
Request HttpServletRequest
Response HttpServletResponse
Config ServletConfig
Application ServletContext
Session Httpsession
exception Thrwable (Error handling page)
Page Object (this: The current JSP translation class, as we wrote last time, hello.jsp translation becomes the life cycle method in Hello_jsp.java)
Out JspWriter
PageContext PageContext

We can open a Java source file with JSP translation

public void _jspservice (HttpServletRequest request, HttpServletResponse    Response) throws Java.io.IOException, servletexception {PageContext pagecontext = null;    HttpSession session = NULL;    ServletContext application = null;    ServletConfig config = null;    JspWriter out = null;    Object page = this;    JspWriter _jspx_out = null;    PageContext _jspx_page_context = null;      try {response.setcontenttype ("text/html;charset=utf-8");      PageContext = _jspxfactory.getpagecontext (this, request, response, NULL, True, 8192, true);      _jspx_page_context = PageContext;      application = Pagecontext.getservletcontext ();      Config = Pagecontext.getservletconfig ();      Session = Pagecontext.getsession ();      out = Pagecontext.getout (); _jspx_out = out; String path = Request.getcontextpath ();    String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; } 

As you can see from the above, built-in objects are created in the service method, and directives and declarations are member variables and methods, so built-in objects cannot be used in directives and declarations

1. Out Object

is the JspWriter class, which is equivalent to a printwriter with a cache. Somewhat similar to the PrintWriter method

If we execute the following code on the JSP page, we will find

<%    out.write ("abc");    Response.getwriter (). Write ("2222223");%>

Because the buffer is added to the out of the settings, so in the browser first print out the following statement, the front to wait for its buffer (the default size is 8KB) is not full, to wait until the JSP page after the execution of the print, we can also go to manually refresh the page

We can set the buffer size in the instruction section of the JSP.

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8" buffer= "8kb"%>

Let's continue with the experiment and run the following code on the JSP page

<%    out.write ("abc");    View the size of the buffer    System.out.println ("Size of the current buffer:" +out.getbuffersize ());    System.out.println ("Remaining size of the current buffer:" +out.getremaining ()); %>

See the result is this:

We sent only "ABC" in the browser to 3 bytes, but it is the size of more than 200 bytes, where is the extra part? This time we can all look at the Out_jsp.java file,

Out.write ("\ r \ n");      Out.write ("<! DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" >\r\n ");      Out.write ("

In fact, we will find that we send bytes to browse far more than ABC, in fact, there are some tags and other content, they are to be sent to the browser side, also accounted for size.

Let's do an experiment with the following code,

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8" buffer= "1kb"%><%system.out.println ( Out.getremaining ()); for (int i=1;i<=1024;i++) out.write ("a");//view buffer size Response.getwriter (). Write ("Guo Qingxing"); %>

See the effect at this time:

I don't know why I'm here. The initial buffer size is 1022 (I set it to be 1kb buffer= "1kb"), but this is not my focus, the point is he this diagram illustrates the operation mechanism of the buffer, if the buffer is full, immediately print it out, not full, Just wait for the page to finish printing, just like the first 1022 buffer full after the printing, and then back to put two a, when the buffer is not full, you have to wait for the JSP page to execute after loading, so appear after the "Guo Qingxing".

(I found I didn't know where two bytes were used to make the initial buffer size only 1022B, I tried buffer= "2kb" and buffer= "3kb" and found that the remaining size was normal)

2. Exception Object

The object represents the Exception object (Throwable object) thrown by other pages

We opened the JSP translated Java source files found, in the service method of the file actually eight built-in objects exist, but there is no such object, this is because we did not specify it as an error handling page, only to specify the current page is wrong in the wrong page to use it.

3. PageContext Object

JSP context object, in order to make it easier for us to use the other eight built-in objects.

(For example, when we want to call the Session object or request object outside of the service method)

public void _jspservice (Request,response) {

Creating built-in objects

HttpSession session = ....;

Ervletconfig config = ....;

Encapsulates 8 frequently used built-in objects into a PageContext object

PageContext PageContext = package;

Calling the Method1 method (if you pass the eight built-in objects as arguments to the following method is obviously too cumbersome, this time we can use the already encapsulated PageContext as parameters to pass the past is obviously much simpler)

Method1 (PageContext);

}

public void Method1 (PageContext pagecontext) {

Want to use built-in objects

Get 8 additional built-in objects from a PageContext object

JspWriter out =pagecontext.getout ();

HttpServletRequest rquest = Pagecontext.getrequest ();

........

}

Note: PageContext is also a domain object and already has four domain objects: Request,servletcontext, the session, and now PageContext, (the cookie is not a domain object, but the data is sent to the browser-side save), Now the servlet already has three domain objects, and the JSP has four domain objects (JSP is a servlet).

#保存数据

1) By default, save to page field

Pagecontext.setattribute ("name");

2) data can be saved to four domain objects

Pagecontext.setattribute ("name", domain-wide constant)

#获取数据

1), by default, gets from the page domain

Pagecontext.getattribute ("name")

2), can get data from four domains

Pagecontext.getattribute ("name", domain-wide constant)

Pagecontext.page_scope (PAGE field)

Pagecontext.request_scope (REQUEST domain)

PageContext. Session_scope (Session field)

Pagecontext.application_scope (aplication domain: i.e. servletcontext)

3), automatically search for data in four domains

Pagecontext.findattribute ("name");

If you have the same name, follow the order: page domain, request domain, session domain-> context domain (application domain)

We can do an experiment on PageContext.

<%//pagecontext as a domain object to save the data Pagecontext.setattribute ("message", "I am Coming");    Pagecontext.setattribute ("message", "I am the value in the REQUEST domain set in PageContext, I am coming", pagecontext.request_scope);     Request.setattribute ("name", "gqxing");//equivalent to the above code Request.setattribute ("value", "Request ' s value");      %> 

Results:

Understanding of four domain objects

You can try to identify the difference (the scope of the JSP's four domain objects)

page field (PageContext): can only be used for the current page, can not be used to do the forwarding of data sharing, nor do redirected data sharing

Request domain: Can only be used for data sharing on the same request, so it can only be used in forwarded requests

Session field: Can only be used for shared data in one conversation (one conversation: When a user opens a browser, browses multiple Web sites, closes the browser), forwarding and redirection are available

Context domain (application): can only be used in the same web app. (global)

We can do the following experiments:

Make an experiment with the forwarded request first, as follows:

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

Then on another page to accept the data

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

Finally, the results were found:

When we change the forwarding mode to redirect, as follows:

Response.sendredirect (Request.getcontextpath () + "/demo5.jsp");

This results

JSP (3): Understanding of built-in objects and four domain objects

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.