Java Server Pages FAQ
---------------------
(tm owned by Sun?)
Maintainer: Richard Vowles, [EMAIL PROTECTED] (www.esperanto.org.nz)
Version 2:
- clarification of servlet engine in (2)
WWW Location of FAQ: http://www.esperanto.org.nz
Table of Contents
-----------------
1) What is JSP?
2) What version is the current version of JSP?
3) Where can I get the specification for JSP?
4) Who supports JSP?
5) What books are available for JSP?
6) Is JSP better than ASP (Microsoft's Active Server Pages)?
7) What HTML editors support JSP?
8) Yes, but how does it perform?
9) How to invoke a JSP page directly from by browser?
10) How do you invoke a JSP page from a servlet?
11) How do you pass data (including beans) to a JSP from a servlet?
12) How does JSP actually work?
13) How can I pool connections to my database?
14) How do I use other languages in my JSP?
15) How can I set a cookie in JSP?
16) Can JSP and Servlet share same Session and Beans?
17) Help! JWS (Java Web Server) won't work with JSP 0.92!
18) How do I use ERRORPAGE?
Other topics + descriptions & additions gladly accepted.
FAQ Contents
------------
1) What is JSP?
JSP is a dynamic scripting capability for web pages that allows Java as
well as a few special tags to be embedded into a web file (HTML/XML,
etc). The suffix traditionally ends with .jsp to indicate to the web
server that the file is a JSP file.
JSP files actually get compiled into Servlets, so what is the point? Why
not just write Servlets?
For most people, the benefit is twofold:
- the focus is on HTML. Java and the JSP extensions assist in making
the HTML more
functional. Servlets on the other hand allow outputting of HTML but
it is a tedious
process.
- it is easy to make a change and then let the JSP capability of the
Web Server you are
using deal with compiling it into a Servlet and running it.
2) What version is the current version of JSP?
0.92 is the current version of JSP and has been for some time. Sun are
really, really, really slow at bringing out the 1.0 spec for JSP and
this is causing a lot of flak as many believe that they are trying to
add too much functionality.
There is some question about the release of the Servlet Interface
standard that is required for using 0.92. This may affect whether your
web server can support this standard. If the servlet engine is provided
by the JSP implementor, then you are generally in luck.
0.92 is available from http://java.sun.com/products/jsp/jsp092.html
0.91 is a popular version though, and much software that supports the
JSP pre-standard actually supports 0.91 of JSP.
0.91 is available from http://www.burridge.net/jsp/Spec91/jsp_spec.html
3) Where can I get the specification for JSP?
You can get the current spec from
http://java.sun.com/products/jsp/jsp092.html
4) Who supports JSP?
There are the following implementations available:
(Name of Product, Version supported, where to get it from, cost?)
- JSP Reference Implementation, 0.92, http://java.sun.com/products/jsp,
free
- GNUJSP, 0.91, http://www.xs4all.nl/~vincentp/gnujsp/, free
- PolyJsp, 0.92, http://www.plenix.org/polyjsp, free + open source
- Resin. A JSP 0.92 implementation for compiled JavaScript,
http://www.caucho.com/, free for personal use
- SJSP. A JSP 0.92 that uses compile-time instrospection,
http://web.telecom.cz/sator/jsp/index.html, free for personal use
- zJSP. A free 0.91 implementation,
http://www.zachary.com/creemer/zjsp.html
- Livesoftware JRUN 2.3 Standard/Pro, 0.92, http://www.livesoftware.com,
commercial
- NewAtlanta ServletExec 2.0.X, 0.91, http://www.newatlanta.com/,
commercial ($395 approx)
- IBM WebSphere 1.X/2.X, 0.91, http://www.software.ibm.com/webservers/,
commercial
5) What books are available for JSP?
Most book vendors appear to be waiting for the 1.0 version of the
specification to be released before committing to any books. This tends
to give ammo to the ASP camp who point to the dearth of good books on
JSP (this may in fact just be a condemnation of ASP for needing so
many... :-)
That said, there was some momentum for a group of people to write a web
book on JSP. If you are interested, contact Aaron at [EMAIL PROTECTED]
6) Is JSP better than ASP (Microsoft's Active Server Pages)?
This is a matter of great debate. Many punters think that JavaSoft has
the benefit of hindsight - what works well and what does't. ASP is very
complex but supports multiple scripting languages and the ActiveX model
of Microsoft. Although ASP _is_ available on many platforms, the ActiveX
library support that makes it as powerful as it is is missing and thus
makes ActiveX only feasible for an NT platform.
According to recent statistics (see the RedHat site), 22% of web servers
are NT, (21% are Linux), that means there is 78% needing something else,
and JSP advocates believe that JSP fits the bill nicely that you very
much.
7) What HTML editors support JSP?
Most of the visual developers do not support JSP in any way, shape or
form, they mess up the tags totally. The following HTML editors have
been known to work with JSP:
- HomeSite, http://www.allaire.com, JSP colour coding will be in HS
4.0.1, but you can get a plug in now from
http://www.burridge.net/jsp/homesite.html
- NetObjects ScriptBuilder 3.0 (win32) supportes Spec. JSP 0.91 and it
can be found at http://www.netobjects.com/products/html/nsb3.html
- Windows Notepad
- VI
8) Yes, but how does it perform?
I have been picking up some comments:
Brian Burridge, [EMAIL PROTECTED]
"Our extranets for 300 franchises, 500+ users, our Intranet for
thousands of employees, and eventually all of our other web sites. New
Atlanta has been running for 6 weeks now without even one problem.
Programming in JSP has turned out to be as easy as Perl, yet all our
tests
show it to be 10x faster."
9) How to invoke a JSP page directly from by browser?
JSP files are just like HTML files, so if your index.jsp file is in your
root directory of your web site, use http://localhost/index.jsp
(Was this what you meant Thomas?)
10) How do you invoke a JSP page from a servlet?
(Contributed by: [EMAIL PROTECTED])
After scanning through archives of the JSP mailing list to no effect I
finally remembered that I'd pasted this example into a document I
wrote. It
was originally sent by Satish Dharmaraj of Sun to show the model 2
approach
(as described in the 0.92 specification): how to pass data from a
servlet
to a JSP .
Create a directory called model1/ under the samples/ directory. Place
foo.jsp and Foo.java inside this directory.
Compile FooServlet.java and place FooServlet.class in
TOP/servlets/directory.
Then invoke using http://host:8080/servlet/FooServlet
In this example, FooServlet creates a list and then stores the result in
Foo.class. Foo.Class is then passed as a datasource to foo.jsp.
The sources are:
1) FooServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import model1.Foo;
public class FooServlet extends HttpServlet
{
public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
String s[] = new String[] {"blue", "green", "red"};
Foo f = new Foo(s);
req.setAttribute("foo", f);
getServletContext().getResourceDispatcher("/samples/model1/foo.jsp").forward
(req, res);
}
}
2) foo.jsp
<html>
<usebean name=foo type=model1.Foo lifespan=page>
</usebean>
<ul>
<loop property=foo:list propertyelement=x>
<li> <display property=x>
</loop>
</ul>
</html>
3) Foo.java
package model1;
public class Foo {
String s[];
public String[] getList() { return s; }
public Foo(String s[]) { this.s = s; }
}
(from "O�Hare, Thomas Bernhard"
<[EMAIL PROTECTED]>)
11) How do you pass data (including beans) to a JSP from a servlet?
req.setAttribute("NameOfAttribute", anObject)
where anObject is any object, including Java Beans
(from "O�Hare, Thomas Bernhard"
<[EMAIL PROTECTED]>)
12) How does JSP actually work?
13) How can I pool connections to my database?
Controlling connections to the database is a desirable thing - having to
connect to the database for each page is is expensive, and keeping a
connection in a session variable is far too expensive in terms of client
connections to the database. Thus, people often create pools for
connections to the database that the client comes in and gets and then
returns when complete (making sure a try/catch is used to ensure the
connection is returned!).
My personal bias indicates that you shouldn't pool connections to your
database inside JSP, you should be using a middleware layer and
communicating to it (like RMI or CORBA). However, people do write
entire applications in JSP and beans that reside in the web server, so
how do you do it?
I've written connection pooling in JBCL (JBuilder's layer on top of
JDBC) which isn't much use to people, does anyone have a connection
pooling example?
14) How do I use other languages in my JSP?
JSP is *Java* Server Pages, and the tags for other languages were taken
out in 0.92. That said, two implementations (as of writing) support
other languages:
- PolyJsp, 0.92, http://www.plenix.org/polyjsp, free + open source
- Resin. A JSP 0.92 implementation for compiled JavaScript,
http://www.caucho.com/, free for personal use
15) How can I set a cookie in JSP?
This should work:
response.setHeader("Set-Cookie", "cookie string");
To give the response-object to a bean, write a method setResponse
(HttpServletResponse response)
- to the bean, and in jsp-file:
<%
bean.setResponse (response);
%>
(from Aapo Kyr�l� <[EMAIL PROTECTED]>)
16) Can JSP and Servlet share same Session and Beans?
Example: I used Beans and Session with my servlet, JSP can use same
Beans
and Session or not?
From: Robert Hodges <[EMAIL PROTECTED]>
This can be done, but you are likely to run into problems with
class loaders. For instance, we have Apache/JServ which uses the
AdaptiveClassLoader along with GNU-JSP which has a different class
loader. If you just casually allocate objects in a servlet and
then pick them up in JSP pages, you'll most likely get the
dreaded ClassCastException, which signals the VM's pleasure when
you try to cast a class that was brought in by a different class
loader.
Note that there are sometimes problems even within JSP, as GNU-JSP
drops the class loader every time you recompile a page, so if
you allocated a class instance using one version of the page, then
recompiled and tried to fish that instance back again, you would
either (1) not find it or (2) get the ClassCastException (but
see para #1 above).
If you really need to pass information around, the best way for
Apache and GNU-JSP is to do the following:
(1) Have one server per person when developing.
(2) Make sure that all your Java classes, including servlet code,
load through the system class path. This means they load
through the primordial loader, which does not go away or
change.
(3) Make sure that your compiled JSP pages go to another location
than your regular Java classes. That way, the JSP loader
will just pick them up through the primordial loader.
In this scheme, you will need to reboot the Web server each time
you make a change to the regular Java classes or else great
confusion will ensue. (And possibly outrage among your users,
I might add.)
If you need to share a Web Server between multiple people, or
cannot reboot whenever you make class changes, the solution is
much more complex. I can post a treatise on one approach at a later
time (big project, deadline Friday) if there is interest.
17) Help! JWS won't work with JSP 0.92!
JSP spec 0.92 wont work with JWS as yet. There is a version mismatch in
the
Servet API releases and JWS (till 1.1.2) does not support JSP.
Try an implementation of JSP 0.91 with JWS.
(From: Anand Kumar S <[EMAIL PROTECTED]>)
18) How do I use ERRORPAGE?
(Contributed by: [EMAIL PROTECTED])
Using ERRORPAGE
I'm not sure if I'm using the correct approach here but in the
application
I'm developing each JSP page includes the line
<%@ ERRORPAGE="01_error.jsp" %>
and the 01_error.jsp page reads:
<HTML>
<USEBEAN name = "AISRB" type = "prj.servlet_utils.PresentError" lifespan
=
page>
</USEBEAN>
<HEAD>
<TITLE><DISPLAY PROPERTY=AISRB:Error PLACEHOLDER="--Error
Title--"></TITLE>
</HEAD>
<BODY>
<BLOCKQUOTE>
<TABLE BORDER="0">
<TR>
<TD><img src="icon/oops.gif"></TD>
<TD><FONT SIZE="5">
<DISPLAY PROPERTY=AISRB:Error PLACEHOLDER="--Error Title--">
</FONT></CENTER></TD>
</TR>
</TABLE>
<P>
<BR>
<DISPLAY PROPERTY=AISRB:Error_Line PLACEHOLDER="--Error Line--">
<BR>
<%= exception.getMessage() %>
<!DISPLAY PROPERTY=exception:message PLACEHOLDER="--Error Message--">
<BR>
<P>
<HR size ="1">
<FONT SIZE="1">Footer</FONT>
<BR>
</BLOCKQUOTE>
</BODY>
</HTML>
The bean is always present in the current request (placed there by
preceding
servlet); I'm actually using this approach for internationalization -
the
bean contains strings callled Error_line etc to say "An error has
occurred"
in the appropriate language. Of course, it doesn't actually contain any
information on the error - this is supposed to be supplied by the
exception
bean.
Speaking of which, I wanted to use the line
<DISPLAY PROPERTY=exception:message PLACEHOLDER="--Error Message--">
to display the exception but that didn't seem to work so now I'm using
<%= exception.getMessage() %>
instead because it does seem to work.
I rather suspect ths is not the best aprroach to reporting unexpected
errors
so you may not want to put it in the FAQ. If you have something better
I'd
be pleased to hear about it.
--
Richard Vowles, Senior Systems Engineer,
Inprise New Zealand, MAIL: [EMAIL PROTECTED], [EMAIL PROTECTED]
HTTP: The Esperanto Group, www.esperanto.org.nz
[my messages contain my own opinions, not those of my employer]
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JSP-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".