JSP Classic Learning notes (including various introductory common syntax) _JSP programming

Source: Internet
Author: User
Tags abstract flush

This article introduces the JSP Classic learning notes. Share to everyone for your reference. Specifically as follows:

JSP is a special form of the servlet, each JSP page is a servlet instance--jsp page has the system compiled into Servlet,servlet and then responsible for responding to user requests.

1.JSP notes

<%--annotation Content--%>, unlike the HTML annotation <!--annotation content-->, the compiled HTML page cannot look up the JSP annotation content.

2.JSP statement

JSP declarations are used to declare variables and methods. The JSP declaration will be converted to a member variable or member method in the servlet, so the JSP declaration still conforms to the Java syntax.

Format: <%! Declaration Part%>

<%!
Declares an integer variable public
int count;
Declares a method public
String info () {return
 ' Hello World ';
}
Out.println (info ());
%>

Because the variables and methods defined by the JSP declaration syntax correspond to the member variables and methods of the servlet, the variables and methods defined in the JSP Declaration section can be decorated with an access controller such as Private,public, or you can use static adornments to turn them into class properties and class methods. However, you cannot use the abstract to modify the declarative part of the method, because the abstract method will cause the JSP corresponding servlet to become an abstract class, resulting in the inability to instantiate.

3. Output JSP expression

Syntax format: <%= expression%>

<%!
public int count;
Public String info () {return
 ' Hello World ';
}
%>
<!--use expression output variable value-->
<%=count++%>
<!--return a value using an expression output method-->
<%=info ()%>

Note: You cannot have a semicolon after an output expression syntax.

4.JSP Script

Control page static content (HTML and other elements) through JSP scripting code

<% for
(int i=0;i<10;i++) {
%>
<tr><td><%=i%></td></tr>
<%}%>

3 Compiler instructions for 5.JSP

A JSP compilation instruction is a message that notifies the JSP engine that it does not generate output directly. The compilation directives all have default values.

Page: This instruction is for the current page.

Include: Lets you specify that another page is included.

Taglib: Used to define and access custom labels.

Syntax format: <%@ compiler directive Name Property name = "Property value" ...%>

5.1.page instruction

Page directive attributes are: Language,extends,import,session,buffer,autoflush,info,errorpage,iserrorpage,contenttype

<%@ page contenttype= "text/html; charset=gb2312 "Language=" Java%>
<% page import= "java.sql.*"%>

5.2.include instruction

With the include directive, you can embed an external file into the current JSP file, parsing the JSP statement (if any) on the page. This is a static include statement that will include other compilation instructions for the target page, but dynamic include does not.

Include can contain either static text or dynamic JSP pages. The static include compilation instruction adds the included page to the page and blends it into a single page.

Include instruction syntax: <% @include file= "Relativeurlspec"%>

If the embedded file needs to change frequently, it is recommended to use the <jsp:include> Action directive because it is a dynamic include statement.

Static inclusion meaning: the containing page will fully contain the code that contains the page at compile time.

7 Action Instructions for 6.JSP

An action instruction differs from a compilation instruction in that it notifies the servlet engine of the processing message and the script action of the action instruction knowledge when it is run. Compilation directives work when you compile the JSP into a servlet, and the action instructions are usually replaced with JSP scripts, which are standardized for JSP scripts.

Jsp:forward: Performs a page shift and forwards the requested processing to the next page.

Jsp:param: For passing parameters, it must be used with tags that support other parameters.

Jsp:include: For dynamically introducing a JSP page.

Jsp:plugin: For downloading JavaBean or applets to client execution.

Jsp:usebean: Create an instance of JavaBean.

Jsp:setproperty: Sets the property value of the JavaBean instance.

Jsp:getproperty: Outputs the property values of the JavaBean instance.

6.1.forward instruction

The forward directive is used to forward a page response to another page. It can be forwarded to a static HTML page, or to a dynamic JSP page, or to a container's servlet.

Forward instruction syntax format for JSP:

<jsp:forward page= "{relativeurl|<%=expression%>}" >
 {<jsp:param.../>}
</jsp:forward >

The value of the request parameter can be obtained by the GetParameter () method of the HttpServletRequest class.

<jsp:forward page= "forward-result.jsp" >
 <jsp:param name= "age" value= ""/>
</jsp:forward >

The corresponding forward-result.jsp page can get the age parameter value through the request built-in object.

<%=request.getparameter ("Age")%>

When the forward instruction is executed, forwarding is not redirected, so the requested address is not changed, and the requested parameter is not lost. But the content of the page is completely changed to the content of the forward target page.

6.2.include instruction

The include directive is a dynamic include directive that is also used to import a page that does not import the compiled instruction of the included page, but inserts the body content of the imported page into this page.

Syntax format for include action directives:

Copy Code code as follows:
<jsp:include page= "{relativeurl|<%=expression%>}" flush= "true"/>

Or

<jsp:include page= "{relativeurl|<%=expression%>}" flush= "true" >
 <jsp:param name= "ParameterName "Value=" Patametervalue "/>
</jsp:include>

The Flush property is used to specify whether the output cache is transferred to the imported file. If specified as true, is included in the file being imported, or false if specified, and included in the original file. Example: <jsp:include page= "scriptlet.jsp"/>

Static import and dynamic import differences:

A. Static import is the full integration of the code that is imported into the page, two pages into a whole servlet; Dynamic import uses the Include method in the servlet to introduce the content of the page being imported.

