Java Reference
In-Depth Information
references line numbers from the generated source code. This may be difficult
to relate to the original JSP source code, so it is advantageous to know how to
view the generated servlet source code, which is stored in the Tomcat 5.5\work\
Catalina\localhost\_\org\apache\jsp folder.
It also is important to note that the compiled JSP servlet, just as any other
servlet, runs as a multithreaded instance on the server; therefore, the concerns
regarding variable usage still apply. JSPs can create and access objects from other
Java classes as any other Java program can; however, to use your own classes,
they must be created within a package. The classes are not directly accessible if
using the default package.
Viewing JSP-Generated Java Source Code
When a JSP first is requested, the JSP container translates the
JSP into servlet source code and then compiles that source code.
To view the generated source code, open the folder at
C:\Program Files\Apache Software Foundation\Tomcat 5.5\work\
Catalina\localhost\_\org\apache\jsp and look for the Java source
file, xxx_jsp.java, where xxx is the name of your JSP.
JSPs are Server-Side Resources
Because a JSP looks so much like HTML, it is easy to forget that
a JSP is a server-side resource, not a client-side resource. Do not
think of a JSP as an HTML page that contains Java code. When
creating a JSP, the code is HTML and Java, and it looks much like
an HTML Web page. In terms of how it works, however, always
think of a JSP as a Java servlet that outputs HTML code. That is
exactly what a JSP is when compiled and executed.
According to the specification for this application, the main Web page in the
WebStocks Web application, named mainForm.jsp, should look like the main
user interface in the StockTracker application. This page contains an HTML
form and is written using JSP, because it must collect user input as well as dis-
play dynamic data from the StockTracker database. Just like the StockTracker
application, the WebStocks Web application displays the user maintenance por-
tion of the user interface only if the user has administrator privileges. By using a
JavaServer Page, the HTML portion of the page provides a form for collection of
user input, while the Java portion allows dynamic data obtained from the data-
base to be displayed. Based on this dynamic data, the portion of the interface
that deals with user maintenance optionally is displayed.
The JavaServer Page, mainForm.jsp, uses JavaScript to do some client-side
processing as shown in Figure 12-65. Lines 14 through 27 define a function that
verifies whether a stock symbol was entered before submitting the form. This
function is called when the user clicks a button that requires a stock symbol be
entered, such as adding or deleting a stock, as was done in the StockTracker
application. The function also is called when the user requests a stock quote. If
no stock symbol was entered, line 18 calls the alert() function to issue a warning
Search WWH ::




Custom Search