This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes IDEs, Version Control and other tools and the fly likes servlet programming problem in Eclipse Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » IDEs, Version Control and other tools
Bookmark "servlet programming problem in Eclipse" Watch "servlet programming problem in Eclipse" New topic
Author

servlet programming problem in Eclipse

Kavitha Sen
Greenhorn

Joined: May 15, 2008
Posts: 28
Hello Java people,

Please help me. I am new to servlet. I have downloaded eclipse and downloaded tomcat.

I have kathy's Headfirst Servlets and JSP book and i tried working with servlet. I can able to run html page but when i click Submit Button i get 404 error---/SelectBeer.do not available. How can i fix this problem?

Here is my html code.



Here is my servlet code.




I copied the class file and put it WEB-INF/classes directiory.

What should i do to run my servlet? Please help!
[ August 19, 2008: Message edited by: Kavitha Sen ]
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Is your servlet actually deployed ? You never 'compile' HTML. You can only compile JSP pages.

Firstly compile your servlet and place it in the webapps folder of tomcat by packaging it any way you like. Next deploy this application. You can then access the servlet via the default 8080 port of tomcat.

Please give us more details about how you went about deploying your package.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Make sure below things:


1. whether you configure Servlet in web.xml?

2.where you place your html?(is it under any folder? if yes look into your <url-mapping> in web.xml )

Hope This Helps
Kavitha Sen
Greenhorn

Joined: May 15, 2008
Posts: 28
Thanks deepak and seetharaman.

I have my servlet coding under JavaResource :src
my html code under WebContent and xml is under WEB-INF
Here is my xml code.



I just opened the xml file in text editor and deleted the old one and printed the above thing.

I just compiled the servlet. and opend the html page in the browser and chose the beercolor and pressed submit button then i get 404 error. do i miss any step?
[ August 18, 2008: Message edited by: Kavitha Sen ]
Kavitha Sen
Greenhorn

Joined: May 15, 2008
Posts: 28
Hello Friends, please help me!
omkar patkar
Ranch Hand

Joined: Aug 25, 2005
Posts: 231
which version of tomcat are you using...are you sure your tomcat and web app setup was correct?

are you able to run the tomcat examples ?

If not then there is problem with tomcat, check if tools.jar was is available while installing the tomcat.

Another user - Kuldeep has made similar post, refer to suggestions given by other forum members in that post.


Thanks and Regards
Omkar Patkar (SCJP 1.4)[url]http://javacollectionsnotes.blogspot.com[/url] | [url]http://omkar-myscjpexp.blogspot.com[/url]
Kavitha Sen
Greenhorn

Joined: May 15, 2008
Posts: 28
Thanks omkar. Yes my tomcat steup is correct. I tried examples given in tomcat..its working.
Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196


import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class BeerSelect extends HttpServlet{
public void doPost(HttpServletRequest request,HttpServletResponse response)

throws IOException,ServletException{response.setContentType("text/html");PrintWriter out=response.getWriter();
out.println("Beer Selection Advice<br>");String c= request.getParameter("color");out.println("<br>Got beer color"+c);
}
}
Please make sure that your servlet class is in the myFirstWebProject package as defined in the servlet-class tag above.
[ August 18, 2008: Message edited by: Piyush Mangal ]
Kavitha Sen
Greenhorn

Joined: May 15, 2008
Posts: 28
Thanks piyush.

Yes,the BeerSelect.java is in myFirstWebProject directory .

I am little confused with the class files. while browsing they say we have to copy the class file and put it in web-inf or some thing like that..i don't know what to do.

[ August 18, 2008: Message edited by: Kavitha Sen ]
[ August 18, 2008: Message edited by: Kavitha Sen ]
Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196
Hi Kavitha,
Yes you have to place all your .class files into the WEB-INF/classes/...
But as you have specified the servlet-class as
myFirstWebProject.BeerSelect, you also need to have a package statement in
your servlet class which is missing. After compling this class, put this class into the WEB-INF/classes/myFirstWebProject/ .
Kavitha Sen
Greenhorn