B. The compilation instructions for the page being imported when the static import works, while the compilation instructions for the page being imported when dynamically imported are not working, just inserting the body content of the page being imported.

In addition, additional request parameters can be added when executing include dynamic directives.

In fact, the forward action instruction is similar to the include action instruction, which uses methods to introduce the target page, which can be drawn by viewing the servlet code generated by the JSP page: Forward instruction uses _jspx_page_context forward () method to introduce the target page, and the include directive uses the Jspruntimelibrary include () method to introduce the target page. The difference is that when the forward is executed, the page being forward will completely replace the original page, and the Include page simply introduces the original page when the include is executed. In short: Forward takes the target page instead of the original page, and the include inserts the target page into the original page.

6.3.usebean,setproperty,getproperty directives

These 3 directives are javabean-related directives in which the usebean instruction Initializes a Java instance in the JSP page; The setproperty directive is used to set the value for the property of the JavaBean instance. The GetProperty directive is used to output JavaBean instance properties.

Usebean syntax format: <jsp:usebean id= "name" class= "classname" scope= "Page|request|session|application"/>

Where the id attribute is the instance name of the JavaBean, the class attribute determines the implementation class of the JavaBean. The scope property is used to specify the scope of the JavaBean instance's existence.

SetProperty instruction Syntax format: <jsp:setproperty name= "Beanname" property= "PropertyName" value= "value"/>

GetProperty instruction Syntax format: <jsp:getproperty name= "Beanname" property= "PropertyName"/>

Cases:

<!--Create an instance of the Com.company.Person instance fame P1--> <jsp:usebean id= "P1" class= "Com.company.Person"
Page/>
<!--set P1 Name property value--> <jsp:setproperty name= "P1"
property= "name" value= "Zhangsan"/>
<jsp:setproperty name= "P1" property= "Age" value= "a"/>
<!--output P1 's Name property value--> <jsp
: GetProperty name= "P1" Property name= "name"/>

9 built-in objects in a 7.JSP script

Application,config,exception,out,page,pagecontext,request,response,session

An instance of Application:javax.servlet.ServletContext that represents the Web application that the JSP belongs to, and can be used for JSP pages, or for exchanging information between the servlet. Common method: Getattribute,setattribute,getinitparameter.

An instance of Config:javax.servlet.ServletConfig that represents the configuration information for the JSP. Commonly used methods are getinitparamter,getinitparameternames, in fact, JSP pages usually do not need to configure, there is no configuration information. Therefore, the object is more effective in the servlet.

An instance of exception:Java.lang.Throwable that represents exceptions and errors in other pages. This object is available only if the page is an error-handling page, that is, the Iserrorpage property of the compile instruction page is true. The common methods are getmessage () and Printstacktrace ().

An instance of Out:javax.servlet.jsp.JspWriter that represents the output stream of a JSP page, used to output content, and forms an HTML page.

Page: It's usually not very useful to represent the pages themselves. This in the servlet.

An instance of PageContext:javax.servlet.jsp.PageContext that represents the JSP page context that can be used to access shared data on a page. Common methods include Getservletcontext () and Getservletconfig ().

An instance of Request:javax.servlet.http.HttpServletRequest that encapsulates a request. The client's request parameters are encapsulated in the object. is a common object that must be used to obtain client request parameters. Common methods are getparameter,getparametervalues,setattribute,getattribute,setcharacterencoding.

A Response:javax.servlet.http.HttpServletResponse instance that represents the server-side response to the client. This object is often used in a direct response, but instead of an out object, unless a non character response is required. and response objects are often used for redirection, commonly used methods: Getoutputstream (), Sendredirect.

An instance of the Session:javax.servlet.http.HttpSession that represents a session. Common method: Getattribute,setattribute.

8.JSTL JSP standard Tag Library

JSP's tag function library is divided into five major categories:

Jstl
Predecessor name
Uri
Example
Core Tag Library
C
Http://java.sun.com/jsp/jstl/core
<c:out>
i18n Format Tag Library
Fmt
Http://java.sun.com/jsp/jstl/fmt
<fmt:formatDate>
SQL Tag Library
Sql
Http://java.sun.com/jsp/jstl/sql
<sql:query>
XML Tag Library
Xml
Http://java.sun.com/jsp/jstl/xml
<x:forEach>
Function Tag Library
Fn
Http://java.sun.com/jsp/jstl/functions
<fn:split>

Core Tag Library

Functional classification

Label name

Expression action

out , Set , Remove , Catch

Process Control

if , Choose , when , otherwise

Iterative operations

ForEach , Fortokens

URL Operation

Import , param , URL , redirect


Use:

Put all the. Jar Packages (internally all label processors) in the tag library under/web_inf/lib,

Put all. TLD (inside all the tag library descriptions) under/web-inf/tlds

<%@ page contenttype= "TEXT/HTML;CHARSET=GBK" language= "java"%>
<%@ taglib uri= "http://java.sun.com/" Jstl/core "prefix=" C "%>
<c:out value=" ${param.name} "default=" Welecome jgl to my website! " />

9.EL expression

${sessionscope.user.sex}

All El starts with ${and ends with}.

EL offers. and [] Two operators to navigate the data. The following two represent the same meaning:

${sessionscope.user.sex} equals ${sessionscope.user["Sex"]}

. And [] can also be mixed at the same time, as follows:

${sessionscope.shoppingcart[0].price}

I hope this article will help you with the JSP program design.

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.