Jsp learning notes 4: built-in objects, built-in jsp learning notes

Source: Internet
Author: User

Jsp learning notes 4: built-in objects, built-in jsp learning notes

Application object:

Set the data shared in the application with the name and val value.

<%  application.setAttribute("name",val);%>

Obtain the value of shared data in an application named name.

<%  application.setAttribute("name");%>

 

Exception object:

It is generally used on the exception handling page. When an error occurs on the current page, the error type and error message are uploaded to the exception handling page. On the exception handling page, you can use the following command to obtain the error information and type:

<%=exception.getClass()%><%=exception.getMessage()%>

 

Out object:

<%  out.println("<tr>");  out.println("text");  out.println("</tr>");%>

Same effect

<tr>text</tr>

 

PageContext object:

Set variables in different ranges:

// Set the variable named page. The value is hello and the default value is page. You do not need to add the parameter pageContent. setAttributesScope ("page", "hello"); // set the variable named request2. The value is hello and the range is requestpageContent. setAttributesScope ("request2", "hello", PageContext. REQUEST_SCOPE); // set the variable named session2. The value is hello and the range is sessionpageContent. setAttributesScope (); ("session2", "hello", PageContext. SESSION_SCOPE); // set the variable named app2. The value is hello and the range is applicationpageContent. setAttributesScope ("app2", "hello", PageContext. APPLICATION_SCOPE );

Get variables of different ranges:

// Get the variable named page. The default range is pagepageContent. setAttributesScope ("page"); // you can call this operation to obtain the request2 variable in the range of requestpageContent. setAttributesScope ("request2", PageContext. REQUEST_SCOPE );

  Note: sessions and applications are not listed one by one.

Get the range of variables:

// Get the range of the variable named page pageContext. getAttributesScope ("page ");

Response value:

1: page 2: request 3: session 4: application

 

Request object: <url>

  For the differences between post requests and get requests, refer:Http://kimmking.github.io/2017/12/01/comparing-get-and-post/

Post request case:

Form. jsp (sender)

<Form id = "f1" method = "post" action = "request. jsp "> enter a number: <input type =" text "value =" "name =" number "id =" number "/> </form>

Request. jsp (receiver)

<% // Obtain the names of all request headers Enumeration <String> headerNames = request. getHeaderNames (); while (headerNames. hasMoreElements () {// retrieve request names one by one String headerName = headerNames. nextElement (); // obtain and print the value of the corresponding request name out. println (headerName + request. getHeader (headerName) + "<br/>");} // sets the decoding method. for Chinese, GBK is used to decode the request. setCharacterEncoding ("GBK"); // obtain and print from form. string num = request. getParameter ("number ");
// Request Parameters for obtaining multiple values:
// String [] mVal = request. getParameter ("mValues"); out. println ("number:" + num + "<br>"); %>

    Note: the sending and receiving of get requests and post requests are similar.

Reponse object:

Response generation non-character response instance:

Generate an image and generate a non-character response (case from lightweight java ee Enterprise Application Practice)

<% -- Created by IntelliJ IDEA. user: macrazds Date: 18-3-11 Time: PM To change this template use File | Settings | File Templates. -- %> <% @ page contentType = "image/png" language = "java" %> <% @ page import = "java. awt. image. *, javax. imageio. *, java. io. *, java. awt. * "%> <% // create BufferedImage object BufferedImage image = new BufferedImage (340,160, BufferedImage. TYPE_INT_RGB); // obtain the Graphics object Graphics g = image using the image object. getGraphics (); // drawing (the image is stored in the image object): g. fillRect (400,400,); // set the same color as g. setColor (new Color (255, 0, 0); // draw the arc of the same g. fillArc (20, 20, 100,100, 30,120); g. setColor (new Color (0,255, 0); g. fillArc (20, 20, 100,100,150,120); g. setColor (new Color (0, 0, 255); g. fillArc (20, 20, 100,100,270,120); g. setColor (new Color (, 0); // set the text font g. setFont (new Font ("Arial Black", Font. PLAIN, 16); // draw the string g. drawString ("red: climb", 200,60); g. drawString ("green: swim", 200,100); g. drawString ("blue: jump", 200,140); g. dispose (); // After the image is painted, the g object dispose // The image is output to the page in response to ImageIO. write (image, "png", response. getOutputStream (); %>

Then you can directly call the img tag on other pages to display the image:

 

  Note: In this example, img. jsp cannot be displayed in chrome. The reason is unclear, but it can be displayed normally in firefox or IE.

Redirection:

After entering the current page, redirect (automatic jump) to another page without retaining the attributes and all request parameters that are passed to the original page request range.

// Redirect to index. jsp <% response. sendRedirect ("index. jsp"); %>

Add Cookie:

Cookie: It is often used for websites to record customer information, such as user names.

Write a Cookie for username:

<% // First obtain the request parameter String name = request. getParameter ("name"); // create a Cookie object named username with the obtained name as the value. Cookie c = new Cookie ("username", name ); // set the Cookie lifetime to 3600 seconds. setMaxAge (3600); // Add the new Cookie Object c response to the client. addCookie (c); %>

Obtain the specified Cookie from the client:

<% // Obtain all Cookie cookies reserved on the local machine [] cookies = new request. getCookies (); // retrieve the obtained Cookies for (Cookie c: cookies) {// the condition is that the Cookie object name matches) if (c. getName (). equals ("username") out. println (c. getValue () ;}%>

It is worth mentioning that if the Cookie content is in Chinese, you can use the following command:

Java.net. URLEncoder. encode ("Chinese content", "gbk"); // The returned value belongs to the String type and can be directly used as the java.net parameter of the Cookie constructor. URLDecoder. decode (cookie. getValues (); // if the content is Chinese, and the Cookie object of the Chinese content has been correctly created through the preceding command, this can be directly put out. println (String str); directly displayed in

 

Session object:

This object indicates a session between the browser and the server, that is, the browser connects to the server and starts until it is disconnected from the server. It is often used for user login to the system. In addition, attributes within the range can be shared among multiple pages (from the same server.

Common Methods:

// Set a session attribute session. setAttribute (String attName, Object attValue); // obtain a session attribute value session. getAttribute (String attName );

 

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.