Javaweb Summary (iv)-jsp in-depth analysis

Source: Internet
Author: User
Tags to domain

One, JSP domain object

1.JSP attribute range (domain object range)

JSP provides four domain objects, namely PageContext,request,session,application.

PageContext: The attribute scope is limited to the current JSP page. An attribute can only be obtained on one page, and jumps to other pages cannot be obtained.

Request: attribute scoped to the same request only. A set of properties on a page, as long as the server's jump , the page after the jump can continue to get.

session: the properties stored in the Session object can be accessed by all servlets and JSP pages that belong to the same session. The browser opens to close called a session.

application: the properties stored in the Application object can be accessed by all servlets and JSP pages of the same Web application.

2. Methods related to domain objects

(1) SetAttribute ()

The method that sets the property. The four attribute ranges previously explained are actually set by the PageContext attribute range. Open the description document where the PageContext is located.

The PageContext class inherits the Jspcontext class, and the SetAttribute method is defined in the Jspcontext class, as follows:

There is a scope integer variable in this method that represents the extent to which an attribute is saved.

There is a variable of type int, which can be found in PageContext 4.

    public static final int page_scope = 1;    public static final int request_scope = 2;    public static final int session_scope = 3;     public static final int application_scope = 4;

If the setattribute () method does not write the scope parameter of the following int type, this parameter defaults to Page_scope, at which point the setattribute () method sets the page property range, If the int type parameter passed over is Request_scope, then the SetAttribute () method sets the request attribute range, and the same is true for the scope parameter passed to Session_scope and Application_ Scope, it means that the setattribute () method sets the session property scope and the application attribute range.

(2) getattribute (String name)

Gets the specified property.

(3) Getattributenames ()

Gets the enumeration object that consists of all property names.

(2) RemoveAttribute (String name)

Removes the specified property.

Second, request forwarding and redirection

1. Usage

Forwardservlet.java

public class ForwardServlet extends Httpservlet{public void doget (HttpServletRequest req,httpservletresponse resp) Throws servletexception,ioexception{string Age = Req.getparameter ("Age"); System.out.println ("forwardservlet:age =" + age);//Request Forwarding//1. Call HttpServletRequest Getrequestdispatcher () method to get the RequestDispatcher object. String Path = "Testservlet"; RequestDispatcher dispatcher = Req.getrequestdispatcher ("/" + path)//2. Call RequestDispatcher Object Forward () Method Dispatcher.forward (REQ,RESP);} public void DoPost (HttpServletRequest req,httpservletresponse resp) throws Servletexception,ioexception{doget (req, RESP);}}

Testservlet.java

public class Testservlet extends Httpservlet{public void doget (HttpServletRequest Req,httpservletresponse resp) throws Servletexception,ioexception{string age = Req.getparameter ("Age"); System.out.println ("testservlet:age =" + age);} public void DoPost (HttpServletRequest req,httpservletresponse resp) throws Servletexception,ioexception{doget (req, RESP);}}

Results:

Forwardservlet:age = 123
Testservlet:age = 123

If you redirect the ForwardServlet internally to Testservlet.

public class ForwardServlet extends Httpservlet{public void doget (HttpServletRequest req,httpservletresponse resp) Throws servletexception,ioexception{string Age = Req.getparameter ("Age"); System.out.println ("forwardservletage =" + age);//The redirect of the requested string path = "Testservlet"; resp.sendredirect (path); public void DoPost (HttpServletRequest req,httpservletresponse resp) throws Servletexception,ioexception{doget (req, RESP);}}

Results:

Forwardservletage = 23
Testservlet:age = null

Javaweb Summary (iv)-jsp in-depth analysis

Related Article

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.