Joined: May 15, 2008
Posts: 28
Thanks for your reply piyush.

myFirstWebProject is my project name.under that i have JavaResousrce:src in this folder only i have java file.
then in WEB-INF i have lib folder and xml file.
my directory structure doesn't have WEB-INF/classes All the above are in my projectExporer in eclipse.

If i create directoris and place java file and html file accordingly...i don't know how to compile servlet(in book we need to compile staring with %javac -classpath ect ect
my problem is i don't know where to put these comments to compile the servlet) i am sorry i am new to servlet so i am asking all these.
[ August 18, 2008: Message edited by: Kavitha Sen ]
Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196
I would recommend you dont use Eclipse IDE as of now.
Create the following project hierarchy

myFirstWebProject
some.jsp
WEB-INF/classes/
WEB-INF/web.xml

where myFirstWebProject is your root folder and
the context root would be myFirstWebProject.

As for compling the servlet class
use the command line
javac -cp pathToYourServlet-api.jar yourservletClassName.java
Aniruddh Joshi
Ranch Hand

Joined: Jul 29, 2008
Posts: 275

you can make your job easier using netBeans IDE.
It has an integrated Apache Tomcat server which is useful for beginners.
It saves you from complicated integration issues.

I am not suggesting to use it for long,but at your stage you can go for it.


Anrd
"One of the best things you could do is to simplify a larger application into a smaller one by reducing its process and complexity - Fowler"
Kavitha Sen
Greenhorn

Joined: May 15, 2008
Posts: 28
piyush,

I created the directory sturcture like you said under c directory.
i also created src folder to put java files.

i opend command prompt and did like the following...

c:\javac -cp myFirstWebProject -api.jar ch1servlet.java

it says 'javac' is not recogonised as internal or extrenal command.

oh my god is it means my environmental varialbe needs to be changed?
Kavitha Sen
Greenhorn

Joined: May 15, 2008
Posts: 28
Thanks anirooth.

but let me try this first if i really can't do it then i go with netbeans like you suggest.
Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196
Yes you need to set the JAVA_HOME environment variable.
Sachin Joshi
Ranch Hand

Joined: Aug 06, 2008
Posts: 83

directory having javac.exe needs to be in your PATH variable. This will be present in %JAVA_HOME%/bin directory so you may need to set PATH using below command

If you java is installed in c:\java dir then, this is how you would do....



And I would suggest not to use any IDE for beginner as you will never understand how the servlet gets deployed. Try compiling using IDE and copy the class files to you app directory in webapps folder. This should give you much clarity on what you are doing....


Web Development Tips and Tutorials - By Sachin
Kavitha Sen
Greenhorn

Joined: May 15, 2008
Posts: 28
thanks piyush and sachin.

my jdk is in c:/programfiles/java

so shall i set like this...?



[ August 18, 2008: Message edited by: Kavitha Sen ]
[ August 18, 2008: Message edited by: Kavitha Sen ]
Kavitha Sen
Greenhorn

Joined: May 15, 2008
Posts: 28
i have set the path in autoexe file but still can't make it in command prompt.

Rather shall i just comiple the servlet in the eclipse ide and drag the class file to WEB-INF directory of eclipse explorer?

I tried that too. Still not working.
Arunkumar Durairaj
Greenhorn

Joined: Jun 01, 2008
Posts: 4
hi kavitha ,
hope you got it working now ! after looking over enthusiastic responses , everything seems to be correct.so the problem is with your deployment environment(tomcat).just ensure these steps are made correct

1:Check if tomcat is properly running(check localhost URL)
2:Type in your application url
3:Check whether classes do exists at webinf location ( for this configure output class path to webinf in eclipse)
4.if these steps are ensured fine.then your app should work
Debendra nayak
Greenhorn

