2017.10.9 Response object, Application object, Session object difference

Source: Internet
Author: User

1.response objects

The response object corresponds to the Request object, and the server outputs information to the client. When the server transmits data to the client,

The JSP container automatically creates the response object and encapsulates the information into the response object, and when the JSP container finishes processing the request, the response

The object will be destroyed. Response and request combine to complete the interactive function of dynamic Web pages.

Common methods of 1.1 response objects

The Response object provides the page redirection (Sendredirect) method, the Set state line (SetStatus) method, and the Set text type (setContentType) method

Method description

Sendredirect (String URL) sends a redirect response to a client using the specified redirect location URL

Setdataheader (String name,long data) sets a response header with the given name and date value, and if the specified name is already set, the new value overrides the old value

SetHeader (String name,int value) sets a response header with the given name and integer value, and if the specified name is already set, the new value overrides the old value

setContentType (String name,int value) sets the content type for the response, whose parameter values can be tex/html, Text/plain, Application/x_msexcel, or application/m Sword

setcontentlength (int len) sets the content length for the response

SetLocale (Java.util.Local Loc) set up region information for the response

1.3 Redirect Web pages

Using the Sendredirect () method in the Response object

Page timed refresh or auto jump

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%String Path=Request.getcontextpath (); String BasePath= Request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >The current time is:<%=NewDate (). toLocaleString ()%><br/> 

Use the Session object to get the conversation information and display

<% @page contenttype= "text/html" import= "java.util.*" pageencoding= "UTF-8"%>        The session creation time is:         <%=New Date (Session.getcreationtime ())%><br/>                session ID Number:        <% Session.getid ();%><br/>                 customer last visited:        <%=new java.sql.Time ( Session.getlastaccessedtime ())%>                 Two request interval how long the session will be canceled (ms):        <%= Session.getmaxinactiveinterval ()%><br/>        is the newly created session:<%=session.isnew ()? " Yes ":" No "%>    </body>

Case-----Statistics Website Visitors

<% @page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>Integer Yournumber=NewInteger (0); %> <%if(Session.isnew ()) {//if it's a new sessionInteger number= (integer) application.getattribute ("Count"); if(number==NULL)//if is the first to visit this site{ number=NewInteger (1); }                  Else{ number=NewInteger (Number.intvalue () +1); } application.setattribute ("Count", number); Yournumber= (Integer) application.getattribute ("Count"); }         %>Welcome to visit this site, you are the first<%=yournumber%>access to a user. </body>

Using out objects to respond to user output

<% @page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>        use of out-of-object        output page information             out.print ("aaa<br/>bbb");            Out.print ("<br> username or password is incorrect, please re-<a href= ' http://www.sohu.com ' ><font size= '" color= ' red ' > Login </font></a> ");            Out.print ("<br><a href= ' Javascript:history.back () ' > Back </a> ...");          %>    </body></HTML>

2017.10.9 Response object, Application object, Session object difference

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.