Basic instructions and actions commonly used in JSP

Source: Internet
Author: User

JSP Basic Instructions

The JSP command directive is used to set properties related to the entire JSP page, and it does not directly produce any visible output, but simply tells the engine how to handle the rest of the JSP pages. Its general grammatical form is: <%@ directive name attribute = "Value"%>

Three command commands are page, include, Taglib, respectively.

One, page directive

Function: Sets the static properties of the entire JSP Web page.

Syntax: <%@ page Tag element = "value"%>, such as <%@ page language= "java"%>

Tag elements: Language, import, ContentType, Session, ErrorPage, Iserrorpage, and so on.

1, language

Language= "Language" specifies what language the JSP container will use to compile the JSP Web page. Currently, only the Java language can be used, but no additional languages are excluded. The default value is java. For example <%@ page language= "java"%>

2. Import

import= "Importlist" defines which Java APIs can be used by this JSP page. A comma-delimited list of one or more full-quality class names. This list is used to create the appropriate import statements in the generated Java servlet. The following packages are automatically included and do not need to be pointed out: java.lang.*;java.servlet.*;java.servlet.jsp.*;java.servlet.htt.* such as <%@ page import= "Java.util. * "%>

3, ContentType

Contenttype= "Ctinfo" means that the MIME type and optional characters that will be used in the build servlet are decoded. For example <%@ page contenttype= "TEXT/HTML;CHARSET=GBK"%>

4. Session

Session= "True|false" indicates whether the JSP page requires an HTTP session, and if true, the resulting servlet will contain code that creates an HTTP session (or accesses an HTTP session), which defaults to true.

5, ErrorPage

Errorpage= "Error_url" means that if an exception occurs, the page will be re-directed to a URL page. The error page must specify Iserrorpage= "true" in its page directive element

6, Iserrorpage

Iserrorpage= "True|false" is true if this page is used as a page to handle exception errors. In this case, the page can be specified as the value of the ErrorPage property in the page directive element of the other pages. Specifying this property to True causes the exception implied variable to be available for this page. The default value is False.

XXXXX Instance 1, showing the current system design xxxx

1. Enter the following code in Notepad:

<%@ page language= "java" contenttype= "TEXT/HTML;CHARSET=GBK"%>

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

<%

Date Dnow = new Date ();

int dhours = Dnow.gethours ();

int dminutes = Dnow.getminutes ();

int dseconds = Dnow.getseconds ();

Out.print ("Server time:" + dhours + ":" + dminutes + ":" + dseconds);

%>

<script language= "JavaScript" >

var dnow = new Date ();

Dhours = Dnow.gethours ();

Dminutes = Dnow.getminutes ();

Dseconds = Dnow.getseconds ();

document.write ("<br> Browser time:" + dhours + ":" + dminutes + ":" + dseconds);

</SCRIPT>

2. Save As page.jsp, create a new 02 folder under the WebApps directory of the Tomcat installation directory, and create a new 1 folder under 02.

Put the page.jsp under the 1 folder.

3. Enter the following code in Notepad:

<?xml version= "1.0" encoding= "UTF-8"?>

<web-app id= "Webapp_9" version= "2.4" xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"

Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"

xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-

App_2_4.xsd ">

</web-app>

4, Save as Web. XML, create a new Web-inf folder under the 02 folder, and Place Web. XML under the Web-inf folder.

5, enter the input http://localhost:8989/02/1/page.jsp in the browser to open the Page.jsp page.

6, <script language= "JavaScript" >...</SCRIPT> This code, is the foreground script code, used to compare the front, background code

Do not need to run the engine to compile the condition. In fact, you can delete <script language= "JavaScript" >...</SCRIPT> this piece of code.

Ii. include directives

Features: the include directive is used to insert the contents of a static file into the current page. This file can be a JSP, HTML, text, or Java program.

Syntax: <%@ include file= "filename"%> such as <%@ include file= "111.txt"%>

Tag element: File

Note: the include directive element and the behavior element mainly have two different points.

The 1.include instruction is static, execution time is executed in the compile phase, and the content is introduced as static text, which is fused with the inclusion when it is translated into a servlet. So file cannot be a variable, nor can it take any arguments after file.

The 2.include behavior is dynamically included, execution time is executed during the request phase, and the introduced content is dynamically generated and then included in the page when it is requested when the page is executed.

3, <%@ include file= "filename"%> if you start with a file name, referring to the path of the JSP file you are using,

If/start, refers to the path relationship that is up and down the JSP file being used.

XXXXX Instance 2, statically containing external resource file xxxx

1. Enter the following code in 111.txt:

Include file start here.<br>

<%! String str= "Here is include ' s context!"; %>

<% out.println (str+ "<br>");%>

In fact, in 111.txt input the following code, the effect is the same, but the Chinese will appear garbled

Include file start here.<br>

Here is include ' s context!<br>

2, save 111.txt in the Webapps\02\2 directory.

3. Writing native.jsp files

<body>

Native file Start here.<br>

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

Native file End here.<br>

</body>

4, Save the native.jsp in the webapps\02\2 directory.

Enter the input http://localhost:8989/02/2/native.jsp in the browser.

Iii. taglib directive

Features: Use the tag library to define new custom tags and enable custom behaviors in JSP pages.

When a page references a user-defined label, the taglib directive is used to reference the custom tag library and to specify a prefix for the label.

Syntax: <%@ taglib uri= "uritotaglibrary" prefix= "TagPrefix"%>

such as <%@ taglib uri= "http://www.jspcentral.com/tags" prefix= "JAXP"%>

Tag elements: URI, attributes of the prefixpage Directive element

Uri= the URI of the "Taglibraryuri" tag library descriptor, which is mainly said to be the taglibrary storage location.

Prefix= "TagPrefix" is used to identify a unique prefix that uses custom tags in the later part of the page.

The prefix is used in front of the name of the label, for example, in public in front of the loop. Empty prefixes will be ignored. If you are developing or using custom tags, you cannot use tag prefixes: Jsp,jspx,java,javax,servlet,sun and SUNW, as they have been used by Sun's systems.

Basic JSP Action

The action elements in the JSP include: include, Forward, Usebean, GetProperty, setProperty, plugin, param.
1.include Action
<jsp:include> tags are used to include a static or dynamic file.
2.forward Action
<jsp:forward> tags are used to redirect a static HTML file, a JSP file, or a program segment.
3.useBean Action
The <jsp:useBean> tag is used to create a bean instance in a JSP page and specify its name and scope.
4.getProperty Action
The <jsp:getProperty> tag is used to get the Bean's property value (which must be created before <jsp:useBean>), convert it to a string, and then insert it into the output page.
5.setProperty Action
The <jsp:setProperty> tag indicates the value of the attribute used to set the bean.
6.plugin Action
<jsp:plugin> tags are used to play or display an object in the browser (typically applets and beans), which requires the Java plugin to be installed on the browser. When the JSP file is compiled and sent to the browser, the,<jsp:plugin> element will be replaced with a <object> or <embed> element based on the browser's version.
7.param Action
<jsp:param> tags are used to pass parameters and must be used in conjunction with forward, include, and plugin actions.

Basic instructions and actions commonly used in 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.