Forum Moderators: mack

Message Too Old, No Replies

Apache Tomcat and classes

Java Server Pages

         

moonbather

4:31 pm on Oct 2, 2003 (gmt 0)

10+ Year Member



Hello,

I have Apache Tomcat installed on my home PC for learning purposes, and have been trying to run "simple" JSP examples from a book.

One example is supposed to be part of a front end, enabling a user to select choices from a list, and have his/her choices mirrored back. It uses three files, called mirrorform1.jsp, mirrorform2.jsp, and ThisArray.java.

mirrorform1.jsp and mirrorform2.jsp are in the ROOT directory. ROOT itself is within the webapps directory.

The book states that ThisArray.java should be added to Tomcat's classes folder where "it will be found automatically by the server". The folder is supposed to exist at the same level within the Tomcat hierarchy as the bin, common, lib, logs, server, webapps, and work folders, but on my installation, there was no classes folder created at this location when I installed it.

So I created a classes folder, and added ThisArray.java to it.

However, when I try to run the example, sending off the choices using mirrorform1.jsp, I get a HTTP Status 500 error which complains about ThisArray.java - I don't think mirrorform2.jsp can find it.

mirrorform1.jsp is:

<%@ page language="java" contentType="text/html" %>
Please select cities of interest to you:<br /><br />
<form action="mirrorform2.jsp">
<input type="checkbox" name="cities" value="Jena">Jena</input><br />
<input type="checkbox" name="cities" value="Meissen">Meissen</input><br />
<input type="checkbox" name="cities" value="Eisenach">Eisenach</input><br />
<input type="checkbox" name="cities" value="Bitterfeld">Bitterfeld</input><br />
<input type="checkbox" name="cities" value="Potsdam">Potsdam</input><br />
<input type="checkbox" name="cities" value="Frankfurt">Frankfurt</input><br />
<input type="checkbox" name="cities" value="Rathenow">Rathenow</input><br />
<input type="submit" value="enter">
</form>

mirrorform2.jsp is:

<%@ page language="java" contentType="text/html" %>
<%@ page import="ThisArray" %>
<% String[] pick=request.getParameterValues("cities");
if (pick!=null && pick.length>0) { %>
Your current selected cities:
<form action ="process.jsp">

<input type="checkbox" name="cities" value="Jena" <%=ThisArray.contains(pick,"Jena")? "checked":"" %> >Jena</input><br />
<input type="checkbox" name="cities" value="Meissen" <%=ThisArray.contains(pick,"Meissen")? "checked":"" %> >Meissen</input><br />
<input type="checkbox" name="cities" value="Eisenach" <%=ThisArray.contains(pick,"Eisenach")? "checked":"" %> >Eisenach</input><br />
<input type="checkbox" name="cities" value="Bitterfeld" <%=ThisArray.contains(pick,"Bitterfeld")? "checked":"" %> >Bitterfeld</input><br />
<input type="checkbox" name="cities" value="Potsdam" <%=ThisArray.contains(pick,"Postdam")? "checked":"" %> >Potsdam</input><br />
<input type="checkbox" name="cities" value="Frankfurt" <%=ThisArray.contains(pick,"Frankfurt")? "checked":"" %> >Frankfurt</input><br />
<input type="checkbox" name="cities" value="Rathenow" <%=ThisArray.contains(pick,"Rathenow")? "checked":"" %> >Rathenow</input><br />
Press the <input type="submit" value="enter"> to continue.
</form>
<% } %>


ThisArray.java is:

public class ThisArray
{
public static boolean contains (String[] key, String val)
{
boolean exists=false;
if(key==null) ¦¦ val==null {return false; }
for (int i=0; i<key.length; i++)
{
if val.equals( key[i])) {exists=true;break; }
}
return exists;
}
}

The file process.jsp mentioned in mirrorform2.jsp is just supposed to indicate that another file would process mirrordorm2.jsp if a user was happy to continue as required by whatever the application involved.

I wondered if anyone could advise me how to get this example working? Thank you for reading this far.

jayjay

9:16 pm on Oct 3, 2003 (gmt 0)

10+ Year Member



A couple suggestions:

Make sure to restart Tomcat after adding a class to the /class directory.

You can alternatively try placing ThisArray.java in

/ROOT/WEB-INF/class

hth

moonbather

2:55 pm on Oct 4, 2003 (gmt 0)

10+ Year Member



jayjay, I liked and tried your suggestions, but Tomcat still has the sulks...this JSP stuff can be quite frustrating:-{

garann

8:45 pm on Oct 6, 2003 (gmt 0)

10+ Year Member



What package is ThisArray in, and do you have all the correct folders for the package in the classes directory? That is, if your package is com.example.tutorials, is ThisArray.java at
webapps/ROOT/WEB-INF/classes/com/example/tutorials?
And, since process.jsp appears to be doing all the work, what happens in that file?

moonbather

2:59 pm on Oct 7, 2003 (gmt 0)

10+ Year Member



The book doesn't go into what process.jsp would do. All the example is supposed to do is echo back the boxes you check in mirrorform1.

The book says ThisArray.java would be found automatically and compiled, if placed in the classes folder. But when Tomcat was installed there was no classes folder in the place the book said one should be.

In my original attempt:
I created a folder called classes where the book said there should be one, and put ThisArray.java in it. The address of the folder is:
C:\Tomcat\jakarta-tomcat-4.1.27\classes.

There was no references to any packages, as the book doesn't say anything about needing them, but of course it could easily be wrong. Interestingly, there is now a ThisArray.class present in the folder, suggesting ThisArray.java was "found".

In my second attempt:
I put ThisArray.java in the classes folder at
C:\Tomcat\jakarta-tomcat-4.1.27\webapps\ROOT\WEB-INF\classes
and it's the only thing in that folder (ie no ThisArray.java present).

Thanks for your interest garann.

moonbather

3:00 pm on Oct 7, 2003 (gmt 0)

10+ Year Member



The book doesn't go into what process.jsp would do. All the example is supposed to do is echo back the boxes you check in mirrorform1.

The book says ThisArray.java would be found automatically and compiled, if placed in the classes folder. But when Tomcat was installed there was no classes folder in the place the book said one should be.

In my original attempt:
I created a folder called classes where the book said there should be one, and put ThisArray.java in it. The address of the folder is:
C:\Tomcat\jakarta-tomcat-4.1.27\classes.

There was no references to any packages, as the book doesn't say anything about needing them, but of course it could easily be wrong. Interestingly, there is now a ThisArray.class present in the folder, suggesting ThisArray.java was "found".

In my second attempt:
I put ThisArray.java in the classes folder at
C:\Tomcat\jakarta-tomcat-4.1.27\webapps\ROOT\WEB-INF\classes
and it's the only thing in that folder (ie no ThisArray.class present).

Thanks for your interest garann.

zoran

7:53 pm on Oct 7, 2003 (gmt 0)

10+ Year Member



Hello moonbather

Just a short fixes for the Java code in ThisArray.java - missing paranthesis in "if" clauses - you probably write them in a hurry:)

ThisArray.java should be added to Tomcat's classes folder where "it will be found automatically by the server"

Tomcat automatically compiles JSP pages, yes. I don't know about Tomcat compiling the .java sources... (although it could be, it does this with JSP code, but I don'y know :)
Which book, please?

One way to make these rolling:
1. create a directory for the web application i.e. TOMCATDIR\webapps\test
2. copy mirrorform1.jsp, mirrorform2.jsp and process.jsp to it
3. create a WEB-INF subdir i.e. TOMCATDIR\webapps\test\WEB-INF
4. inside this add a web.xml descriptor file i.e. TOMCATDIR\webapps\test\WEB-INF\web.xml
containing this code:


<?xml version="1.0"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app/>


5. create a classes subdir inside the WEB-INF dir i.e. TOMCATDIR\webapps\test\WEB-INF\classes
6. and finally :) copy the compiled ThisArray.class to this classes subdir

After starting Tomcat you'll be able to acces your web app at [localhost:8080...]

moonbather

11:26 pm on Oct 14, 2003 (gmt 0)

10+ Year Member



Thank you for your reply, zoran. I'm going to look into your suggestions.

BTW, the example appears on pages 80 to 83 of a book called Java Server Pages in Easy Steps, by Mike McGrath. ISBN 1-84078-197-1. Published in England in 2002, by Computer Step (http://www.ineasysteps.com).