Create a JSP site

Source: Internet
Author: User
Tags stock prices
js| Create | The site can now use all of these JSP syntax. We will create a Web page that has an input form where the user can enter a stock symbol to get the current stock price (with a 20-minute delay). If the input is incorrect, the error prompt page is displayed.



quote.jsp

First, create the quote.jsp page with the following code and save it in the Web directory under the JSWDK installation directory. Most pages are standard HTML with JSP code. Line six is a page instruction indicating that all errors will be sent to the "errorpage.jsp" text. Line 13th to 15th is a script segment that shows the table only if you have a "symbol" parameter. The "If" code snippet ends at 32 to 34 lines. Line 17th defines the JavaBean used, and line 18th loads its symbolic attributes based on the parameters. Lines 27th through 29 show the Bean's properties. In addition to the "If" section, no other Java code is actually involved.



<title>stock quotes</title>


<body>

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

<form action= "quote.jsp"

method= "Get" > <p>enter Symbol: <input size= "a" name= "symbol" ><input

Type= "Submit" value= "Submit" ></p>

</form>

<%

if (Request.getparameter ("symbol")!= null) {

%>

<jsp:usebean id= "Quotes" scope= "page" class= "Com.jguru.Quotes"/>

<jsp:setproperty name= "Quotes" property= "*"/>

<table border= "1" >

<tr>

<th align= "left" >Symbol</th>

<th align= "left" >Name</th>

<th align= "left" >Price</th>

</tr>

<tr>

<td><jsp:getproperty name= "Quotes" property= "symbol"/></td>

<td><jsp:getproperty name= "Quotes" property= "name"/></td>

<td><jsp:getproperty name= "Quotes" property= "Price"/></td>

</tr>

</table>

<%

}

%>

</body>


errorpage.jsp



Next, save the following JSP source code in the "errorpage.jsp" file in the Web page directory. Tip the This is a error page is the first line that sets the page directive Iserrorpage property to True. The previous page describes the location of the error page and this page indicates that this is the wrong page. Other JSP-specific code in the JSP file is used to access the implied exception object. The page displays only its values:

<%@ 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 Get stock prices through Yahoo resources. You need to save the source code Quotes.java to the Quotes.java file in the "Classes\com\jguru" directory in the JSWDK installation directory. From this step, it will be compiled by the Javac compiler in JSDK.

Package Com.jguru;

Import java.util.*;

Import java.net.*;

Import java.io.*;

public class Quotes {

String symbol;

String name;

String Price;

public void Setsymbol (String symbol) {

This.symbol = symbol;

Getsymbolvalue (symbol);

}

Public String Getsymbol () {

return symbol;

}

Public String GetName () {

return name;

}

Public String GetPrice () {

return price;

}

private void Getsymbolvalue (String symbol) {

String urlstring =

"Http://quote.yahoo.com/download/javasoft.beans?SYMBOLS=" +

Symbol + "&AMP;FORMAT=NL";

try {

URL url = new URL (urlstring);

URLConnection con = url.openconnection ();

InputStream is = Con.getinputstream ();

InputStreamReader ISR = new InputStreamReader (IS);

BufferedReader br = new BufferedReader (ISR);

String line = Br.readline ();

StringTokenizer tokenizer = new StringTokenizer (line, ",");

Name = Tokenizer.nexttoken ();

Name = name.substring (1, Name.length ()-2); Remove Quotes

Price = Tokenizer.nexttoken ();

Price = Price.substring (1, Price.length ()-2); Remove Quotes

catch (IOException exception) {

System.err.println ("IOException:" + exception);

}

}

}

When you create these two JSP files and build the JavaBean source code files and compile them, you can load "quote.jsp" from the http://localhost:8080/quote.jsp. File to view the results (assuming you did not change the JSWDK settings to use a different port). This page can certainly be done more beautifully, but it has certainly achieved the intended purpose, while well demonstrating the functionality of the JSP.



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.