File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes questions where to put the servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "questions where to put the servlets" Watch "questions where to put the servlets" New topic

questions where to put the servlets

Hellen Han

Joined: Oct 10, 2000
Posts: 17
I got questions when I practice the cookie example from:
There is two servlet, which generate the HTML form and another which takes care of redirect and search.
My problems is that when I click the submit button on form, it can not find the hall.CustomizedSearchEngines, also I have found the problems, but I can not fix it.
I am using tomcat, I have directory under tomcat\wevapps, I put all the servlet under tomcat\webapps\project\web-inf\classes\hall.
I invoked the SearchEngineFrontEnd under http://localhost:8080/poject/servlet/.... until now every thing is fine, but when I push submit button, another servlet can not be found. I notice the path was shown on the browser is: "http:\\
localhost:8080/poject/servlet/servlet/hall.Customized...." .
That is why another servlet can not be found since the path is not right, they have double "servlet".
I have used form action is:
<FORM ACTION=\"servlet/hall.CustomizedSearchEngines\">\n".
if this action is directly put in the HTML, it work perfectly.
I wonder if someone can give me some ideas why it is not work.
thanks in advance.
Ranch Hand

Joined: Oct 20, 2000
Posts: 68
There in no need for you to make the things complicated.
Why donot you use simple "PAKAGING SERVLETS" method ?
There is no need for you to put your "hall" dir in TOMCAT at all.
Assume that you have "hall" dircectory like this
In your autoexec.bat file include this statement
Here your TOP LEVEL dir is "C:\".
Complile all your hall pakage files.
Invoke your servlet as below
"http://localhost:8080/servlet/hall.SearchEnginesFrontEnd "
Your program will run without any error. Here " /servlet" is needed for TOMCAT to understand that you are trying to invoke a servlet not a JSP or HTML file. It will search all class path given in autoexec.bat + its own class path like
"C:\jakarta-tomcat\webapps\ROOT\WEB-INF\classes" etc(mentioned in server.xml configuration file.)
Watch your FORM code
<FORM ACTION="/servlet/hall.CustomizedSearchEngines">
the above address is added to "project/servlet" which is asumed base directory and adding "/servlet/hall.CustomizedSearchEngines"
to form full URL name.
Why don't you try the above "Package Servlets" and see the result.
Hellen Han

Joined: Oct 10, 2000
Posts: 17
I follow your instruction and that worked great. Thanks very much.
I have another question which may looked funny to you, but it worked for me.
Whenever I want to use <Form Action="some here"> If I use "servlet/hello" , the result is right, but if I use "/servlet/hello", results will be "page can not find". This is one of examples.
So when I tried the code from books or website, I have to take "/" away before servlet in order to make it work on my machine. I wonder if I need to set some staff on my machine like you have said.

import java.lang.String;
import javax.servlet.*;
import javax.servlet.http.*;
public class Hello extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

PrintWriter out = res.getWriter();
String name = req.getParameter("name");
out.println("<HEAD><TITLE>Hello, " + name + "</TITLE></HEAD>");
out.println("Hello, " + name);

public String getServletInfo(){
return "a servlet that knows the name of the person to whom it's" + "saying hello";
<FORM METHOD=GET ACTION="servlet/Hello">
If you do not mid me asking, what is your name?
How to explain this? I am really confused what happened here. I appreciate someone can explain this.

Ranch Hand

Joined: Oct 20, 2000
Posts: 68
Your program is working fine in my PC, even If I give
"/servlet/hello" or "servlet/hello".
Could you give me some more information like
Where do you keep your HTML files & Servlet files?
Have you created your own directory inside TOMCAT and configured
"server.xml" if so what is your
<Context path=" "
docBase=" "
/Context >
Then I can explain what is going on in your case.
Also What is the URL address in browser window after you clicked "SUBMIT QUERY" Button.

[This message has been edited by P SOLAIAPPAN (edited November 18, 2000).]
Hellen Han

Joined: Oct 10, 2000
Posts: 17
Sorry I did not come here to check message until now:
My directory :
for html file: c:\tomcat\webapps\project\form.html
for java file: c:\tomcat\webapps\project\web-inf\classes\hello
I did create a new folder project under tomcat to put my practice files. I do not know how to configure on server.html, and that might the problem I can not invoke the servlet sometimes.
ALso, the URL address in browser window after you clicked "SUBMIT QUERY" Button: http://localhost:8080/servlet/Hello?name=xxxx.
Actually the URL address should be http://localhost:8080/project/servlet/Hello?name=xxxx. So this is not right.
Now those are the staff, I thought the problem might be how to configure the server.xml. I really appreciat your suggestion.
Have a nice weekend.

Ranch Hand

Joined: Oct 20, 2000
Posts: 68
The problem of yours is beter understood now. Edit your "server.xml" file which is inside "conf" directory of "jakarta-tomcat" dir. Goto end of file, you will see

copy the following and paste above "</ContextManager>"
<Context path="/project"
isWorkDirPersistent="false" />
save the server.xml and restart tomcat.
Here "/project" is vitual path it refers to "http://localhost:8080/project"
The REAL PATH is tomcat INSTALLATION DIRECTORY + webapps/project
But you will be back to square one again. In your HTML file you can only use "servlet/hello" in Form Action command so that the virtual path will be "http://localhost:8080/project/servlet" which is correct address as per your application.
When you use ""/servlet/Hello" this will be assumed to be INSTALLATION directory(which is jakarta-tomcat) not the "webapps/project" directory and you will get unexpected result ie.
where as you wanted
To avoid such a problem in your case you can restore to " SEARCH CLASS PATH" method to force your servlet engine to search all class path for your "Hello" servlet class.
In autoexec.bat include the following
"SET CLASSPATH=%CLASSPATH%;C:\jakarta-tomcat\webapps\project\classes "
I thing it is clear.

Hellen Han

Joined: Oct 10, 2000
Posts: 17
first I copied those sentences to the server.xml, I tried to use "/servlet/Hello" for the FORM ACTION, it did not work. Then I tried to restore the autoexec.bat like you have said, but it did not work either. I also restart the computer every time.
I think I must have done sth wrong here, I wonder could you suggest me some internet/books so that I can find this kind of information. I am so frustrated, sometimes everything is right and I just can not figure it out.
Have a nice holiday.
Hellen Han

Joined: Oct 10, 2000
Posts: 17
Hi, I must misunderstand what you means. Check if I am right, when I copy those into server.xml, I only can used FORM ACTION "sevlet/Hello".
How about next part, if I restore the autoexec.bat, could I used "/servlet/Hello". I have tried and it do not work. How could I make the "/sevlet/Hello".
Thanks all those information.
Nice Thanksgiving.
Hellen Han

Joined: Oct 10, 2000
Posts: 17
Hi, I figured it out.
I restore the autoexec.bat like you said, and I thought I forget put the full path name there. Now It looks like: c:\tomcat\webapps\project\web-inf\classes.
and "/servlet/Hello" as FORM ACTION. It worked.
I really appreciate your patient explanation and admire your knowledge.
Thanks again
I agree. Here's the link:
subject: questions where to put the servlets
jQuery in Action, 3rd edition