JSP basic knowledge point (transfer wisdom)

Source: Internet
Author: User

First, JSP overview
1. Jsp:java Server Pages (running on the server side of the page). is the servlet.
Learning JSP Learn the key: Always associate to the servlet.
2, the principle of JSP
The JSP will be translated into a servlet by Tomcat. The servlet is in the tomcat\work\catalina\locahot\day09\ ....
3, JSP, servlet development Best Practices:
Servlet: General As a control component, handling business logic
JSP: General As a display component, displaying data

Second, the syntax of the JSP
1, JSP template elements: (first write HTML)
is the HTML tags in the JSP
Role: page layout and landscaping
2. JSP java Script expression:
Function: Output data to the page
Syntax: <%= expression%> (actually, the call output stream is printed on the page)
3. Java script fragment in JSP: (in real development, you should do not have a line of Java script fragments in the JSP)
Role: Writing Java code logic
Syntax: <%
Statement 1;
Statement 2;
%>
Principle: The statement is translated into the service method of the corresponding servlet by the server.
4, the JSP declaration: (Understand its principle)
Role: Define members of a class
Syntax: <%!
Your Java code
%>
5, the JSP comments:
Role: Comment Java script code
Syntax: <%--This is a comment--%>

Third, the JSP directive: for the JSP engine (server)
Basic syntax format: <%@ directive name Attribute 1 = "Value 1" Property 2 = "Value 2" ....%>
Role: Tells the server how to handle content other than instructions in the JSP.

3.1page
Role: Define various properties of a JSP page
Property:
Language: Instructs the JSP page to use the scripting language. Java is the default value, and currently only supports Java.
Extends: Indicates the parent class of the servlet class that the JSP corresponds to. Do not modify.
*import: Imports the class or package used by the Java script in the JSP. (like import statements in Java)
The JSP engine automatically imports the classes in the following packages:
javax.servlet.*
javax.servlet.http.*
javax.servlet.jsp.*
Note: An Import property can import multiple packages, separated by commas.
*sessioin: Indicates whether the JSP page creates a HttpSession object. The default value is true, creating
*buffer: Indicates the cache size of the output stream used by the JSP. The default value is 8Kb.
AutoFlush: Automatically refreshes the cache of the output stream.
IsThreadSafe: Indicates whether the page is thread-safe (obsolete). The default is true.
true: Not secure.
False: safe. Indicates that the servlet corresponding to the JSP implements the Singlethreadmodel interface.
*errorpage: Indicates the page to be turned (forwarded) after the current page error.
The target page, if "/" (currently applied), is the absolute path.

To configure the global Error prompt page:
Xml
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/404.jsp</location>
</error-page>
*iserrorpage: Indicates whether the current page produces a exception object.
*contenttype: Specifies the MIME type of the current page. The effect is exactly the same as the Response.setcontenttype () function in the servlet
*pageencoding: The encoding used by the notification engine to read the JSP (because it is being translated)
There is also the role of the ContentType attribute.
*iselignored: Whether to ignore the El expression. ${1+1}. The default value is False.

The simplest way to use the page directive: <%@ page pageencoding= "UTF-8"%>
3.2include (static included, can be used in the development of static without moving)
Function: Contains other components.
Syntax: <% @include file= ""%>file specifies the target component to include. The path if the "/" (currently applied) is the absolute path.
Principle: The content of the target component is added to the source component, and the result is output.

Dynamic includes:
Take action element: <jsp:include page= ""/> Path If "/" (currently applied) is the absolute path.


3.3taglib
Role: Introduction of external labels
Syntax: <% @taglib uri= "tag namespace" prefix= "prefix"%>
<%@ taglib uri= "Http://java.sun.com/jsp/jstl/core" prefix= "C"%>

Iv. built-in objects for JSP (9 JSP built-in objects)
JSP's built-in object reference name corresponds to the type

Request HttpServletRequest
Response HttpServletResponse
Session HttpSession (with switch: The value of the session property of the page instruction)
Application ServletContext
Config ServletConfig
Page this (current Servlet object)
Exception java.lang.Throwable (switched: The Iserrorpage property of the page directive is changed to true)

Out JspWriter
PageContext Javax.servlet.jsp.PageContext is very important.
There are three major functions:
1, itself is a domain object. You can also manipulate three other domain objects (pagecontext servletrequest HttpSession ServletContext)
The domain range represented by itself is this page.
void SetAttribute (String name,object value)
void RemoveAttribute (String name)
Object getattribute (String name)
Manipulate the other three domain objects
void SetAttribute (String name,object value,int scope)
void RemoveAttribute (String name,int scope)
Object getattribute (String name,int scope)

The parameter int scope is defined by the static variables provided by the PageContext class.
Pagecontext.page_scope: Page range (the map in the PageContext itself, code page)
Pagecontext.request_scope: Range of requests (the map in ServletRequest, code request)
Pagecontext.session_scope: Request scope (is the map in HttpSession, code SESSION)
Pagecontext.application_scope: Request range (the map in ServletContext, code application)


(very useful) object Findattribute (String name): Searches for objects of the specified name, followed by the page request session application scope, until it is found.

2. Get the other 8 implicit objects
3, provide a convenient way to forward and include
RequestDispatcher rd = request.getrequestdispatcher ("/url");
Rd.forward (Request,response);

Pagecontext.forward ("url");
Pagecontext.include ("url");

Five or four large domain objects (data transfer between two resources)
The name range name of an implicit object in a JSP is of a specific type
PageContext page Javax.servlet.jsp.PageContext
Request Request Javax.servlet.ServletRequest (it is useless to display the data)
Session Session Javax.servlet.http.HttpSession (show the data, after a while you need to use)
Application Application Javax.servlet.ServletContext (display the data, we all need to use.) Not recommended, if used, must be processed synchronously)
Vi. common action elements of JSP
<jsp:include/>
<jsp:forward/>
<jsp:param/&gt: Use this tag to pass request parameters when included and forwarded
Six, how to debug the JSP error occurred

JSP basic knowledge point (transfer wisdom)

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.