Felayman -- detail JSP

Source: Internet
Author: User

1. What is JSP?

LJSP

-To put it simply, Java code is embedded in HTML, which consists of static HTML, dedicated JSP tags, and Java code.

-Server-side technology. Automatically converted to servlet in the background

-Only when the modified object is accessed for the first time will it be converted into a servlet and compiled. Create a unique JSP instance and call _ jspInit to complete initialization, then, each user request will create a thread that calls the _ jspService method of the JSP instance. Therefore, multiple concurrent requests may cause multiple threads to call the _ jspService at the same time.

-Tomcat, JSP files under the default directory are converted to generate servlet files and the generated class files after compilation are placed in the install_dir/work/Catalina/localhost/_/org/apache/jsp directory

15 common attributes of the JSP page command

<% @ Pageimport = "java. util. Date" %>

<% @ Page language = "java" contentType = "text/html; charset = UTF-8"

PageEncoding = "UTF-8" %>

The first JSP page

<%

Date date = new Date ();

Out. print (date );

%>

1. include command

In JSP usage, the Include command is often used to Include another page.

When we compile these pages, the current page will be merged with the page specified by the include command.

This is the operating mechanism of JSP include.

2. taglib command

You can use the taglib command to identify the tag library used on the page, such as jstl and struts2.

4.JSP action tag

Action commands in JSP Include, Forward, UseBean, GetProperty, SetProperty, and Plugin.
I. Include commands
A tag indicates a static or dynamic file.
Syntax:

Or



Note:
1. page = "path" is a relative path or a relative path expression.
2. flush = "true" must be set to true. The default value is false.
3, The sub-statement allows you to pass one or more parameters to a dynamic file, or you can use multiple To pass multiple parameters to a dynamic file.
Ii. Forward Command
The tag indicates redirecting a static html/jsp file or a program segment.
Syntax:

Or

......

Note:
1. page = "path" is an expression or a string.
2, Name: Specifies the parameter name and value. The parameter is sent to a dynamic file. The parameter can be one or more values, but the file must be a dynamic file. To pass multiple parameters, you can use multiple Send multiple parameters to a dynamic file.
Iii. UseBean instructions
A tag is used to create a BEAN instance on the JSP page and specify its name and scope.
Syntax:

TypeSpec has the following possibilities:
Class = "className" | class = "className" type = "typeName" | beanName = "beanName" type = "typeName" | type = "typeName" |
Note:
You must use class or type instead of both class and beanName. BeanName indicates the Bean name in the form of "a. B. c ".
Iv. GetProperty command
A tag is used to obtain the BEAN property value, convert it into a string, and insert it to the output page.
Syntax:

Note:
1. in use Before, you must use To create it.
2. unavailable To retrieve an indexed attribute.
3. It can be used with the JavaBeans component But cannot be used with Enterprise Java Bean.
5. SetProperty command
Label indicates the attribute value in Bean.
Syntax:

Among them, prop_expr has the following possible situations:
Property = "*" | property = "propertyName" | property = "propertyName" param = "parameterName" | property = "propertyName" value = "propertyValue"
Note:
Use jsp: setProperty to assign values to the attributes of a Bean. You can use either of the following methods.
1. Use jsp: setProperty: After jsp: useBean:

...

In this way, jsp: setProperty will be executed.
2. jsp: setProperty appears in the jsp: useBean Tag:

...


In this way, jsp: setProperty is executed only when a new object is instantiated.
* In The name value in should be The id value is the same.
Vi. Plugin commands
The tag indicates executing an applet or Bean. If possible, you need to download a Java Plug-in to execute it.
Syntax:
Type = "bean | applet"
Code = "classFileName"
Codebase = "classFileDirectoryName"
[Name = "instanceName"]
[Archive = "URIToArchive,..."]
[Align = "bottom | top | middle | left | right"]
[Height = "displayPixels"]
[Width = "displayPixels"]
[Hspace = "leftRightPixels"]
[Vspace = "topBottomPixels"]
[Jreversion = "Maid number | 1.1"]
[Nspluginurl = "URLToPlugin"]
[Iepluginurl = "URLToPlugin"]>
[
[ ] +
]
[ Text message for user ]

Note:
Elements are used to play or display an object (typically an applet and Bean) in a browser, and such display requires the java Plug-in the browser.
When the Jsp file is compiled and sent to the browser, The element will be replaced OrElement. Note,For HTML 4.0,Used for HTML 3.2.
Generally, The element specifies whether the object is an Applet or Bean. It also specifies the class name and location. It also specifies where to download the Java Plug-in.

Request object

<% @ Page language = "java" contentType = "text/html; charset = UTF-8"

PageEncoding = "UTF-8" %>

Request

<%

Request. setCharacterEncoding ("UTF-8 ");

%>

<% = "Transfer Method:" + request. getMethod () %>

<% = "Character encoding method:" + request. getCharacterEncoding () %>

<% = "Protocol used:" + request. getProtocol () %>

<% = "Client address:" + request. getRequestURI () %>

<% = "Client address:" + request. getRequestURL () %>

<% = "Client IP Address:" + request. getRemoteAddr () %>

<% = "Get server port:" + request. getServerPort () %>

<% = "Get server name:" + request. getServerName () %>

<% = "Get host name:" + request. getRemoteHost () %>

<% = "Obtain the script file path:" + request. getServletPath () %>

<% = "Get protocol header information:" + request. getHeader ("host") %>

<% = "Get protocol header information:" + request. getHeader ("user-agent") %>

<% = "Get protocol header information:" + request. getHeader ("accept-language") %>

Response

<% @ Page language = "java" contentType = "text/html; charset = UTF-8"

PageEncoding = "UTF-8" %>

Response

<%

// Redirect page

Response. sendRedirect ("out. jsp ");

// Disable Cache

Response. setHeader ("Cache-Contral", "no-store ");

// Disable Cache

Response. setHeader ("Expires", "0 ");

// Set automatic page refresh

Response. setHeader ("refresh", "1 ");

// Timed jump page

Response. setHeader ("refresh", "5; URL = out. jsp ");

// Forcibly output the buffer content to the client

Response. flushBuffer ();

// Obtain the buffer size

Response. getBufferSize ();

// Set the buffer size

Response. setBufferSize (1023 );

// Clear the buffer size

Response. reset ();

// Check whether the server has written data to the client

Response. isCommitted ();

%>

Session

1. test1.jsp

<% @ Page language = "java" contentType = "text/html; charset = UTF-8"

PageEncoding = "UTF-8" %>

Session

<%

String name = "felayman ";

Session. setAttribute ("username", name );

%>

3. test. jsp

<% @ Page language = "java" contentType = "text/html; charset = UTF-8"

PageEncoding = "UTF-8" %>

Insert title here

<%

String username = (String) session. getAttribute ("username ");

%>

Session: <% = username %>

PageContext object

<% @ Page language = "java" contentType = "text/html; charset = UTF-8"

PageEncoding = "UTF-8" %>

PageContext

<%

// Jump to the page

PageContext. forward ("out. jsp ");

%>

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.