Dynamic Web Programming: JSP Introductory Learning Tutorial

Source: Internet
Author: User
Tags execution expression file upload connect odbc sql stmt stock prices
js| Programming | dynamic | tutorials | Web page

first, what is JSP

JSP (JavaServer pages) is a Dynamic Web page technology standard, which is initiated by Sun Microsystems Company, and is built by many companies, and its Web site is http://www.javasoft.com/products/jsp. Adding Java program Fragments (scriptlet) and JSP tags (tag) to the traditional Web page HTML file (*htm,*.html) forms the JSP Web page (*.jsp). When a Web server encounters a request to access a JSP Web page, it first executes the program fragment in it, and then returns the execution results to the customer in HTML format. Program fragments can manipulate databases, redirect Web pages, and send emails, and so on, which is the functionality required to build dynamic Web sites. All programs operate on the server side, the network sent to the client is only the result of the minimum requirements for customer browsers, can be achieved without plugin, no ActiveX, no Java applets, even without frame. This article will introduce the method of using JSP technology to develop dynamic Web pages, and will briefly analyze the differences between JSP technology and Microsoft's ASP technology.

second, how to install and start

In order to experiment JSP technology, first need to establish the running environment, this process is quite simple:

1. Download JDK (Java 2 SDK, Standard Edition, v 1.2.2) at http://java.sun.com/jdk/.

2. Download JSWDK (JavaServer Web Development Kit 1.0.1) at http://java.sun.com/products/jsp/. Linux users can download Tomcat 3.0 at ttp://jakarta.apache.org/.

3, installation in the Windows NT environment for example, the JDK installation is to run the downloaded Jdk1_2_2-win.exe first, and then modify the system environment parameters, in the path parameter to add [X:]\jdk1.2.2\bin and add new environment parameters classpath= [X:]\jdk1.2.2\lib\tools.jar, where [x:] is the hard drive character (c:, D:, and so on) where the JDK is installed. JSWDK installation only needs to release the Jswdk1_0_1-win.zip with directory to the hard disk root (C:\, d:\ And so on), and then you can find the \jswdk-1.0.1\ directory on your hard disk. If you do not want to keep JSWDK in the future, you can delete this directory, without any system files and registry legacy problems. More detailed installation procedures, as well as installation of JDK and Tomcat under Solaris/unix and Linux, can refer to the installation instructions in the downloaded packages.

4, starting with the Windows NT environment for example, in the \jswdk-1.0.1\ directory to perform Startserver.bat, you can start a JSWDK Web server support JSP Web technology. To not conflict with existing Web servers (such as IIS, PWS, and so on), the JSWDK Web server uses port 8080. In the browser's address bar type http://localhost:8080 or, if you can see the JSWDK Welcome page on the JSP experimental environment has been completed, you can enter the next experiment. To close the Web server, run Stopserver.bat.

three, the JSP simple example

The document directory for the Web server contained in JSWDK is \jswdk-1.0.1\webpages by default, and the main document is index.html and index.jsp by default. That is to say, access to http://localhost:8080 equals access \jswdk-1.0.1\webpages\index.html. With a text editor, such as Notepad in Windows, create a text file hi.jsp, saved in the \jswdk-1.0.1\webpages\ directory, which reads as follows:

<title> hi-jsp Experiment </title>
String MSG = "This JSP test.";
Out.print ("Hello world!");

Typing the Web server in Http://localhost:8080/hi.jsp,JSWDK in the address bar of the browser executes the Java program statements enclosed in <% and%> in the JSP file, where Out.print is outputting the text to the Web page, and the statement <%= variable | The function of an expression%> is to output the value of a variable or expression in a Java scriptlet to a Web page, performing the result as shown in Figure 1. Figure 1 To assign the variable msg as a Chinese string, with <%=%> output, or with Out.print output Chinese string, then in the English version NT4 and Redhat 6.1 under the normal results of the experiment, and in the Chinese NT 4.0 and Chinese 98, it will appear garbled.

