I'm taking a distributed-Java class at DeAnza College, and I'm currently
working on a homework assignment where we're supposed to set up
sub-classes of HttpServlets to directly respond to incoming requests,
pass control around to each other, make requests upon java beans which
in turn call a database manager to query a relational database.
I'm currently stuck because I can't find any way to activate the HttpServlet
either directly via a URL or indirectly via a transfer of control from
a JSP. At the moment, I'm not concerned about what is the right thing
to do once the HttpServlet has control, I'm just trying to get control
into the HttpServlet in the first place. As a test of a piece of code
getting control, I have it overwrite a file on the /tmp directory
(this is RedHat Linux). Here is the code for a
standalone application and a
HttpServlet
to overwrite that file, with slightly different data so I can look at the
file to see which of the two programs wrote it, but otherwise the actual
code for file I/O is identical (I wrote IO.java first, then simply copied
that code to the DispatcherServlet.hava, deleting the calls to System.out.
The standalone application runs fine overwriting that file as many
times as I want. (The first time it just wrote "Testing file output.",
then I changed the text to what you see now to verify that it woulc
overwrite the file with new data.)
The HttpServlet never runs that file I/O code no matter what I try.
Here's the directory structure where JSPs run fine and where JSPs can
call java class files as needed:
/usr/java/j2sdkee1.3/public_html/date/WEB-INF/classes
JSPs ^-java-classes
Note that the compiled JttpServlet file, i.e. DispatcherServlet.class,
is on that classes sub-directory.
The following URL works for such a JSP:
http://localhost:8000/date/index2.jsp
Here's the complete text of that JSP
Notice down near the end I attempt to transfer control to the HttpServlet.
That doesn't work. The code in the HttpServlet never gets executed,
as evidenced by the file on /tmp never being overwritten.
I know the JSP itself is getting controll, because before I added the
code for RequestDispatcher it produced all the expected output to the
browser.
Also I tried both of these URLs for starting the HttpServlet directly
(without going through the JSP):
http://localhost:8000/date/WEB-INF/classes/DispatcherServlet.class
http://localhost:8000/date/DispatcherServlet.class
and neither of them works either.
By comparison, I can "cheat" by calling methods in a Java class file
directly from the JSP without going through the RequestDispatcher,
for example here is the source of such an example.
See near the bottom where I import Lab3Props.class and then call some
methods to deal with properties files etc., and then I call some methods
to retrieve data from Cloudscape (relational database), and all that works!
But for our class assignment, we are supposed to skip any JSP intermediary,
and invoke the HttpServlet class as a proper servlet directly from a URL
such as the Submit button an a HTML form, or if we can't do that we're
allowed to have the Submit button invoke a JSP file which immediately
passes control to the HttpServlet, but we're not allowed to make
direct calls from a JSP to methods inside a class file. The whole
point of this homework assignment is to learn how to use HttpServlets
to handle the HttpServerRequest object, and have them invoke JavaBeans
to deal with business logic, with JSP used only for output *after* the
business logic has finished setting up all the data to be passed back
to the browser (client).
Oh, in case you're wondering why I have the example 'date' application
from the J2EE tutorial, and my own code, mixed in together. I tried
writing my own tiny J2EE application and using deploytool to make it
available, and it didn't work at all, but the date sample application
did work, so I thought I'd start with something that worked and then
make one tiny change at a time and see if it would still work as it
diverged more and more away from the sample application and started to
have more and more of my own code in it. But after getting direct calls
to methods in class files to work fine, including resource bundles
on properties files, and JDBC link to Cloudscape, I got stumped at the
very next step, when I tried to switch from a simple JSP application
to make it use a HttpServlet.
So my questions are:
- What directory should the DispatcherServlet.class be on?
- What URL should work for calling it directly from a Web browser>
- What should be in the JSP to switch control to the HttpServlet?
Update: I wrote this JSP to illustrate how *not* to call a HttpServlet.
This actually works in that control passes into the HttpServlet and
it proceeds to overwrite the file on the /tmp directory.
Here's the code for totally cheating.