js| News | Web page
JavaServer pages (JSP) is a combination of cross-platform and cross Web server mechanism, Java-oriented technology to generate dynamic Web pages. JSP is compatible with Microsoft's ASP (Active Server Pages), but it uses HTML-like tags and Java 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. Although you can obtain the ASP add-on program for these servers, but the price is very expensive, and at present, Sun does not charge the JSP component you want to collect fees (although sun is likely to charge for future). These components are also easy to obtain for Solaris and Linux as well as for Windows.
Do not confuse your JSP with server-side JavaScript. The Web server automatically converts Java 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.
First, how to install your machine to use the JSP
You will need the Java 2 Software development tool (JSDK), its original name being 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 Network Information Server (IIS) and the 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.
1, download and install the components you need
The currently published 1.2.2-001,JSDK downloadable version is in the form of an installable compressed document. The downloaded file is approximately 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 parameter java.home to the JSDK installation directory. If you are installing and accepting a preset directory on Windows, set this line of code to JAVA. home=c:\1.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 install this file, you will be ready to use JSP.
After you install the JSWDK correctly, execute the startserver command to start the network server with a preset communication port of 8080. To see if you have the correct installation tool after starting the server, you can load any one of the sample JSP files (http://localhost:8080/examples/jsp/). If you can successfully execute a sample file, you can know that you have correctly set the software. If you see the error message in the console window of the startup server, you need to solve the problem. The most common problem is that there is no set (or incorrectly set) environment parameter java.home. To check your current environment settings, type set in DOS mode.
2. Before starting to explain 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>
Place this file with all your HTML and JSP pages in 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 code, and you will see the current date and time.
Now that you have downloaded, installed, and architected the environment, you are ready to understand JSP syntax and build your own JSP-oriented solution.
second, the basic principle of JSP grammar
After installation, next we'll talk about JSP syntax. If you are lazy, you can download the grammar card, if you are unfamiliar with Java programming, you may want to refer to the Sun's manual; However, the site creator should not do too many Java use programs, except for a few functions, Java code that appears on your JSP page should minimize it.
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. The JSP compiler guidelines and instruction components have five types. JSP 1.0, most of the JSP is included in the <% as the beginning of the%> as the end of a single tag, the new JSP 1.1 specification has been published, it is also compatible with XML.
1, JSP compiler guidance and instruction components
Compiler guidelines <%@ Compiler guidelines%>
Predefined <%! Predefined%>
Op-<%= expression%>
Code fragment/Small instruction <% program snippet%>
Annotation <%--Annotation--%>
2, compiler guidance
The compiler guidelines for 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 <%@?%> tag. 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 custom tags.
You can almost find page compiler guidelines on most of your JSP pages. Although this is not necessary, it allows you to specify where you can find supported Java categories: <%@ page import= "Java.util.Date"%>, where the information should be routed when a Java problem occurs: <%@ page Errorpage= "errorpage.jsp"%>, and whether you want to manage communication for the user during the talk period, you may be able to access multiple pages (there will be more talk in JavaBeans later): <%@ page session= "true"% >.
The include compiler guidelines let you 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"%>.
3, JSP predefined let you define the parameters of the Web layer, to store information or define the supported function, so that the rest of the JSP page can be used. If you find yourself with too many code, you'd better put them in a different Java class. You can find the predefined in the <%!?%> tag. Remember to add a semicolon to the back of the parameter predefined, just like any valid Java narration: <%! int i=0; %>.
4, JSP has the formula, the evaluation formula can be converted into strings and directly used in the output page. The JSP operation belongs to the <%=?%> tag and does not contain a semicolon, a useless part of a quote string:
<%= I%>
<%= "Hello"%>
5, the JSP code fragment or small instruction file is included in the <%?%> tag. This Java code executes when the network server accepts the request. Small instruction files can be raw HTML or XML, and their internal code snippets allow you to create conditional execution code, or just something that uses another program code. For example, the following code combines expressions with small instruction files, and displays the string "Hello" in H1,h2,h3 and H4 tags. The small instruction file is not limited to one line of original code:
<% for (int i=1; i<=4; i++) {%>
<H<%=i%>>Hello</H<%=i%>>
<%}%>.
Annotations.
The last major JSP component is an embedded annotation. Although you can include HTML annotations in your files, users will see them if they view the original code of the page. If you don't want the user to see your annotation, you can put it in the <%--?--%> tag:
<%--for server-side annotations--%>.
third, JSP and JavaBean
Although you can put a large chunk of code in a small instruction file, most Java code is a reusable component called JavaBean. JavaBean are like ActiveX controls: They provide known functionality and are designed to be reusable for any purpose.
JavaBean's ¤ value 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 the JavaBean is built before the site is built, the first thing you need to do is to tell the JSP page that it needs to use JavaBean, which can be done with <jsp:useBean> tags:
<jsp:usebean id= "LocalName" class= "Com.jjj.Person" scope= "Application"/>.
<jsp:useBean> tags require you to identify Usebean with the id attribute. Here, you provide a name for the JSP page to identify the Usebean, in addition to the ID attribute, you must also tell the page where to find the Usebean, Or it's the Java category name. The category attribute provides a way to find it in a variety of ways, and the last component to be required is the scope property. With the help of the range attribute, you can tell Usebean that you want it to be a single page (preset) [scope= "page"], for a requested web page [scope= "Request"], for a call period [scope= "session"], or for the entire application [ Scope= "Application"] to maintain its own resources. For the duration of the call, you can easily maintain the items in the JSP Web page, such as a shopping cart: Once you have declared the JavaBean, you can access its features to order it. To get the value of an attribute, use the <jsp:getProperty> label. With <jsp:getProperty> tags, you can specify the Usebean name to use (from the Usebean ID field), and the attributes of the value you want to get. Then the real value is placed in the output:
<jsp:getproperty id= "LocalName" property= "name"/>.
To change the characteristics of the JavaBean, you need to use the <jsp:setProperty> tag. For this tag, you also need to identify the Usebean and the features you want to modify, and in addition you need to provide a new value. If the name is correct, these can be obtained directly through the specified 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 allow Web servers to find JavaBean, you have to place 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.
four, the internal object of the 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. These are examples, however, where access is acceptable, and the full use of internal object settings is an understanding of the latest Java Servlet APIs.
For an internal object description:
Request client, this request contains parameters from the Get/post request
Response Web page Returns the response of the client
The properties of the PageContext Web page are managed here
Session with request-related call period
What the application servlet is doing
Out output stream used to transmit the response
Config servlet schema Object
Page JSP pages themselves
Exception for error pages, not captured exceptions
So, what do these do and how do you use them? Basically, in your small command file, you can use them to access the servlet that executes the JSP code. To avoid talking about the details of too many servlet APIs, let's examine some of the things you can do with them: you don't have to use a formula, you can directly access the 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 a JavaBeans or find yourself putting too many Java primitives into your JSP files, you need to build a supported Java class that encourages reuse and reduces the time it takes to convert JSP pages. When you need to create 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:\1.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.
v. Establish a JSP website
Now it's time to put all the JSP syntax together. We will create a Web site with input format for users to enter stock tickers and get the current stock price (about 20 minutes delay). If there is an error, the error page is displayed.
quote.jsp
First, build the quote.jsp with this code and store it in the directory of the JSWDK installation directory. Most of the content of a Web page is standard HTML format, and JSP code is scattered everywhere. Line 6th is the Web Page Editor guide, which indicates that any errors are routed to the errorpage.jsp file. Lines 13th through 15 are small indicators, basically saying that the table is displayed only if the "symbol" parameter exists. This if section contains lines of code from line 32nd to 34. Line 17th defines the JavaBean to use, while line 18th loads its symbolic attributes from the argument. Lines 27th through 29 indicate the attributes that display Usebean. In addition to the IF section, there is no other Java code.
errorpage.jsp
Next, store the following JSP raw code in the page directory of the file errorpage.jsp. This is an error the hint of the page is on the first line, this row sets the page compiler to guide the Iserrorpage property to True. Although the previous page explains where the error page is, this page is saying that it is an error page. The other specific JSP code in the JSP file is the access to the internal exception object. This page only displays its value:
<%@ page iserrorpage= "true"%>
<title>error page</title>
<body>
<!--Print Exception-->
We got ourselves an exception:
<%= Exception%>
<a href= "quote.jsp" >Restart</a>
</body>
Quotes.java
Quotes JavaBean uses Yahoo's resources to get stock. The original code should be stored in the Quotes.java file in the Classes\com\jguru directory under the JSWDK installation directory. From there you can use the JSDK compiler to compile it. Once you have built two JSP files, created the JavaBean original files and compiled them, you can http://localhost:8080/quote.jsp load quote.jsp files to see the results, assuming you did not change JSWDK installation, The use of different communication ports > this page can be enhanced, but it renders a good demonstration of JSP function.