Joined: Mar 28, 2008
Posts: 7
Hi Kavitha,
You should have the folder structure like this:
some.jsp
WEB-INF\classes
WEB-INF\lib
WEB-INF\web.xml
You should keep all your servlet class files in the classes folder.
You are saying that you are not able to compile the servlet, it is because, you need to set the location of your servlet(.java) file in your classpath in the environment variable setting(say classpath=d:\myjavaprogram and you should have this directory in the D: drive).Then only you will be abe to compile any java files in that corresponding directory. You also need to set the JAVA_HOME=c:\java and path=c:\java\bin (according to your folder structure). I hope it will help you...........
[ August 19, 2008: Message edited by: Debendra ]
Kavitha Sen
Greenhorn

Joined: May 15, 2008
Posts: 28
Arun,Thanks a lot!

yes the tomcat is working fine and i checked with local host and also application url. its working fine. I copied the class file from some directory and paste it in web-inf dir ..still its not running.

Debendra,Thankyou.

I tried like that yesterday. but its not working. i changed in environment variable-system variable(there is owner variable). when i put javac my commant prompt says

not recogonised as internal and external variable.

i am stuck here in chapter 3 for past 1 week. please help.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60798
    
  65

"Debendra", please check your private messages for an important administrative matter. Again.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Sachin Joshi
Ranch Hand

Joined: Aug 06, 2008
Posts: 83

Directory names are case sensitive so make sure following


- WEB-INF is all uppercase letters.
- classes directory name is all lowercase.
- lib directory name is all lowercase.

The directory containing javac.exe should be in PATH variable. Make sure you have not done any typo in it. If its windows then you need to use "\" not "/" . Best way to avoid such typo is to copy the directory location from explorer address bar.


Lets do it step by step....
Sachin Joshi
Ranch Hand

Joined: Aug 06, 2008
Posts: 83

This is what you need to run for setting you environment. This needs to be run everytime you open the command window.



For confirmation of values in PATH variable below command can be used




is your directory name "programfiles "or "Program Files"
Kavitha Sen
Greenhorn

Joined: May 15, 2008
Posts: 28
Thanks for your help sachin.

My jdk is in Program Files/Java