Four, the unified website Interface

JSP supports server-side file inclusion, that is, you can insert multiple other files in a JSP file to achieve a unified web interface. Modify the above hi.jsp and save as mypage.jsp:

<%@ include file= "top.htm"%>
String MSG = "This JSP test.";
Out.print ("Hello world!");
<%@ include file= "bot.htm"%>

With the visual HTML editor, such as FrontPage, Dreamweave, and other design Web site framework structure, the design of the frame structure file divided into two parts, the upper half of the above save as top.htm, the following half saved as bot.htm, the code as follows:

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title> My home page </title>
<table border= "0" width= "100%" cellpadding= "4" cellspacing= "0" align= "center"
<TD width= "100%" colspan= "2" bgcolor= "#837ED1" align= "center" ><font face= "Official script"
Color= "#FFFF00" size=5> home page title </font>
<TD bgcolor= "#837ED1" width= "15%" valign= "Top" align= "center" > <br>
<font color= "#FFFFFF" > option </font> <p> <font color= "#FFFFFF" > options </font> </p>
<p> <font color= "#FFFFFF" > options </font> </p>
<p> <font color= "#FFFFFF" > ... </font> </p>
<p> </p>
<TD width= "85%" valign= "Top"
--------------------------------------------------------------------------------------------------------------- -

Type http://localhost:8080/mypage.jsp in the address bar of the browser, and get the results as shown in Figure 2. Figure 2 This site interface can be unified, and designers can focus on the functional modules to deal with user login, connect the database, send email and so on. Each

JSP files have the following structure:

<%@ include file= "top.htm"%>
Implement some features
<%@ include file= "bot.htm"%>
Maintaining the site's interface is also relatively easy, as long as you modify top.htm and bot.htm, you can affect all pages.

Five, server parameter setting

The JSWDK Web server parameter is saved in \jswdk-1.0.1\webserver.xml, and the default setting value can be modified by opening and editing the file with Windows WordPad. This section focuses on a slightly different setting for Tomcat under Jswdk,linux. JSWDK The default document directory is \jswdk-1.0.1\webpages, where subdirectories, such as \jswdk-1.0.1\webpages\test, can be created in a browser http://localhost/ Test access to this directory, in order to enable this subdirectory to execute the JSP program, you must also add in the section in Webserver.xml:

Also, you must create a \jswdk-1.0.1\webpages\test\web-inf directory and copy it from the \jswdk-1.0.1\webpages\web-inf directory: Mappings.properties, Mime.properties, Servlets.properties and Webapp.properties. Complete these procedures to notify the JSWDK Web server of executing JSP programs in Http://localhost/test.

VI. JavaBean

One of the attractions of JSP Web pages is the ability to combine JavaBean technology to expand the functionality of programs in a Web page. JavaBean is a Java class (class) that encapsulates properties and methods to become an object that has some functionality or that deals with a business. JavaBean is organized into package (packets) for management, in effect, is to put a group of JavaBean together in a certain directory, the definition of each class plus package So-and-So, in this case, test. Directory test must be placed in the directory contained in the system environment classpath, the system can find the JavaBean. JSWDK adds \jswdk-1.0.1\webpages\web-inf\jsp\beans\ to Classpath in default state. It is also an easy way to set up your own JavaBean and package in this directory. A simple JavaBean framework is described below. Create a text file Helloworld.java with a text editor and save it in \JSWD

The contents of the K-1.0.1\webpages\web-inf\jsp\beans\test directory are as follows:

