Java Web-based JSP summary and Java Web JSP Summary

Source: Internet
Author: User

Java Web-based JSP summary and Java Web JSP Summary
1. Overview

A JSP file is a file formed by combining the three commands, nine objects and JSP expressions with HTML statements. Its essence isServlet.Java code + HTML statements.

2. Operating Principle

  When each JSP page is accessed, the JSP Engine translates it into a Servlet Source program, which is then compiled into a Servlet class file. The subsequent execution process is consistent with that of the common Servlet. The Servlet engine loads and translates class files for execution.

3. Nine objects

JSP has nine built-in objects and can be used without declaration.

(1)Request: HTTPServletRequest.

(2) response: it is actually HTTPServletResponse.

(3)PageContext: Indicates the current JSP page, which contains almost the information of the current JSP page. You can also obtain the other eight objects from this object.

(4)Session: Indicates a session between the browser and the server. It is an object of HttpSession.

(5)Out: You can directly use this object to output text or HTML code on the page.

(6)Application: Indicates the current WEB application, which is a ServletContext object.

(7)Exception: After declaring<% @ Page isErrorPage = "true" %>You can use this object to output exception information.

(8) config: The ServletConfig object of the Servlet corresponding to the current JSP. To access the configuration parameters of the current JSP, You need to map the access.

(9) page: refers to the reference of the Servlet object corresponding to the current Jsp, but is of the object type.

The four most common and important objects are as follows:

PageContext: the scope isCurrent JSP page. The variables in the file introduced by <% @ include file = "" %> can also be accessed on the current page. However, through pageContext. include ("file") the variables and parameters of the file cannot be directly used and modified on the current page. The essence of this method is to add the content of the file to the current page intact, it can be understood that the HTML page is directly spliced in this location.

Request: takes effect inRequest forwardingIs always valid.

Session: the scope is browser and ServerOne session. It is intuitively manifested in the opening and closing of a browser. Use cases such as the implementation of shopping cart on a shopping website.

Application: the current application is valid when it is loaded on the server. This object will be invalidated if the application is detached from or reloaded from the server, and the server is closed. Use Cases such as website statistics on the number of visitors.

Attribute-related methods:

   Object getAttribute (String name) to get the specified attribute

    Enumerration getAttributeNames () obtains the Enumeration object composed of all attributes.

   RemoveAttribute (String name) removes the specified attribute

    SetAttribute (String name, Object o)

4. Three major commands

(1) page: Declares the page information or the imported content.

① Import the jar package required by the file. External jar package is generally stored in the Web project WEB-INF \ lib,

<%@ page import="java.util.*"%>

② Close the session, which is enabled by default.

<%@ pagesession="false" %>

③ If an error occurs on the current page, the "file" page is displayed. The redirection method is request forwarding.

<%@ page errorPage="file"%>

④ On the above "file" page, you can handle or describe page errors and use them with exception objects.

<%@ page isErrorPage="true"%>

⑤ Character encoding of the current JSP page (JSP file encoding)

<%@ page pageEncoding="utf-8"%>

⑥ Response type of the current JSP page (Content Encoding sent by the server to the browser)

<%@ page contentType="text/html; charset=utf-8"%>

7. whether to use the EL expression, usually false.

<%@ page isELInored="false" %>

The response specifies the information of the JSP page. You can use the getServletInfo () method to obtain the string.

<%@page info="this JSP info" %>

(2) include command: include other files and merge them with the current page. Such files usually include files in text, JSP, or other formats.

① Static Introduction: the JSP Engine is notified to merge other source files into the Servlet source files converted to the current JSP page when translating JSP pages. The two source files are merged into a Servlet Source file. "File" is the relative path.

<%@ include page="file"%>

② Dynamic Introduction: when the sentence is executed, the program passesRequest forwardingJump to "file", execute the content, and then return the remaining part of the execution current page, actually two Servlet Source files.

<jsp:include page="file"></jsp:include>

(3) taglib: allows users to customize new labels as needed to facilitate the implementation of functions, but this also leads to low code accessibility.

5. Others

(1) JSP expression: You can directly display the content in the expression in the browser.

<%= "Hello World!" %>

(2) declare a function

<%!     pulic void function(){      out.println("Hello World!");    }%>

(3) Comment

   

<! -- Client annotation, which can be viewed from the client source -->
<% -- Server comment, client cannot view -- %>

 

 

 

 

 

  

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.