Jsp-faq (3)

Source: Internet
Author: User
Tags exception handling html page interface odbc variables variable scope stringbuffer
JS) What Do the differing levels of beans storage (page, session, app) mean? Toc



From:joe Shevland <J_Shevland@TurnAround.com.au>

The spec isn't clear on what the application level scope actually means, but the general discussion has it this is applic The ation is a single JSP whose beans persist the "page" scope of the from called to call-unlike those beans the which. That said, the 0.92 spec still stores "application" beans in the servlet level so they can actually is used by multiple SE Rvlets.

(from Gabriel Wong <gabrielw@EZWEBTOOLS.COM>)
In purely Servlet terms, they mean:

Page-no Storage
Session-servletrequest.getsession (True). Putvalue ("Myobjectname", MyObject);
Application-getservletconfig (). Getservletcontext (). setattribute ("Myobjectname", MyObject);
Request-the storage exists for the lifetime of the request, which may be forwarded between JSP ' s and Servlets.
Where can I find the mailing list archives? Toc



Archives of the JSP mailing list are available at http://archives.java.sun.com/archives/jsp-interest.html

These archives are searchable.

What are the important steps in using JDBC in JSP? Toc



1) Instantiate an instance of the "JDBC driver you" 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 to it, making it available for the next step.

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

Connection conndb = drivermanager.getconnection ("Jdbc:odbc:MyDSN",
"username", "password");
DriverManager searches through any registered drivers (instantiating a new instance above be enough to register a driver W ith the DriverManager, as each implementation are required to) and, based on the JDBC URL for you are using, returns the Approp Riate implementation of Connection.

3) Create a Statement object to retrieve a ResultSet

Statement smentdb = Conndb.createstatement ();
ResultSet Rsdb = Conndb.executequery ("SELECT * from Foo");
Or
Rsdb = Conndb.executeupdate ("UPDATE Foo SET Bar = NULL");
4) Close down connections to free resources:

Rsdb.close ();
Smentdb.close ();
Conndb.close ();
Note Smentdb.close () closes the Rsdb object, and conndb'll 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 are a cachedresultset capability which would provide some assistance in making Your pages perform better:

From:digne Marc <jmdigne@MEUDON.NETCABLE.TM.FR>

I tested the JDBC cachedrowset http://developer.java.sun.com/developer/earlyAccess/crs/index.html

"JDBCTM CachedRowSet is a 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 the as a disconnected set of rows that are being cached outside of a data source.

Data contained in a cachedrowset is updated and then resynchronized with the underlying data source. "

How does variable scope work in JSP? Toc



From:alexander Yavorskiy <Alexander_Yavorskiy@VANTIVE.COM>

Hi
An interesting observation about the variables declared in JSP pages.

Any variable declared inside <% ...%> is a local to the page and isn't visible to outside functions, even those Dec Lare on the same JSP.

Example:

<%
int evilvariable = "666";
%>
...
function TestFunction () {
Do not-evilvariable from
}
Why? Evilvariable eventually becomes a local variable in the service () method of the resulting servlet and so are not accessible By the other methods of the that servlet.

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

Example:
<%!
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 are accessible by all othe R methods of that servlet.

Conclusion

It is important to understand this difference because in servlet environment there'll only being a single (!!!) instance of The resulting servlet running and serving all requests for a particular page. Thus, potentially all of the variables of this servlet would be share across the requests as opposed to variables lo Cal to the Service () method, that is recreated for each request. So, we should is careful about putting none constant variables in <server></server>. At the same time, it might is useful to does so in some situations.

How does I forward to an HTML page? Toc



The method forward () in the Servlet APIs works for JSP pages, but this are only true for the _active_ content, Li Ke JSP pages.

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

From:volker Stiehl <stiehl@ZNNBG.SIEMENS.DE>

In order to access HTML files for your have to use the new "Resource abstraction" feature of 2.1.
Try the following:

URL url = getservletcontext (). GetResource ("/abc/xyz.html");
Out.println (Url.getcontent ());
Are there any white papers or documents explaining how JSP fits? Toc



From: "Craig R. McClanahan" <cmcclanahan@mytownnet.com>

Http://www.software.ibm.com/ebusiness/pm.html

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 ibm-specific references in several to the other white papers, but they still provide a useful overview of T He technology basis for large scale web-based application development and deployment. The White Paper index are at:

Http://www.software.ibm.com/ebusiness/library.html

How do I create dynamic GIFs for my JSP? Toc



From:matti Kotsalainen <matti@RAZORFISH.COM>

If you are want to create GIFs, use the ACME Labs excellent free Gifencoder (http://www.acme.com/), and then do something as this :

Frame frame = null;
Graphics g = null;
FileOutputStream fileout = null;
try {
Create an Unshown frame
frame = new frame ();
Frame.addnotify ();
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);
Encoder.encode ();
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) {;}
}
}
Know where I could get some code this would encode something to the HTML DTD standard? Toc



As a matter of fact ...

(Nb:this is a implementation of the HTMLEncode function that ASP has)

From:eric Lunt <elunt@YAHOO.COM>

Somewhere in me net-travels I picked up this version which performs pretty:

/**
* Returns an HTML rendition of the given <code>string</code>. This is
* Written by <a Href=mailto:kimbo@biddersedge.com>kimbo Mundy</a>.
* @param text A <code>String</code> to is used in an HTML page.
* @return A <code>String</code> that quotes any HTML markup
* characters. If No quoting is needed, this'll be
* the same as <code>text</code>.
*/
public static string ashtml (string text) {
if (text = = null)
Return "";
StringBuffer results = null;
char[] orig = null;
int beg = 0, Len = text.length ();
for (int i = 0; i < len; ++i) {
char C = Text.charat (i);
Switch (c) {
Case 0:
Case ' & ':
Case ' < ':
Case ' > ':
Case ' "':
if (results = = null) {
Orig = Text.tochararray ();
results = new StringBuffer (LEN+10);
}
if (i > Beg)
Results.append (orig, Beg, I-beg);
Beg = i + 1;
Switch (c) {
Default://Case 0:
Continue
Case ' & ':
Results.append ("&");
Break
Case ' < ':
Results.append ("<");
Break
Case ' > ':
Results.append (">");
Break
Case ' "':
Results.append ("" ");
Break
}
Break
}
}
if (results = null)
return text;
Results.append (orig, Beg, Len-beg);
return results.tostring ();
}
What is page compilation? Toc



(30)



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.