JSP syntax nine large built-in objects and four scopes

Source: Internet
Author: User
Tags throwable

A Jsp

Principle: JSP is actually a servlet.

The servlet is responsible for business logic processing and the JSP is only responsible for display. In development, there cannot be a single line of Java code in the JSP

Two JSP Syntax

1. JSP template elements: HTML tags and text in JSP

2. Script: Write Java code, <%%>

3. Expression: For output variables and expressions, <%=%>

4. Note: Divided into three types

A) <!---->: The JSP will be translated when translated into a servlet. Also available on the page.

b) <%/*.......*/%>:jsp will be translated when translated into a servlet, but not in the page (right-click Source code).

c) <%--..........--%>:jsp will not be translated when translated into a servlet.

Three Instructions

1. Page: This command can be placed anywhere in the JSP page. Scoped to the entire JSP page

Property:

Language: Java is the default value, and currently only Java

Extends:jsp the parent class of the corresponding servlet

Import: Imports the packages, classes used in this JSP page. Multiple packages are separated by commas, and the default imported packages are: java.lang.*,javax.servlet.*,javax.servlet.http.*,javax.servlet.jsp.*

Session: Whether to create a HttpSession object, the default is True

Buffer: Set cache, Value none|8kb|sizekb, default 8kb. is typically the default value.

AutoFlush: Auto Refresh, True|false, default true.

isthreadsafe:jsp whether the corresponding servlet implements the Singletreadmode interface. The default is true, False is implementation. Implementing a single Thread

ErrorPage: Setting error page

Configuring the default error page in Web. xml

<error-page>

<exception-type>java.lang.Exception</exception-type>

<location>/exce.jsp</location>

</error-page>

<error-page>

<error-code>404</error-code>

<location>/404.jsp</location>

</error-page>

Iserrorpage: The default is Flase, which indicates whether to generate a reference exception for Throwable objects.

ContentType: Sets the encoding of MIME types in HTML, as in Servlet response.setcontenttype ();

Pageencoding: Tells the engine which encoding to use when translating JSPs.

Isellgnored: Whether to ignore El expressions, JSP2.0, false by default

Four Include

Contains:

1. Dynamic inclusion

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

If there are two JSP files, a. A dynamic contains B,a and B are translated into A_jsp.class and B_jsp.class files respectively, then the container merges two servlets. Output again.

2. Static inclusions

<%@ include file= "..."%>

A and B are translated into a a_jsp.class file. have been merged and given to the container. Output again.

2. Taglib:

<% taglib uri= "Http://java.sun.com/jsp/jstl/core" prefix= "C"%>

Five Nine large implicit objects

Request:httpservletrequest

Response:httpservletrequest

Session:httpsession (only if the session is set to true in the page directive)

Config:servletconfig

Application:servletcontext

Exception:throwable (will only be available if Iserrorpage is set to true in the page directive)

Page: Reference to the current servlet

Out:jspwriter (equivalent to PrintWriter)

PageContext:javax.servlet.jsp.PageContext;

Role:

A. You can get the other 8 large implicit objects

B. itself is also a domain object, scoped to this page

Provides methods for manipulating other domain objects (servletcontext,httpsession, ServletRequest)

To manipulate the own domain object:

Pagecontext.getattribute (Stringname);

Pagecontext.setattribute (stringname,object value);

Pagecontext.removeattribute (Stringname);

Ways to manipulate other domain objects:

Object getattribute (stringname,int scope)

Scope:pagecontext has the corresponding constant

Pagecontext.application_scope

Pagecontext.session_scope

Pagecontext.request_scope

Pagecontext.page_scope

Voidremoveattribute (String name,int scope)

Voidsetattribute (String name,object value,int scope)

When we only know that there is a name such a parameter, but do not know in which domain. Can be used:

Pagecontext.findattribute (Stringname);

The order of lookups from small to large,Page->request->session->context

C. Methods of inclusion and forwarding are provided

Pagecontext.forward (String path);

Pagecontext.include (String Path)

Six Four domain objects

PageContext: Valid on this page

Request: Valid in a single request and forwarded servlet

Session: Valid in one conversation, until timeout and destruction

Application: Effective throughout the web app, with a lifetime of Web runtime

Principle: The smaller the domain object, the better.

Seven JSP built-in tags (action elements)

Jsp:include: Dynamic Inclusion

Jsp:forward: Request Forwarding

Jsp:param: Passing Request parameters

Eight JavaBean

1. The construction method with no reference

2. Private fields (field names start with lowercase)

3. Provide public getter and setter methods.

4. Java.io.Serialiable (serializable) interface is generally implemented.

Role: for encapsulating data

Nine Action elements of the JavaBean

1, Jsp:usebean

Function: Finds an object of the specified name from the specified domain scope. A direct return was found; the instance that created the object was not found and placed in the specified scope.

Scope: Page Request sessionapplication

Property:

ID: The name of the reference object

Class: Type of Lookup

Scope: the specified range. The default value is page

2, Jsp:getproperty

Gets the value of the specified property for the specified JavaBean and prints it to the page. If the property value is NULL, print null.

Property:

Name of the Name:javabean

Property: The Getter attribute is manipulated

3, Jsp:setproperty

Action: Sets the value of a property of JavaBean.

Property:

Property: Attribute name. Wildcard characters supported *

Name of the Name:javabean

Value: Specify value directly

PARAM: Specifies the name of the request parameter.

Ten The development model of Javaweb

1. Model 1:jsp+javabean

Features: Ideal for developing very simple applications.

2. Model 2:jsp+servlet+javabean (MVC)

M: Model JavaBean

V: View JSP

C: Controller Servlet

JSP syntax nine large built-in objects and four scopes

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.