JSP Quick Start

Source: Internet
Author: User
Tags date format define execution expression functions variables variable

JavaServer Pages (JSP) is a Java-oriented Cross-platform Web development language.



JSP is compatible with Microsoft's active Server Pages, but it uses HTML-like labels and Java program snippets rather than VBScript. When you are using a Web server that does not provide local ASP support, that is, Apache or Netscape server, you may consider using JSP. While you can get the ASP add-on modules for these servers, the price is quite expensive, and currently sun does not charge you for the JSP components you need (although Sun may charge it in the future). These components are also easy to obtain for Solaris and Linux and Windows.



Do not confuse the JSP with the server-side JavaScript. The Web server automatically converts Java program code snippets written in JSP into Java servlets. Many features that previously had to be controlled with Perl handwriting or server-specific APIs, such as ASP, can also be automated through JSPs.



Now let's start by helping you build an executable JSP sample Web site.





Install your machine to use JSP



You will need the Java 2 Software development tool (JSDK), its original name is the Java development tool (JDK) and the JavaServer Web site development tool (JSWDK), Tomcat, or other Web servers that support JSP. Sun offers free jsdk and JSWDK for Windows,solaris, and Linux platforms for use.



If you want to use JSP on your current Web server, but the server itself does not support JSP and Java Servlets, you can try Allaire's jrun, which acts like a Netscape Enterprise Edition and FastTrack Server, Microsoft's Internet Information Server (IIS) and personal network server (PWS), Apache, and other servers of the network servers add-on devices. You can also use the Java version of the Apache Web server, which is available in the latest JSWDK.



Download and install the components you need

The downloadable version of the currently published 1.2.2-001,JSDK is in the form of an installable archive. The downloaded archive is about 20MB, providing a complete Java development environment that allows you to build Java solutions that take advantage of standard APIs as the core. However, the only thing your Web server needs to apply to JSP is the Java compiler. To have the network server know the location of the compiler, set the environment variable java.home to the JSDK installation directory. If you are installing and accepting a preset directory on Windows, set this line of program code to JAVA. home=c:.2.2 add to your Autoexec.bat file and reboot.



After installing the JSDK, download and install the JSWDK or beta version of Tomcat, the Java-dominated Apache network server. Where the installation is not important, it is important that you can find it. In general, it is placed in the upper directory, which allows you to replace JSWDK or JSDK Web servers without the need to move other Web servers. After you have installed this file, you can prepare to develop JSP.



After you install the JSWDK correctly, execute the startserver command file to activate the network server with a preset communication port of 8080. To see you after activating the server is all C has the correct installation tool, you can load the sample JSP file any one (http://localhost:8080/examples/jsp/). If you can successfully execute an example file, you can know that you have correctly set the software. If you see the error message in the console window of the activation server, you need to resolve the problem. The most common problem is not setting (or incorrectly setting) environment variable Java.home. To view the current environment settings, type set in DOS mode.



Begin



Before interpreting the JSP syntax, create a quick Web page that displays the current date and time and store it as a sample.jsp:





<title>first page</title>


<body>


<%= new Java.util.Date ()%>

</H3>

</body>




Put this file with all of your HTML and JSP pages in the directory of your JSWDK installation directory. You can download this page in http://localhost:8080/sample.jsp. When you first visit this page, the Web server will translate JSP into Java servlet Program code, then you will see the current date and time.



Now that you have downloaded, installed, and architected the development environment, you are ready to understand JSP syntax and build your own JSP-oriented solution.



The basic principle of JSP grammar





After the installation, we will discuss the syntax of the JSP. If you are lazy, you can download the grammar card and if you are unfamiliar with Java programming, you may want to refer to the Sun's manual; Web site creator should not do too much Java development. In addition to a few function calls, the Java program code that appears on your JSP page should be minimized;



With that in mind, let's take a look at the JSP compiler guidelines and instruction components, and then we'll explain JavaBeans and internal objects. There are five types of JSP compiler guidelines and instruction components. After JSP 1.0, most of the JSPs are contained in a single volume label that ends with <% as the starting%>. The new JSP 1.1 specification has been published and is also compatible with XML.



Compiler guidelines and instruction components for JSP



compiler indicates

<%@ compiler indicates%>



Statement

<%! Statement%>



An expression

<%= expression%>



Program code Snippets/Small Instructions

<% Program code fragment%>



Comments

<%--annotation--%>









compiler indicates



The compiler instructions for the JSP are for the JSP engine. They do not produce any visible output directly; instead, they tell the engine how to handle other JSP pages. They are always included in the <%@?%> volume label. The two main guidelines are page and include. We will not discuss taglib compiler guidelines but it can be used in JSP1.1 to create a custom volume label.



You can almost find page compiler instructions on most of your JSP pages. Although this is not necessary, it allows you to specify where to find supported Java categories such things:

<%@ page import= "Java.util.Date"%&GT;



Where should the message be routed when a Java problem occurs:

<%@ page errorpage= "errorpage.jsp"%&GT;



Well, you are? Information that requires the user to manage the call period, may access multiple Web pages (there will be more talk in JavaBeans later):

<%@ page session= "true"%>.



The include compiler instructs you to divide your content into manageable components, like those with headers or footnotes. The included pages can be either a fixed-format HTML page or a Web page of JSP content:

<%@ include file= "filename.jsp"%>.



Declared



The JSP declaration lets you define variables in the page layer to store the information or define the supported functions so that the rest of the JSP page can be used. If you find yourself with too many program code, you'd better put them in a different Java class. You can find the statement in the <%!?%> volume label. Remember to append a semicolon to the variable declaration, as in any valid Java narrative: <%! int i=0; %>.



An expression



There are expressions in the JSP, and the result of the evaluation expression can be converted to a string and used directly on the output page. The JSP operation belongs to the <%=?%> volume label and does not contain a semicolon, a useless part of the quoted string.



<%= I%>

<%= "Hello"%>.



Program code snippet/Small instruction file



The JSP program code fragment or small instruction file is included in the <%?%> volume label. This Java program code executes when the network server accepts the request. A small instruction file can be raw HTML or XML, and its internal code snippets allow you to create conditional execution code, or just something that uses another program's code. For example, the following program code combines expressions with small instruction files, displays the string "Hello" in H1,h2,h3, and H4 volume labels. Small instruction files are not limited to one line of original program code:



<% for (int i=1; i<=4; i++) {%>

<H<%=i%>>Hello</H<%=i%>>

<%}%>.



Comments

The last major JSP component is an embedded annotation. Although you can include HTML comments in your file, they will see the comments if the user views the original code of the page. If you don't want the user to see your annotation, you can put it in the <%--?--%> volume Label:



<%--the annotation--%> for the servo side.



JSP and JavaBean





Although you can put a large chunk of program code in a small instruction file, most Java program code belongs to reusable components called JavaBean. JavaBean are like ActiveX controls: They provide known functionality and are designed to be reusable for any purpose.



The value of JavaBean is that it can be used through a set of features that provide access to JavaBean settings. As an example, the person is JavaBean, and his name, social security number, and address can be characteristic. For JSP sites, basically you are ' JavaBean ' dynamically connected to your site.



Suppose JavaBean is in?html>



Getting Started with JSP



JavaServer Pages (JSP) is a Java-oriented Cross-platform Web development language.







JSP is compatible with Microsoft's active Server Pages, but it uses HTML-like labels and Java program snippets rather than VBScript. When you are using a Web server that does not provide local ASP support, that is, Apache or Netscape server, you may consider using JSP. While you can get the ASP add-on modules for these servers, the price is quite expensive, and currently sun does not charge you for the JSP components you need (although Sun may charge it in the future). These components are also easy to obtain for Solaris and Linux and Windows.



Do not confuse the JSP with the server-side JavaScript. The Web server automatically converts Java program code snippets written in JSP into Java servlets. Many features that previously had to be controlled with Perl handwriting or server-specific APIs, such as ASP, can also be automated through JSPs.



Now let's start by helping you build an executable JSP sample Web site.





Install your machine to use JSP



You will need the Java 2 Software development tool (JSDK), its original name is the Java development tool (JDK) and the JavaServer Web site development tool (JSWDK), Tomcat, or other Web servers that support JSP. Sun offers free jsdk and JSWDK for Windows,solaris, and Linux platforms for use.



If you want to use JSP on your current Web server, but the server itself does not support JSP and Java Servlets, you can try Allaire's jrun, which acts like a Netscape Enterprise Edition and FastTrack Server, Microsoft's Internet Information Server (IIS) and personal network server (PWS), Apache, and other servers of the network servers add-on devices. You can also use the Java version of the Apache Web server, which is available in the latest JSWDK.



Download and install the components you need

The downloadable version of the currently published 1.2.2-001,JSDK is in the form of an installable archive. The downloaded archive is about 20MB, providing a complete Java development environment that allows you to build Java solutions that take advantage of standard APIs as the core. However, the only thing your Web server needs to apply to JSP is the Java compiler. To have the network server know the location of the compiler, set the environment variable java.home to the JSDK installation directory. If you are installing and accepting a preset directory on Windows, set this line of program code to JAVA. home=c:.2.2 add to your Autoexec.bat file and reboot.



After installing the JSDK, download and install the JSWDK or beta version of Tomcat, the Java-dominated Apache network server. Where the installation is not important, it is important that you can find it. In general, it is placed in the upper directory, which allows you to replace JSWDK or JSDK Web servers without the need to move other Web servers. After you have installed this file, you can prepare to develop JSP.



After you install the JSWDK correctly, execute the startserver command file to activate the network server with a preset communication port of 8080. To see you after activating the server is all C has the correct installation tool, you can load the sample JSP file any one (http://localhost:8080/examples/jsp/). If you can successfully execute an example file, you can know that you have correctly set the software. If you see the error message in the console window of the activation server, you need to resolve the problem. The most common problem is not setting (or incorrectly setting) environment variable Java.home. To view the current environment settings, type set in DOS mode.



Begin



Before interpreting the JSP syntax, create a quick Web page that displays the current date and time and store it as a sample.jsp:





<title>first page</title>


<body>


<%= new Java.util.Date ()%>

</H3>

</body>




Put this file with all of your HTML and JSP pages in the directory of your JSWDK installation directory. You can download this page in http://localhost:8080/sample.jsp. When you first visit this page, the Web server will translate JSP into Java servlet Program code, then you will see the current date and time.



Now that you have downloaded, installed, and architected the development environment, you are ready to understand JSP syntax and build your own JSP-oriented solution.



The basic principle of JSP grammar





After the installation, we will discuss the syntax of the JSP. If you are lazy, you can download the grammar card and if you are unfamiliar with Java programming, you may want to refer to the Sun's manual; Web site creator should not do too much Java development. In addition to a few function calls, the Java program code that appears on your JSP page should be minimized;



With that in mind, let's take a look at the JSP compiler guidelines and instruction components, and then we'll explain JavaBeans and internal objects. There are five types of JSP compiler guidelines and instruction components. After JSP 1.0, most of the JSPs are contained in a single volume label that ends with <% as the starting%>. The new JSP 1.1 specification has been published and is also compatible with XML.



Compiler guidelines and instruction components for JSP



compiler indicates

<%@ compiler indicates%>



Statement

<%! Statement%>



An expression

<%= expression%>



Program code Snippets/Small Instructions

<% Program code fragment%>



Comments

<%--annotation--%>









compiler indicates



The compiler instructions for the JSP are for the JSP engine. They do not produce any visible output directly; instead, they tell the engine how to handle other JSP pages. They are always included in the <%@?%> volume label. The two main guidelines are page and include. We will not discuss taglib compiler guidelines but it can be used in JSP1.1 to create a custom volume label.



You can almost find page compiler instructions on most of your JSP pages. Although this is not necessary, it allows you to specify where to find supported Java categories such things:

<%@ page import= "Java.util.Date"%&GT;



Where should the message be routed when a Java problem occurs:

<%@ page errorpage= "errorpage.jsp"%&GT;



Well, you are? Information that requires the user to manage the call period, may access multiple Web pages (there will be more talk in JavaBeans later):

<%@ page session= "true"%>.



The include compiler instructs you to divide your content into manageable components, like those with headers or footnotes. The included pages can be either a fixed-format HTML page or a Web page of JSP content:

<%@ include file= "filename.jsp"%>.



Declared



The JSP declaration lets you define variables in the page layer to store the information or define the supported functions so that the rest of the JSP page can be used. If you find yourself with too many program code, you'd better put them in a different Java class. You can find the statement in the <%!?%> volume label. Remember to append a semicolon to the variable declaration, as in any valid Java narrative: <%! int i=0; %>.



An expression



There are expressions in the JSP, and the result of the evaluation expression can be converted to a string and used directly on the output page. The JSP operation belongs to the <%=?%> volume label and does not contain a semicolon, a useless part of the quoted string.



<%= I%>

<%= "Hello"%>.



Program code snippet/Small instruction file



The JSP program code fragment or small instruction file is included in the <%?%> volume label. This Java program code executes when the network server accepts the request. A small instruction file can be raw HTML or XML, and its internal code snippets allow you to create conditional execution code, or just something that uses another program's code. For example, the following program code combines expressions with small instruction files, displays the string "Hello" in H1,h2,h3, and H4 volume labels. Small instruction files are not limited to one line of original program code:



<% for (int i=1; i<=4; i++) {%>

<H<%=i%>>Hello</H<%=i%>>

<%}%>.



Comments

The last major JSP component is an embedded annotation. Although you can include HTML comments in your file, they will see the comments if the user views the original code of the page. If you don't want the user to see your annotation, you can put it in the <%--?--%> volume Label:



<%--the annotation--%> for the servo side.



JSP and JavaBean





Although you can put a large chunk of program code in a small instruction file, most Java program code belongs to reusable components called JavaBean. JavaBean are like ActiveX controls: They provide known functionality and are designed to be reusable for any purpose.



The value of JavaBean is that it can be used through a set of features that provide access to JavaBean settings. As an example, the person is JavaBean, and his name, social security number, and address can be characteristic. For JSP sites, basically you are ' JavaBean ' dynamically connected to your site.



Assuming that JavaBean is built before the site is built, the first thing you need to do is tell the JSP page that it needs to use JavaBean. This work can be done with the <jsp:useBean> volume label:

<jsp:usebean id= "LocalName" class= "Com.jguru.Person" scope= "Application"/&GT;.



<jsp:useBean> labels require you to identify the beans with the id attribute. Here, you provide a name for the JSP page to identify the beans, in addition to the ID attribute, you must also tell the page where to find the beans, or its Java class name. The category attribute provides a way to find it in a variety of ways, and the last component that is required is the scope property. With the help of the range attribute, you can tell the beans that you want it for a single page (preset) [scope= "page"]; for a requested web page [scope= "Request"] For the duration of the call [scope= "session"], or for the entire application [scope= "Application"] to maintain its own information. For the duration of the call, you can easily maintain the items in the JSP page, such as the shopping cart.



Once you have declared the JavaBean, you can access its features to fix it. To obtain the value of an attribute, use the <jsp:getProperty> volume label. With the <jsp:getProperty> label, you can specify the name of the bean to use (from the Usebean ID field), and the attributes of the value you want to get. The real value is then placed in the output:

<jsp:getproperty id= "LocalName" property= "name"/&GT;.



To change the characteristics of the JavaBean, you need to use the <jsp:setProperty> volume label. You also need to identify the beans and the features to be corrected for this label, but you also need to provide new values. If the name is correct, these can be obtained directly through the prescribed format:

<jsp:setproperty id= "LocalName" property= "*"/>;



To get from one parameter, you must directly name the attribute as well as the parameters:

<jsp:setproperty id= "LocalName" property= "Address" param= "parametername"/>;



Or you can set it directly by name and value:

<jsp:setproperty id= "LocalName" property= "SerialNumber" value= "string"/> or <jsp:setproperty id= "LocalName" Property= "serialnumber" value= <%= expression%>/>.



The last point about JavaBean: to get the Web server to find JavaBean, you need to put their category files in a special location. The simplest place for JSWDK is the category directory in the installation directory, such as \jswdk-1.0.1\classes.



Internal object for JSP





The last component associated with the JSP syntax is called an internal object. Within the JSP small instruction file, you can access these internal objects to interact with the servlet environment that performs JSP Web pages. Many of the access to internal objects should be simplified. However, these are examples, their access is acceptable, and the need to fully utilize the internal object settings requires an understanding of the latest Java Servlet APIs.



The following table lists the internal objects you can use.







Internal Object Description



Request

Client request, this request contains parameters from the Get/post request



Response

Web page Returns the response of the client



PageContext

The properties of the Web page are managed here



Session

Session associated with the request



Application

What the servlet is doing



Out

The output stream used to transmit the response



Config

The schema object for the servlet



Page

JSP page itself



exception

For error pages, an exception that is not caught









So, what do these do, and how do you use them? Basically, in your small instruction file, you can use them to access the servlet that executes the JSP program code. To avoid talking about the details of too many servlet APIs, let's examine some of the things you can do with them:



Instead of using an expression, you can directly access an internal out object to print something to response:

<% out.println ("Hello"); %>.

You do not have to send parameters directly to JavaBean, you can get the value of the parameter by requesting the object:

<% String name=request.getparameter ("name"); OUT.PRINTLN (name); %>.

When you write a lot of apps in JSP, if you build JavaBeans or find yourself putting too many Java primitives into your JSP files, you need to build supported Java classes that encourage reuse and reduce the time it takes to convert JSP pages. When you need to build a Java class, you must:



Add the JDSWK installation directory \ Bin directory to your path. At the end of the path to your Autoexec.bat file, join C:.2.2\bin.





Copy the jar file to the \jre\lib\ext directory with the following instructions:

Copy C:\jswdk-1.0.1\lib\servlet.jar C:\jdk1.2.2\jre\lib\ext.



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.