I set path exactly with the same name(including case and white space. I set the path in environment variable(system variable)

I have not included "set path=" is that is the mistake?
what you mean by every time we have to run?
Is restarting not enough? How can i run the path every time?
sruthi reddy
Ranch Hand

Joined: Aug 03, 2008
Posts: 34
why dont you try running your application from eclipse.right click on your project folder and click run on server. then choose your and tomcat directory and configure that into your eclipse. then your can see servers option i your eclipse and when you click on it , you can see your tomcat and other servers available.check whether your server is on or not and the open your application either in eclipse internal web browser or with IE.
try this and you can know is ther is anything wrong with your code.
Kavitha Sen
Greenhorn

Joined: May 15, 2008
Posts: 28
Thanks sruthi.

yes i tried with eclipse and i know how to start the server in ide.(don't know how to do in command prompt) everything is okay but when i click submit button, i get 404 error instead of corresponding output. I took class files from some directory and placed in WEB-INF but still not working.

My servlet compiles fine no error on that. In ide when you open web.xml ,you see some code right, i just deleted those lines and typed new lines and saved it.

So don't know how to fix now.
sruthi reddy
Ranch Hand

Joined: Aug 03, 2008
Posts: 34
if you havent closed eclipse after deleting the content of your code, then you can get back what you had earlier.just right click on application and click on compare with and you can see your code saved according to time and date.Use replace with to replace the code . If you have closed eclipse after deleting code, then i dont think you can get it back
Sachin Joshi
Ranch Hand

Joined: Aug 06, 2008
Posts: 83

You need to set the PATH variable "Every time you open the command window to compile your servlet".

So if you have compiled your servlet successfully then copy the class file to the WEB-INF/classes folder ....if your servlet is in package (mypack) then its should be in WEB-INF/classes/mypack folder.

Then start the Server. From startup.bat script.

Observe the logs if there is any Exception check what it says....
Kavitha Sen
Greenhorn

Joined: May 15, 2008
Posts: 28
Sruthi,

Let me try to do again the web.xml file and try again in few mins. thanks.

Sachin,

My WEB-INF folder has lib and xml file. Do we need to create the classes folder and run? I just copied the class files and paste it in WEB-INF.

I try and let me tell you the output.
Sachin Joshi
Ranch Hand

Joined: Aug 06, 2008
Posts: 83

This is as per Servlet API specification -

The .class files needs to be kept in WEB-INF/classes folder because this is the directory which will be always checked for classes by the J2EE container.
Kavitha Sen
Greenhorn

Joined: May 15, 2008
Posts: 28
Thanks sachin. I have my servlet in package so am creating those folders and try to run it!
Kavitha Sen
Greenhorn

Joined: May 15, 2008
Posts: 28
I work in eclipse with tomcat.

My servlet is in mySecondWebProject/src/BeerSelect.java

my class file is in

mysecondWebProject/WebContent/WEB-INF/classes/BeerSelect.class

xml file in mysecondWebProject/WebContent/WEB-INF

html file in mySecondWebProject/WebContent/form.html

my xml code...




I tired like you said sachin but not working. I get html page but when i click "submit" button i get 404 error saying /SelectBeer.do not available. where i am going wrong?
[ August 19, 2008: Message edited by: Kavitha Sen ]
Sachin Joshi
Ranch Hand

Joined: Aug 06, 2008
Posts: 83

Change this line



To

Sachin Joshi
Ranch Hand

Joined: Aug 06, 2008
Posts: 83

Also paste the Server log message here so that we can know what is the problem...
Kavitha Sen
Greenhorn

Joined: May 15, 2008
Posts: 28
Hello Sachin,

I changed the code and saved it. Still i get 404 error.

I donot know whats log message. I just compiled servlet and copied the class file and paste it in the coresponding location and run html file. is any other steps to follow?
Sachin Joshi
Ranch Hand

Joined: Aug 06, 2008
Posts: 83

When you start the tomcat server it runs in a console....Check the messages which are logged here.....
Kavitha Sen
Greenhorn

Joined: May 15, 2008
Posts: 28
Here it is..i copied from console...

Aug 19, 2008 8:54:48 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:intro' did not find a matching property.
Aug 19, 2008 8:54:48 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:mySecondWebProject' did not find a matching property.
Aug 19, 2008 8:54:48 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre6\bin;.;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jdk1.6.0_10/bin/../jre/bin/client;C:/Program Files/Java/jdk1.6.0_10/bin/../jre/bin;C:\Program Files\Java\jdk1.6.0_10\bin;C:\Program Files\Java\jdk1.6.0_10\bin;C:\Program Files\Java\jdk1.6.0_10\bin;C:\WINDOWS\System32
Aug 19, 2008 8:54:48 PM org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-80
Aug 19, 2008 8:54:48 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1649 ms
Aug 19, 2008 8:54:48 PM org.apache.catalina.core.StandardService start
INFO: Starting service Catalina
Aug 19, 2008 8:54:48 PM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.16
Aug 19, 2008 8:54:49 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-80
Aug 19, 2008 8:54:49 PM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
Aug 19, 2008 8:54:49 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/63 config=null
Aug 19, 2008 8:54:49 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1479 ms
Kavitha Sen
Greenhorn

Joined: May 15, 2008
Posts: 28
I tried running servlet..it says

405

Http method GET is not supported by this url
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: servlet programming problem in Eclipse
 
Similar Threads
Servlet Doesn't appear to be working.
Starting out on Servlets - from Head First Servlets and JSP
Headfirst ch3 Beerselect submit button not working
Problem getting the Beer Selection Page screen shot
servlet Not able to fetch value from html file