Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

questions where to put the servlets

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got questions when I practice the cookie example from:
http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Cookies.html
There is two servlet, SearchEnginesFrontEnd.java which generate the HTML form and another CustomizedSearchEngines.java 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
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
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
"C:\hall".
In your autoexec.bat file include this statement
" SET CLASSPATH=%CLASSPATH%;C:\ "
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.
solaiappan
 
Hellen Han
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Hello extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

res.setContentType("text/html");
PrintWriter out = res.getWriter();
String name = req.getParameter("name");
out.println("<HTML>");
out.println("<HEAD><TITLE>Hello, " + name + "</TITLE></HEAD>");
out.println("<BODY>");
out.println("Hello, " + name);
out.println("</BODY></HTML>");
}

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

 
P SOLAIAPPAN
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
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 >
settings.
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.
solaiappan

[This message has been edited by P SOLAIAPPAN (edited November 18, 2000).]
 
Hellen Han
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
P SOLAIAPPAN
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
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

</ContextManager>
</Server>
copy the following and paste above "</ContextManager>"
<Context path="/project"
docBase="webapps/project"
defaultSessionTimeOut="30"
isWARExpanded="true"
isWARValidated="false"
isInvokerEnabled="true"
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.
"http://localhost:8080/servlet/Hello?name=solaiappan"
where as you wanted
"http://localhost:8080/project/servlet/Hello?name=solaiappan"
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.
solaiappan

 
Hellen Han
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
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.
han
 
Hellen Han
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Han
 
Hellen Han
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
My first bit of advice is that if you are going to be a mime, you shouldn't talk. Even the tiny ad is nodding:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic