Forum Moderators: mack
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.
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.
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.
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"
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/>
After starting Tomcat you'll be able to acces your web app at [localhost:8080...]