JSP introduction and example

Source: Internet
Author: User

JSP technology uses Java as the scripting language. JSP web pages provide an interface for the Java Library Unit on the server to serve HTTP applications. JSP enables Java code and specific predefined actions to be embedded into static pages. JSP syntax adds XML tags called JSP actions to call built-in functions. In addition, you can create JSP tag libraries and use them like standard HTML or XML tags. The tag Library provides a platform-independent method to expand server performance.

JSP is compiled into Java Servlets by JSP compiler. A JSP compiler can compile JSP into a servlet written in JAVA code and then compile it into a machine code by the JAVA compiler,

You can also directly compile the code into a binary code.

Example:

 
 
  1. <% @ PageLanguage="Java" Import="Java. util .*" ContentType="Text/html; charset = UTF-8"%> 
  2. <Html> 
  3. <Head> 
  4. <Title>Earliest jsp Writing Method</Title> 
  5. </Head> 
  6. <Body> 
  7. <%
  8. // Print a Statement on the console
  9. System. out. println ("the earliest jsp writing method. java is used as the angular language to join the jsp webpage for related interaction .");
  10. %> 
  11.  
  12. <%
  13. // Java language Loop
  14. For (intI=0; I<10; I ++)
  15. {
  16. %> 
  17.  
  18. Test<Br> 
  19.  
  20. <%
  21. }
  22. %> 
  23. </Body> 
  24. </Html> 

Introduction to JSP: Standard tag library and EL Expression Language

JSP Standard Tag Library, JSTL) is a custom Tag Library that implements common functions in Web applications. These functions include

Iteration and condition judgment, data management formatting, XML operations, and database access.

EL provides identifiers, accessors, and operators to retrieve and operate data residing in the JSP Container.

JSTL syntax and Parameters

JSTL contains the following labels:

Common tags: such as <c: out>, <c: remove>, <c: catch>, and <c: set>

Condition tags: such as <c: if> <c: when>, <c: choose>, and <c: otherwise>

URL tags: such as <c: import>, <c: redirect>, and <c: url>

XML tags: such as <xml: out>

International output tags: such as <fmt: timeZone>

SQL labels: such as <SQL: query>, <SQL: update>, and <SQL: transaction>

General purpose labels:

1. <c: out>

Syntax when no Body exists

<C: out value = "value" [escapeXml = "{true | false}"] [default = "defaultValue"]/>

Syntax when Body exists

<C: out value = "value" [escapeXml = "{true | false}"]>

Here is the Body part

</C: out>

Name type description

Expression to be output by value Object

The escapeXml boolean determines the following characters: <,>, &, ', "Whether the string is divided by a number. The default value is true.

Default Object if the result calculated by vaule is null, the default value is output.

2. <c: set>

This label is used to set a specific value in a certain range (page, request, session, application, etc.), or to set an existing

Attributes of the javabean object. It is similar to <% request. setAttrbute ("name", "value"); %>

Syntax 1: Use the value attribute to set attributes in a specific range.

<C: set value = "value" var = "varName" [scope = "{page | request | session | application}"]/>

Syntax 2: Use the value attribute to set an attribute in a specific range with a Body.

<C: set var = "varName" [scope = "{page | request | session | application}"]>

Body section

</C: set>

Syntax 3: Set an attribute of a specific object.

<C: set value = "value" target = "target" property = "propertyName"/>

Syntax 4: Set an attribute of a specific object with a Body.

<C: set target = "target" property = "propertyName">

Body section

</C: set>

Name type description

The table to be calculated by the value Object.

Var String is used to indicate the attribute of the value. If you want to use it in other labels, this is done through the value specified by var. It is equivalent to defining

Variable, and this variable can only be one of the tags.

The valid range of scope String var, which can be page | request | session | one of

Target String specifies the object of the attribute. It must be a javabean or java. util. Map object.

The property name of the Target Object to be set in the property Object. For example, if the setan has a name attribute and the setUserId method is provided, enter userId here.

.

3. <c: remove>

<C: remove var = "varName" [scope = "{page | request | session | application}"]/>

4. <c: catch>

This tag is equivalent to the exception object thrown by the tag inside it.

<C: catch [var = "varName"]> // var indicates the abnormal name.

Content

</C: catch>

Condition tag

1. <c: if>

Syntax 1: No Body

<C: if test = "testCondition" var = "varName" [scope = "page | request | session | application"]/>

Syntax 2: Body

<C: if test = "testCondition" var = "varName" [scope = "page | request | session | application"]>

Body content

</C: if>

Name type description

The condition of the test Boolean expression is equivalent to the condition judgment statement in if.

Var String indicates the name of the statement.

Scope String var.

2. <c: choose>

Syntax: <c: choose>

Body content (<c: when> and <c: otherwise> sub-tags)

</C: choose>

Note: its Body can only consist of the following elements:

1) Space

2) There are 0 or more <c: when> sub-labels. <c: when> must appear before <c: otherwise> labels.

3) zero or more <c: otherwise> sub-tags.

3. <c: when>

It indicates a condition branch of <c: choose> and can only be used in <c: choose>.

Syntax: <c: when test = "testCondition"> // test is of the boolean type and is used to determine whether a condition is true or false.

Body statement

</C: when>

4. <c: otherwise>

It indicates the final selection in <c: choose>. Must appear at the end

<C: otherwise>

Content

</C: otherwise>

Iteration tag

1. <c: forEach>

Syntax 1: iteration in Collection

<C: forEach [var = "varName"] items = "collection" [varStatus = "varStatusName"]

[Begin = "begin"] [end = "end"] [step = "step"]

Body content

</C: foeEach>

Syntax 2: Fixed iterations.

<C: forEach [var = "varName"] [varStatus = "varStatusName"]

[Begin = "begin"] [end = "end"] [step = "step"]

Body content

</C: foeEach>

Name type description

Var String iteration parameter, which is a tag parameter and can be used in other tags to reference the content of this tag.

Items Collection, ArrayList, and items set to be iterated.

Iterator, Map, String,

Eunmeration, etc.

VarStatus String indicates the iteration status. You can access the iteration information.

Begin int indicates the position where the iteration starts.

End int indicates the position of the End iteration.

Step int indicates the Step of iterative movement. The default value is 1.

URL-related tags

1. <c: import>

Syntax 1: Resource content is exposed externally using a String object

<C: import url = "url" [context = "context"]

[Var = "varName"] [scope = "{page | request | session | application}"] [charEncoding = "charEncoding"]>

Content

</C: import>

Syntax 2: The content of a resource is exposed to the outside using a Reader object.

<C: import url = "url" [context = "context"]

VarReader = "varReaderName" [charEncoding = "charEncoding"]>

Content

</C: import>

Name type description

Url String: the URL of the resource to be imported. It can be a relative or absolute path and can be imported to other host resources.

Context String when a relative path is used to access an external context resource, context specifies the name of this resource.

The name of the var String parameter.

Scope String var parameter.

CahrEncoding String indicates the character encoding of the input resource.

The varReader String parameter is of the Reader type and is used to read resources.

2. <c: redirdbms>

Syntax 1: no Body.

<C: redirect url = "value" [context = "context"]/>

Syntax 2: Specify the query parameters in the Body with the Body

<C: redirect url = "value" [context = "context"]>

<C: param name = "name" value = "value"/>

</C: redirect>

3. <c: url>

Syntax 1: No Body

<C: url value = "value" [context = "context"] [var = "varName"] [scope = "{page | request | session + application}"]/>

Syntax 2: Body

<C: url value = "value" [context = "context"] [var = "varName"] [scope = "{page | request | session + application}"]>

<C: param name = "name" value = "value"/>

</C: url>

Name type description

Value String URL value

Context String when a relative path is used to access an external context resource, context specifies the name of this resource.

Var String identifies the URL scalar.

Scope String variable Scope.

SQL-related labels

1. <SQL: setDataSource>

2. <SQL: query>

3. <SQL: update>

4. <transaction>

5. <param>

Example:

 
 
  1. <% @ PageLanguage="Java" ContentType="Text/html; charset = UTF-8"%> 
  2. <% @ TaglibPrefix="Fmt" Uri=Http://java.sun.com/jsp/jstl/fmt"%> 
  3. <% @ TaglibPrefix="C" Uri=Http://java.sun.com/jsp/jstl/core"%> 
  4. <% @ TaglibPrefix="Fn" Uri=Http://java.sun.com/jsp/jstl/functions"%> 
  5. <Html> 
  6. <Head> 
  7. <Title>Jstl jsp writing</Title> 
  8. </Head> 
  9. <Body> 
  10. <! -- Set the variable testset --> 
  11. <C: set Var="Testset" Value="OK" /> 
  12.  
  13. <! -- Determine whether the testset variable is equal to the OK value. If it is equal, the system displays "successful judgment" --> 
  14. <C: if Test="$ {Testset = 'OK '}"> 
  15. Judgment successful<Br> 
  16. </C: if> 
  17.  
  18. <! -- The loop Tag ends at 10 starting from 0. The increment of each loop is 1, and the current value is assigned to the variable bl --> 
  19. <C: forEach Begin="0" End="10" Step="1" Var="Bl"> 
  20. $ {Bl} test<Br>
  21. </C: forEach> 
  22.  
  23. </Body> 
  24. </Html> 

This is where JSP is introduced.

  1. Establishment of JSP development environment
  2. Brief Introduction to JSP environment Configuration
  3. JSP, ASP, and PHP security programming
  4. Integrate JSP and PHP in Apache
  5. Security issues of JSP and Servlet applications

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.