The first experience of JSP learning

Source: Internet
Author: User

Introduction to JSP:

1) Jsp--java Server Pages

2) features and benefits of servlet (itself a servlet)

3) Embed JSP code directly in HEML

4) The JSP program is converted by JSP engine into a servlet code, which is then compiled into a class file loaded into the execution

5) Only when the client requests the JSP for the first time, does it need to convert, compile

Advantages of JSP:

1) Excellent performance

Better than CGI, PHP, ASP

2) Platform Independence

OS independent, Web server agnostic

3) Scalability

Tag extension mechanism to simplify page development

JSP programming--Basic syntax:

JSP traditional Syntax:

1) Declaration:

basic syntax:<%!  %> Description: The variables and methods declared here will be kept as a unique copy until the JSP program stops executing. Variables declared in this section are converted to member variables of the servlet, and functions can only be declared here. Example:<%! int i;  Public void setName () {...} %>

"Note": <%%> the variables and methods declared in this form are local, such as <% int count1=0%>

<%= xxx%>: output XXX content as-is
2) Scriptlet:

basic Syntax:<% program code area%> can be put into any Java program code, this part declares the variable is a local variable, in the service () Method Example:<% for ( int i=0;i<10,i++) {...} %>

3) Note format:

1) <%--...  ...--%>2) <%//...  ...%>3) <%/*...  ... */%>

4) Expression:

basic syntax:<%=.%>= The following must be a string variable or an expression that can be converted to a string without the end of a single line example:<%= "Hello World"%><%=i+1% ><%=request.getparameter ("name")%>

"Attention": shaped like http://localhost:8888/test/Expressions.jsp?testParam=abc%20de, question mark? The following testparam=abc%20de is the parameter passed to the JSP, and if the argument contains a space, it will be replaced with the% 20来;

5) Directive: Compile-time directive

Directive (compile instruction) is equivalent to the command format during compilation:<% @Directive Property = "Property value"%> common Directive:pageincludetaglib

Learn more about the three common directive:

5.1 Directive-page:

indicates the basic format of communication with JSP Container:% @page language= "script language" |extends= "ClassName" |Import= "Importlist" |Buffer= "NONE|KB Size" | --None: No buffering, default 8k session=true|false"| --whether the session can be used, by default true AutoFlush=true|false” --whether the buffer is automatically cleared, by default true IsThreadSafe=true|false"| --default False (never set to true) info= "InfoText" | --any character ErrorPage= "Errorpageurl" |Iserrorpage=true|false"|ContentType= "Contenttyepinfo" |pageencoding="gb2312"%

"Note" Several important properties: Import, ErrorPage, Iserropage, ContentType

The following examples illustrate

Import and ContentType

New Date ()%><%
Out.println ("Hello");
%>

Errpage and Iserrorpage: A friendly error-prone interface that provides user-Errpage pages when an error occurs

<%--*******errpage.jsp*******--%><%@ page contenttype= "text/html;charset=gb2312"%><%@ page is Errorpage= "true"%>error message:<%= exception.getmessage ()%></ Body>

5.2 Directive-include: Used to contain non-dynamic code, cannot pass parameters to the FileURL, that is, the form like abc.jsp?user=aaa can not appear

include the specified JSP program or HTML file in the format:<% @include file= "fileurl%>JSP engine will be included in the file property settings during the conversion period of the JSP program. It then starts to perform the conversion and compilation work. (Copy the file's code to that location intact, then convert and compile, generating only one Java and Class) restriction: Cannot pass parameter to FileURL cannot abc.jsp? user=aaa

6) Action: instruction during runtime

Action (action Instruction) commands are common during runtime: jsp:useBeanjsp:setPropertyjsp:getPropertyjsp:includejsp:forwardjsp:paramjsp: Plugin embedding applets

6.1 Jsp:inlcude/jsp:param

used to dynamically include JSP programs or HTML files, etc. unless this instruction is executed, it will not be compiled by JSP engine such as Tomcat. Format:<jsp:include page= "Urlspec" flush= "true"/><jsp:include page= "Urlspec" flush= "true ">     <jsp:param name=" paramname "value=" paramvalue "/></jsp:include>JSP: Param is used to set the parameters of the include file and the corresponding value and the compiler directive include the difference: the include compiler directive is the JSP program in the conversion period of the file property specified by the program content embedded, and then compiled execution While the include directive is not compiled during the conversion period, only if the client request period is executed until it is dynamically compiled, the include cannot be loaded with parameters, and <jsp:include> can. Dynamically included files and included files are the same request object. Generate two class files

6.2 Jsp:forward/jsp:param

used to route the contents of a JSP to the JSP program specified by the page or to the servlet processing (URL) format:<jsp:forward page= "Urlspec" flush= "true"/ ><jsp:forward page= "Urlspec" > <jsp:param name= "paramname" value= "Paramvalue"/></jsp:forward> <jsp:param> The page that specifies the parameter and its corresponding value forward and forward the same request corresponds to this response.sendredirect

The difference between 6.3 <jsp:forward> and Response.sendredirect

used to route the contents of a JSP to the JSP program specified by the page or to the servlet processing (URL) format:<jsp:forward page= "Urlspec" flush= "true"/ ><jsp:forward page= "Urlspec" > <jsp:param name= "paramname" value= "Paramvalue"/></jsp:forward> <jsp:param> The page that specifies the parameter and its corresponding value forward and forward the same request corresponds to this response.sendredirect

The difference between the two can be described in the following illustration:

The Forward Method request page is marked in blue (both front and back), and the Sendredirect method (different request) is marked with green.

6.4 Jsp:usebean (do not use the naked class, put the bean into the package, such as Com.itcast.useBean)

With Jsp:usebean, you can use the basic elements of a well-defined Beanbean in your JSP: You must have a constructor with no parameters. The empty constructor Bean class that is called when the JSP element creates the bean should not have any public instance variables, that is, no direct access to the instance variable, the first letter of the variable name must be lowercase by getter/setter method to read/write the value of the variable and change the first letter of the corresponding variable to uppercase basic usage: test.jsp/Counterbean.java do not use naked classes (specification requirements) Jsp:usebean The meaning of each parameter: ID: object Instance name Scope:bean scope, default to page, valid for entire JSP pageclass: Bean class name (full name) The Type:bean instance type, which can be this class, or its parent class, or an implemented interface, defaults to the meaning of this class of scope parameters: page: Covers only pages using JavaBean request: The valid range is limited to requests using JavaBean session: The valid range is valid throughout the user's connection (throughout the session) application: The effective range covers the entire application. That is, the entire site is valid Jsp:setproperty format:<jsp:setproperty name="Beanname" property= "PropertyName" |property= "*the value= "Property value" |param= "ParamName"/>equivalent to the format of the Beanname.setpropertyname (value) method call (Setxxx () method) Jsp:getproperty:<jsp:getproperty name= "Beanname" property= "PropertyName"/>the equivalent of the Beanname.getpropertyname () method call (GetXxx () method) establishes the association between the form parameter and the Bean property by Param specifying the name of the form element, specifying the corresponding bean property name by Perperty. The correlation between the two variables is thus established through*to set the association between all properties and input parameters when establishing the correspondence between bean properties and form parameters, the server automatically converts the corresponding parameters into data that matches the property type.

The first experience of JSP learning

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.