JSP - FAQ (3)

By Calvin Gomez,2014-09-16 16:54
9 views 0
JSP - FAQ (3)

    JSP - FAQ (3)


日期;2000-8-10 17:50:50

18) What do the differing levels of bean storage (page, session,

    app) mean? TOC

From: Joe Shevland <>

The spec is not clear on what the Application level scope actually

    means, but the general discussion has it that an Application is a

    single JSP whose beans persist from call to call - unlike those

    beans which have the "page" scope. That said, the 0.92 spec still

    stores "application" beans at the servlet level so they can

    actually be used by multiple servlets.

(From Gabriel Wong <gabrielw@EZWEBTOOLS.COM>)

In purely Servlet terms, they mean:

page - NO storage

session -


application -


    request - The storage exists for the lifetime of the request, which may be forwarded between jsp's and servlets.

19) Where can I find the mailing list archives? TOC

Archives of the JSP mailing list are available at

These archives are searchable.

20) What are the important steps in using JDBC in JSP? TOC

    1) Instantiate an instance of the JDBC driver you're trying to use (jdbc-odbc bridge name from memory so pls check):

Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" ).newInstance();

    This determines if the class is available and instantiates a new instance of it, making it available for the next step.

    2) Ask the DriverManager for a Connection object based on the JDBC URL you are using:

Connection connDB =

    DriverManager.getConnection( "jdbc:odbc:MyDSN",

"username", "password" );

    DriverManager searches through any registered drivers (instantiating a new instance above is enough to register a driver with the DriverManager, as each implementation is required to) and, based on the JDBC URL you are using, returns the appropriate implementation of Connection.

3) Create a Statement object to retrieve a ResultSet

Statement smentDB = connDB.createStatement();

ResultSet rsDB = connDB.executeQuery( "SELECT * FROM Foo" );


rsDB = connDB.executeUpdate( "UPDATE Foo SET Bar = NULL" );

4) Close down connections to free resources:




    Note smentDB.close() closes the rsDB object, and connDB will close smentDB, cascading down, so you can really just: connDB.Close().

Also note there's no exception handling given here.

    With the release of the JDBC 2.0 API, there is a CachedResultSet capability which would provide some assistance in making your pages perform better:


I tested the JDBC CachedRowSet

    "JDBCTM CachedRowSet is an implementation of the Rowset interface. The Rowset interface is part of the JDBC 2.0 Standard Extension API.

    CachedRowSet provides a disconnected, serializable, scrollable container for tabular data. A CachedRowSet object can be thought

    of as a disconnected set of rows that are being cached outside of a data source.

    Data contained in a CachedRowSet may be updated and then resynchronized with the underlying tabular data source. "

21) How does variable scope work in JSP? TOC

From: Alexander Yavorskiy <Alexander_Yavorskiy@VANTIVE.COM>


    An interesting observation about the variables declared in JSP pages.

Any variable declared inside <% .... %> is local to the page and

    is not visible to outside functions, even those declare on the same JSP.



int evilVariable = "666";



function testFunction() {

// do not see evilVariable from here


    Why? evilVariable eventually becomes a local variable in the service() method of the resulting servlet and so is not accessible by other methods of that servlet.

    Any variable declared inside <%! %> become global for any function declared in the servlet.



int evilVariable = "666";



function testFunction() {

int x = evilVariable; //can get to it


    Why? evilVariable declared this way becomes a private member variable of the resulting servlet and so is accessible by all other methods of that servlet.


    It is important to understand this difference because in servlet environment there will only be a single(!!!) instance of the resulting servlet running and serving all requests for a particular page. Thus, potentially all of the member variables of

    that servlet will be share across the requests as opposed to variables local to the service() method that will be recreated for each request. So, we should be careful about putting none constant variables in >. At the same time, it might be

    useful to do so in some situations.

22) How do I forward to an HTML page? TOC

    The method forward() in the Servlet API works for JSP pages, but this is only true for resources with _active_ content, like JSP pages.

    If you wish to forward to an HTML page, you have to use a different method:

From: Volker Stiehl

    In order to access HTML files you have to use the new "resource abstraction" feature of 2.1.

Try the following:

URL url = getServletContext().getResource("/abc/xyz.html");


    23) Are there any white papers or documents explaining how JSP fits? TOC

From: "Craig R. McClanahan" <>

    It is titled "The Web Application Programming Model", and provides a nice overview of the basic architecture IBM proposes for web

    applications (essentially the "Model 2" approach from the JSP specification). There are few IBM-specific product references in this document -- simply translate their term "dynamic server pages" into JSP, and generalize "WebSphere" to any useful

    combination of web server, servlet engine, and app server components. There are more IBM-specific references in several of the other white papers, but they still provide a useful overview of the technology basis for large scale web-based application

    development and deployment. The white paper index is at:

24) How to I create dynamic GIFs for my JSP? TOC

From: Matti Kotsalainen <matti@RAZORFISH.COM>

    If you want to create GIFs, use ACME labs excellent free gifencoder( ,and then do something like this:

Frame frame = null;

Graphics g = null;

    FileOutputStream fileOut = null;

try {

//create an unshown frame

frame = new Frame();


    //get a graphics region, using the frame

    Image image = frame.createImage(WIDTH, HEIGHT);

g = image.getGraphics();

//manipulate the image

    g.drawString("Hello world", 0, 0);

//get an ouputstream to a file

    fileOut = new FileOutputStream("test.gif");

GifEncoder encoder = new GifEncoder(image, fileOut);


} catch (Exception e) {


} finally {

//clean up

if (g != null) g.dispose();

    if (frame != null) frame.removeNotify();

if (fileOut != null) {

try { fileOut.close(); }

catch (IOException ioe) { ; }



    25) Do you know where I could get some code that would encode something to the HTML DTD standard? TOC

As a matter of fact...

    (NB: This is an implementation of the HTMLEncode function that ASP has)

From: Eric Lunt <elunt@YAHOO.COM>

Somewhere in my net-travels I picked up this version which

    performs pretty well:


    * Returns an HTML rendition of the given String. This was

Report this document

For any questions or suggestions please email