Package test;
public class HelloWorld {
Public String name = ' My name ';
Public String Gethi ()
Return "Hello from" + name;
Helloworld.java edited, in DOS, into the directory \jswdk-1.0.1\webpages\web-inf\jsp\beans\, with JDK javac command compiled Helloworld.java as follows:

Javac Helloworld.java

Note that Java is case-sensitive, and in a program, the letters in the compilation command line cannot be written in a wrong case. The success of compilation means that a JavaBean is established. Here's how to use this JavaBean in a JSP. Create a text file hi-bean.jsp with a text editor and save it in the \jswdk-1.0.1\webpages\test directory, which reads as follows:

<title> JavaBean Test </title>
<jsp:usebean id= "Hellobean" scope= "session" class= "Test.helloworld"/>
<%= Hellobean.gethi ()%>
Hellobean.name = "JSP";
Out.print (Hellobean.gethi ());

In the JSP Web page, use the syntax to create the JavaBean object and name it Hellobean. The reader can see from this simple example the setting, getting the JavaBean property, and invoking the JavaBean method. Type http://localhost:8080/test/hi-bean.jsp in the address bar of the browser, and get the results as shown in Figure 3. Note that if you modify and recompile the JavaBean program, the results of the modified Web server that need to shut down and restart the JSWDK will not be effective. If you only modify the JSP file, you do not have to restart the JSWDK Web server. Although, this only completes a very simple JavaBean framework, but follow this framework can design a variety of javabean. For example, accessing data from a JSP is usually done through JavaBean.

Vii. Database Connection

A database connection is the most important part of a dynamic web site, and the technology for connecting to the database in Java is JDBC (Java Database Connectivity). Many database systems have JDBC drivers, and Java programs connect to the database through the JDBC driver, executing queries, fetching data, and so on. Sun also developed JDBC-ODBC Bridge, which uses Java programs to access databases with ODBC drivers, and most database systems now have ODBC drivers, so Java programs can access Oracle, Sybase, MS SQL Databases such as server and MS Access. Here's how to implement a Dynamic FAQ (FAQ and Answer) Web site with access.

First, create an Access database Faq.mdb, where the table FAQs has a field ID (AutoIncrement, and set as the primary key), subject (type, length 200), Answers (memo type). This table can contain some common questions and answers about programming knowledge,

As shown in Figure 4.

Figure 4

Then, add system DSN to the ODBC DataSource module in Control Panel, name the FAQ, and point to Faq.mdb.

Create a JavaBean, named Faq.java, and save it in the \jswdk-1.0.1\webpages\web-inf\jsp\beans\test directory. The contents of Faq.java are as follows:

Package test;
Import java.sql.*;
public class FAQ {
String sdbdriver = "Sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "Jdbc:odbc:faq";
Connection conn = null;
ResultSet rs = null;
Public FAQ () {
try {
Class.forName (Sdbdriver);
catch (Java.lang.ClassNotFoundException e) {
System.err.println ("FAQ ():" + e.getmessage ());
Public ResultSet executequery (String sql) {
rs = null;
try {
conn = Drivermanager.getconnection (SCONNSTR);
Statement stmt = Conn.createstatement ();
rs = stmt.executequery (SQL);
catch (SQLException ex) {
System.err.println ("Aq.executequery:" + ex.getmessage ());
Return RS;

After compiling Faq.java with the method described in the previous section, create a JSP file faq.jsp under the \jswdk-1.0.1\webpages\test directory, which reads as follows:

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title> My FAQ! </title>
<p> <b> This is my faq! </b> </p>
<%@ page language= "java" import= "java.sql.*"%>
<jsp:usebean id= "workm" scope= "page" class= "Test.faq"/>
ResultSet RS = Workm.executequery ("SELECT * from FAQs");
String tt;
while (Rs.next ()) {
tt = rs.getstring ("Answer");
Out.print ("<LI>" + rs.getstring ("Subject") + "</LI>");
Out.print ("<pre>" + TT + "</pre>");
Rs.close ();

Type http://localhost:8080/test/faq.jsp,faq.jsp call JavaBean in the address bar of the browser, read the content from the database and output, and get the results as shown in Figure 5. Limited to space, this article cannot enumerate the complex examples of the jsp-javabean-jdbc/odbc-database, which readers can find and download to the database connection paradigm from the URLs recommended at the end of this article.

VIII. Technical Analysis

Microsoft's ASP technology is also a dynamic web development technology. JSP and ASP form very similar, ASP programmer at a glance can recognize <%%> and <%=%>. But there are a lot of differences that can be found in deep exploration, the main one being the following three points:

1, JSP efficiency and security is higher

ASP to the source form storage, to explain the way to run, each ASP Web page calls need to explain the source code, running efficiency is not high. In addition, the IIS vulnerability has caused many web site source program exposure, including the author of the previous use of ASP developed Web site, the ASP program has been downloaded to the people. JSP is compiled into bytecode (bytecode) before execution, bytecode is interpreted by Java Virtual machine (Java dummy Machine), which is more efficient than source code interpretation, and the cache mechanism of bytecode on the server can improve the efficiency of byte code access. The first call to the JSP Web page may be a bit slow because it is compiled into a cache and is much faster later. At the same time, the JSP source program is not likely to be downloaded, especially

The JavaBean program can be completely placed in the wrong directory.

2, the JSP component (Component) way more convenient ASP through COM to expand complex functions, such as file upload, send email and business processing or complex computing separated into independent reusable module. JSP achieves the same function expansion through JavaBean. In the development aspect, the development of COM is far more complex and tedious than JavaBean, it is not difficult to learn ASP, but it is not easy to learn to develop COM. And JavaBean is much simpler, from the above examples in this article can see that the development of JavaBean is very convenient. In terms of maintenance, COM must be registered on the server, and if the COM program is modified, it must be registered or even shut down and restarted. JavaBean do not need to register, put in the classpath contained in the directory on the line. If JavaBean is modified, then JSWDK and Tomcat now need to shut down and rerun (but not shut down), but the developer has promised to do so in later versions without shutting down the server. In addition, JavaBean is a complete oop, can easily set up a reusable object library for different business processing functions, such as user Rights control, email automatic reply and so on.

3, the JSP adapts the platform to be more extensive

ASP is currently available only for NT and IIS. Although there are chilisoft Plug-ins under UNIX to support ASP, the ASP itself has limited functionality and must be augmented by a combination of asp+com, which is difficult to implement under UNIX. JSP is different, almost all platforms are supported Java,jsp+javabean can be all over the platform to the unimpeded. NT under IIS via a plugin, such as JRun (http://www3.allaire.com/products/jrun/) or servletexec (Http://www.newatlanta.

com/), you can support JSP. The famous Web server Apache has been able to support JSP. Because Apache is widely used on NT, UNIX, and Linux, JSP has a wider operating platform. Although the NT operating system now occupies a large market share, but the advantages of UNIX in the server is still very large, and the new rise of Linux is not small. Porting from one platform to another, JSP and JavaBean don't even have to recompile because Java bytecode is standard and platform-independent. The author will be in the NT under the experiment JSP page intact to get Linux under the run up, feel very satisfied.

Ix. Conclusion

To sum up, JSP is a dynamic site to build a sharp weapon, so recommended to the reader, wish you master JSP, to develop excellent website. ASP programmer also Please try, JSP also has session, request, response/out and other objects, try how to personally.

attached text

Here is an important reference for JSP, interested readers can read further.








Welcome to Www.dukejava.com!

JavaServer pages (JSP) is a java-based technology for creating dynamic Web pages that support Cross-platform and cross Web servers. JSP is comparable to Microsoft's active Server Pages (ASP), but the JSP uses HTML-like markup and Java snippets rather than VBScript. When you use a Web server (such as Apache or Netscape server) that does not provide ASP local support, you can consider using JSP. You can also get an ASP add-on software module for these servers, but it's too expensive. Now Sun doesn't charge you for using JSP (although it may be in the future), and components for Solaris, Linux, and Windows systems are easy to get. Don't confuse the JSP with the server-side JavaScript language. The Web server automatically converts Java snippets generated by JSP into Java fragments (servlets). JSP can also automate many functions, such as using Perl scripting in the past or a server-specific API such as ASP (application programming interfaces). We're about to start helping you build a Web site that uses JSP technology.

Prepare your computer to use the JSP you need the JAVA2 Software development Toolbox (J2SDK), which used to be called the Java Development Toolbox (JDK), the JavaServer Network Development Toolbox (JSWDK), Tomcat, or other Web servers that support JSP. The company provides free J2SDK and JSWDK for Windows, Solaris, and Linux platforms. If you want to use JSP on an existing Web server, and the server itself does not support JSP and Java code Snippets, you can try the Allaire company's JRun. It can be used for Netscape's Enterprise server and FastTrack server, Microsoft Internet Information Server (IIS) and personal WebServer (PWS), Apache and other servers act as attachments to Web servers. You can also use the Java version of the Apache server, which contains the latest JSWDK. Download and install the required software currently, the version number 1.2.2-001 J2SDK downloadable version appears in the form of an installable archive file. This approximately 20MB download software provides a thorough Java development environment that allows you to create any solution that is java-based and leverages the standard core API. In fact, the only condition your Web server uses JSP is to have a Java editor. To have the Web server know the location of the editor, you need to set the JAVA_HOME environment variable to the J2SDK installation directory. If you have already installed in the Windows system and accepted the default directory, add "set java_home=c:\1.2.2" to your "Autoexec.bat" File and reboot. After installing J2SDK, download and install JSWDK or tomcatβ version, the beta version of the java-based Apache Web server. It doesn't matter where you put the JSWDK, as long as you can find it later. In general, place it in the top-level directory so that you do not have to remove another software when replacing JSWDK or JSDK. After installing this file, the preparation is ready, you can start JSP development work. After you install JSWDK correctly, run the StartServer script to start the Web server so that it listens on port 8080 by default. To see if you have correctly installed the tool after you start the server, load a JSP sample file (http://locahost:8080/examples/jsp/). If you can successfully run these sample files, you have installed the software correctly. If you areTo start the server in the console window to see the error message, you need to make some changes. The most frequently occurring problem is that the JAVA_HOME environment variable is not set or incorrectly set. To see the current environment variable settings, type the set setting at a DOS prompt. Start by creating a shortcut page that shows the current date and time of the target and then saves it as sample.jsp before interpreting the JSP syntax:

<title> A-Page </title>
Today is <%= new Java.util.Date ()%>

Put this file and all of your HTML and JSP pages in the directory under the JSWDK installation directory. You can load this page from http://localhost:8080/sample.jsp. When the page is first accessed, the Web server compiles the JSP into a Java snippet so that you can see the current date and time. You have downloaded, installed, and configured the development environment, and you can begin to learn JSP syntax and create your own JSP based solution.

JSP Basic Syntax

After you finish installing the problem, now go to the JSP syntax. An opportunistic way is to download the grammar card. If you are unfamiliar with Java programming, you may want to take a look at Sun's guidelines. However, web creators do not need to do much Java development. In addition to some method calls, you should use as little Java code as possible in your JSP pages. With these tips in mind, let's take a look at the JSP directives and the scripting elements, and then we'll explain JavaBeans and suppressed objects. There are five kinds of JSP directives and script elements. In JSP version 1.0, most JSPs are surrounded by a tag that starts with "<%" and ends with "%>." After the updated JSP 1.1 specification was introduced, there was an XML-compatible version. JSP directives and script elements

Directives <%@ Directive%>
Declarations <%! Declaration%>
Expressions <%= Expression%>
Code fragment/scriptlet <% Code Fragment%>
Comments <%--Comment--%>


JSP directives are designed for the JSP engine. They don't directly produce any visible output, but just tell the engine how to handle the rest of the JSP pages. These directives are always enclosed in the "<%@%>" tag. The two most important directives are "pagepage" and "Include". The "Taglib" directive is not in the discussion column, but it can be used to create a custom tag with JSP1.1. "Page" instructions are seen almost at the top of all JSP pages. Although not necessary, it allows you to specify where to find the supporting Java Category:

<%@ page import= "Java.util.Date"%>,

Where to turn on the Web surfers when there is a Java run problem:

<%@ page errorpage= "errorpage.jsp"%>,

and whether you need to manage the user's session-level information, which may come from multiple pages (further in the JavaBeans section below):

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

The include directive can divide your content into more manageable elements, such as an element that includes a normal page header or footer. The included web page can be a fixed HTML page or more JSP content:

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


JSP declarations allow you to define page-level variables to hold information or to define the support methods that may be required for the remainder of the JSP page. If you find that there are too many code, it is often best to write them in a separate Java class. Declarations are generally in the "<%!%>" tag. Be sure to end the variable declaration with a semicolon (;) because any content must be a valid Java statement:

<%! int i=0; %>.

An expression

With a JSP expression, the expression evaluation result is converted to a string and is directly included in the output page. The JSP expression is included in the "<%=%>" tag without a semicolon unless the semicolon is used in the quoted string section.

<%= I%>
<%= "Hello"%>.

code fragment/script fragment

The code fragment/script fragment (Scriptlets) JSP code fragment or script fragment is embedded in the "<%%>" tag. This Java code runs when the Web server responds to the request. There may be raw HTML or XML statements around the script fragment, where code snippets can allow you to create conditional execution code or code that uses another piece of code. For example, the following code combination uses expressions and snippets to display the string "Hello" in the H1, H2, H3, and H4 tags. Code snippets are not limited to one line of source code:

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



The last major JSP element is an embedded annotation. Although you can always add HTML comments to a file, users will see the comments when they view the page source code. If you don't want the user to see it, you should embed it in the "<%--?--%>" tag:

<%--comment for server side only--%>. JSP with JavaBean Although you can place large chunks of code in a script fragment (scriptlet), most Java code is a reusable component named Javabea. JavaBean are similar to ActiveX controls: they all provide common functionality and can be reused. JavaBean values are obtained by using properties that you can use to access JavaBean settings. To use a person as an analogy, this person is a javabean, the person's name, ID number and address is its attribute. In the JSP website, you basically is by playing "Connect the beans" to make your website dynamic. Assuming that JavaBean is created before the site, the first thing you need to do is tell the JSP page that it will use a "Bean". You can do this with the "<jsp:useBean>" tag:

<jsp:usebean id= "LocalName" class= "Com.jguru.Person" scope= "Application"/>. The "<jsp:useBean>" tag requires you to identify the bean using the "id" attribute. Here you provide a name for the rest of the JSP page of the Bean. In addition to the ID attribute, you must tell the page where to look for the bean, or what its Java class alias is. This category property provides the ability to confirm the bean, and some other methods can do the same. The last required element is the scope attribute. With the help of the scope attribute, you can tell the bean to make a single page (by default) [scope= ' page], a request for [scope= requests] as a session [scope=], or an entire application [scope= ' Application "] Keep the message. With a session range, you can easily maintain items such as shopping carts on JSP Web pages. Once you have a javabean, you can access its properties to customize it. To get the property value, use the <jsp:getProperty> flag. With this tag, you can specify the name of the bean to use (from the Usebean "id" field) and the attribute you want to get its value. The actual values are placed in the output:

<jsp:getproperty id= "LocalName" property= "name"/>.

To change the JavaBean property, you must use the " " tag. For this tag, you need to recognize the bean and attributes again to modify and provide additional new values. If the name is correct, these values can be obtained directly from a submitted table: parameters are obtained:

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

Can be obtained from one parameter, but you must directly name the attributes and parameters:

<jsp:setproperty id= "LocalName" property= "SerialNumber" value= "string"/>

Or you can set it directly with a name and value:

<jsp:setproperty id= "LocalName" property= "SerialNumber" value= <%=.

The last thing about JavaBean: for a Web server to find JavaBean, you need to place its category files in a special location. With JSWDK, the most convenient location is the category directory under the installation directory, such as \jswdk-1.0.1\classes.

JSP implied Object

The last element associated with the JSP syntax is something called an "implied object." In the JSP code fragment, you can use these hidden objects to interact with the code snippet execution Environment of the JSP page. You should try to access these built-in hidden objects as little as possible. However, in some cases, access to the implied object is acceptable. To take full advantage of the hidden objects, you need to know the latest Java Servlet APIs. The following table lists the available suppressed object collections.

Implied Object Description

Request client requests, including parameters passed from the Get/post request

Response Web page Returns the response of the client

PageContext Manage Page Properties here

Session associated with Request

Application the operating environment of the code fragment

Output stream of Out transfer response

Config code fragment Configuration object

Page JSP pages themselves

Exception exceptions that are not captured in Web pages with errors

What do you use them for? Basically, in your script snippet, you can use them to enter the code snippet that executes the JSP code. Instead of delving into the details of too many servlet APIs, let's look at some of the things that we can do with them: You can go directly out of the "out" implied object without an expression and output something to the response:

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

Instead of sending a parameter directly to JavaBean, you can get the parameter value from the Request object:

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

In the process of developing your JSP, you need to create a supporting Java Class (Class), if you create a JavaBeans or find a JSP file that adds too much Java source code. It can help you reuse the source code and reduce the JSP page compile time. When you need to create a Java taxonomy file, you must: Add the JDSWK installation\bin directory to the path. At the end of the path line of the Autoexec.bat file, add "C:1.2.2/bin".

Use this command to copy the jar file to the "\jre\lib\ext:" Directory: Copy C:\jswdk-1.0.1\lib\servlet.jar


Create a JSP web site

Now it's time to put all these JSP grammars in handy. We'll create a Web page that has an input form that allows the user to enter a stock symbol to get the current stock price (20 minutes). If the input is incorrect, the error page is displayed.


First, use this code to create a quote.jsp Web page and save it in a Web page directory under the JSWDK installation directory. Most Web pages are standard HTML, which is dotted with JSP code. Line six is a Web page indicating that all errors will be sent to the "errorpage.jsp" text. Line 13th to 15th is a script fragment that shows the table only if the "symbol" argument is supplied. The "If" code snippet

32 to 34 lines end. Line 17th defines the JavaBean to use, and line 18th loads the JavaBean symbol attributes from the parameter. Lines 27th through 29 show the Bean's properties. In addition to the "if" snippet, no other Java code is actually involved.


Next, save the following JSP source code in the "errorpage.jsp" file in the Web page directory. "This is an error page" prompts for the first line, which sets the Iserrorpage page hint property to True. The previous page said the location of the error page, this page indicates that this is the error page. JSP-specific code in other JSP files is only used if the implied exception object is accessed. The page displays only its values:

<%@ page iserrorpage= "true"%>
<title> Error Page </title>
Our Error Page </font>
!--Print Exception-->
We got ourselves an exception:
<%= Exception%>
<a Href= "Quote.jsp" >restart </a>
Quotes JavaBean
Use Yahoo resources to get stock prices. This resource needs to be saved to the "Quotes.java" file in the "Classes\com\jguru" directory in the JSWDK installation directory. Please edit it here using the Javac editor in JSDK.

your first JSP page.

After creating the two JSP files, building the JavaBean resource file and editing it, you can load the "quote.jsp" file from http://localhost:8080/quote.jsp to see the results, assuming you haven't changed the JSWDK installation settings, Instead, use a different port. This page can certainly be done more beautifully, but it has achieved the functionality that needs to be implemented, and has demonstrated the functionality of the JSP well.

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.