How to run Servlet in Tomcat?

Hello All,

I'm new to this java world.infact i'm posting same problem again ,due to lack of response, and still i'm not able to solve it.i'd a very simple servlet prgm, and when i'm tryin to run it thru tomcat, it gives 404 error "The requested resource (/Servlet/servlet/ServletClasses.SimpleServlet) is not available".

i'm using JDK-1.5.0_11 and tomcat-5.0.27.

Now i compiled the servlet prgm and placed tht class file into the "ROOT/webapps/Servlet/WEB-INF/classes" folder.

My servelt prgm is SimpleServlet.java-:

package ServletClasses;

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

import java.util.*;

class SimpleServlet extends HttpServlet

{

public void init(ServletConfig config)

throws ServletException

{

super.init(config);

}

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

{

doPost(request, response);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

{

response.setContentType("text/html");

PrintWriter out= response.getWriter();

out.println("<html>");

out.println("<head> <title> Simple Servlet </title></head>");

out.println("<body>");

out.println("your Address is "+request.getRemoteAddr() + "\n");

out.println("</body></html>");

out.close();

}

}

And my web-xml file is -:

__

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app>

<servlet>

<servlet-name>HelloWorldExample</servlet-name>

<servlet-class>HelloWorldExample</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>HelloWorldExample</servlet-name>

<url-pattern>/servlet/HelloWorldExample</url-pattern>

</servlet-mapping>

<servlet>

<servlet-name>SimpleServlet</servlet-name>

<servlet-class>ServletClasses.SimpleServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>SimpleServlet</servlet-name>

<url-pattern>/SimpleServlet</url-pattern>

</servlet-mapping>

</web-app>

"http://localhost:8080/Servlet/servlet/ServletClasses.SimpleServlet" is the url i'm giving in browser.Please help me now, wether i've to make any changes to my web-xml file or what else i've do, so tht i'll b able to run servlet in tomcat.

Thanx in adv..........

[2745 byte] By [Garva] at [2007-11-27 1:17:22]
# 1

I recommend you to read the Serlvet tutorial in the Java EE tut: http://java.sun.com/javaee/5/docs/tutorial/doc/

It starts at chapter 3. Once done, then repair your web.xml accordingly.

I also highly recommend you to read something about naming conventions in Java. For example uppercasing in package names is not-done. Also see http://www.google.com/search?q=java+naming+conventions+site:sun.com

BalusCa at 2007-7-11 23:52:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Hi!

I didn't see in depth your code, but I have noticed one possible error.

You are trying to access your servlet from the address "http://localhost:8080/Servlet/servlet/ServletClasses.SimpleServlet". But in your web.xml the url-pattern element contains "/SimpleServlet". You have to use this link: http://localhost:8080/Servlet/SimpleServlet to access your servlet.

http://www.myjavaserver.com/~voronetskyy/

voronetskyya at 2007-7-11 23:52:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Thanx for reply

To voronetskyy

I tried the way u told also but getting different error , seeing this error i tried http://localhost:8080/Servlet/servlet/SimpleServlet. buut again got error 404. here is the error i got after doin the way u said:-

The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Error instantiating servlet class ServletClasses.SimpleServlet

org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)

org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)

org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)

org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)

org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)

org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)

java.lang.Thread.run(Thread.java:595)

root cause

java.lang.IllegalAccessException: Class org.apache.catalina.core.StandardWrapper can not access a member of class ServletClasses.SimpleServlet with modifiers ""

sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)

java.lang.Class.newInstance0(Class.java:344)

java.lang.Class.newInstance(Class.java:303)

org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)

org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)

org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)

org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)

org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)

org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)

java.lang.Thread.run(Thread.java:595)

Garva at 2007-7-11 23:52:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
You should make your servlet class public, otherwise Tomcat will not be able to instantiate it.
serlanka at 2007-7-11 23:52:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
making SimpleServlet class public also gives 404 error.i feel tht i'm not able to write the contents in web.xml properly or not giving proper url in browser.......plz tell me how to do mapping in web.xml and thn what url shud i use in browser?
Garva at 2007-7-11 23:52:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
I'd given you two links here above. The first one contains clear and extensive material how to create and use servlets. Be so kind to click at this link and read it thoroughly.
BalusCa at 2007-7-11 23:52:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
no dont change the mapping. you were right before try this http://localhost:8080/servlet/SimpleServlet
lrngjavaa at 2007-7-11 23:52:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

Thanx Balu,

thanx for favourin me.infact i'm reading tht tutorial and at the same time i'd opened this forum also.b4 postin my problem here i'd refered atleast 10 servlet tutorial about confugrin web.xml etc, after not gettin solution any where, i relied at this forum, so plz u also be kind give me some solution with tht link.its just i lost faith on those tutorial and seeked help here.

Garva at 2007-7-11 23:52:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9
oh have you created a jar file yet?
lrngjavaa at 2007-7-11 23:52:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10

tryin http://localhost:8080/servlet/SimpleServlet also gives error 404.What kind of jar file shud i create?Actually i've just started learnin servlets and i'm referin a pdf(core servlets)and i followed whatever he said, but he didn't mentioned to create any jar files, infact i'd given path of servlet.jar file in the environment varibles.plz tell me lil bit elaborating it? infact i feel books r the worst thing to start learnin, i think practically nothing goes the way thy mention............

Message was edited by:

Garv

Message was edited by:

Garv

Garva at 2007-7-11 23:52:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11
check on this link.. i had the same problem that why i have to put it down. http://gurnah.com/blog/?p=25
G_Abubakra at 2007-7-11 23:52:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 12

To G_Abubakr :

Thanx man, atlast my code also works the way u explained in ur blog. but still if i try to put my servlet in tht package and thn doin neccessary change in the web.xml, i'm not getting to output.what if i want to put my servlet in some package? i mean i tried the prgm given above and gave url as http://localhost:8080/Servlet/ServletClasses.SimpleServlet thn i'm getting error tht "error in instantiating servlet"

Garva at 2007-7-11 23:52:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 13
are you sure you are not missing anything in xml, you are putting the pattern, mapping, classname correctly
G_Abubakra at 2007-7-11 23:52:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 14
Thanx to all guys for supporting me, though my problem is not fully solved but atleast i got some output removing my servlet frm its package.This definately gave me lil confidence and now i'll sure concentrate on the links Mr.Balu had given.
Garva at 2007-7-11 23:52:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 15
To G_Abubakr : wait i'll post my xml here with in a sec after changin it to previous mappings.
Garva at 2007-7-21 20:03:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 16

here is the xml code:

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<servlet>

<servlet-name>SimpleServlet</servlet-name>

<servlet-class>ServletClasses.SimpleServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>SimpleServlet</servlet-name>

<url-pattern>/SimpleServlet</url-pattern>

</servlet-mapping>

<session-config>

<session-timeout>

30

</session-timeout>

</session-config>

</web-app>

and this url http://localhost:8085/Servlet/ServletClasses.SimpleServlet i'm giving to browser.

m getting Error 404 again

Garva at 2007-7-21 20:03:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 17
first before doing anything check if your localhost really response from 8085 and not 8080..check http://localhost:8085 and http://localhost:8080
G_Abubakra at 2007-7-21 20:03:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 18
ok ok possibly i given tht url wrong coz as per my url pattern it shud b http://localhost:8085/Servlet/SimpleServlet.and this way it is working.thanx dear, its been 10 days i was stucked like this only............
Garva at 2007-7-21 20:03:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 19

no man i cinfigured tomcat at 8085 in my system, and again including package and mapping the way told above gives me error :

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Wrapper cannot find servlet class ServletClasses.SimpleServlet or a class it depends on

org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)

org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)

org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)

org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)

org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)

org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)

java.lang.Thread.run(Thread.java:595)

root cause

java.lang.ClassNotFoundException: ServletClasses.SimpleServlet

org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1340)

org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1189)

org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)

org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)

org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)

org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)

org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)

org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)

java.lang.Thread.run(Thread.java:595)

Garva at 2007-7-21 20:03:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 20
I think you have a problem in the way you are calling,, i got the same problem until i put everything, it turn ok.. check my package in the blog, and see how im restructor the classes and xml.
G_Abubakra at 2007-7-21 20:03:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 21
wether class file shud b in /WEB-INF or /WEB-INF/classes? i mean my code with package is working only if i put ServletClasses dir in /WEB-INF/classes.
Garva at 2007-7-21 20:03:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 22
should be hereWEB-INF\classes
G_Abubakra at 2007-7-21 20:03:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 23
tht's what m askin coz again in tht book he mentioned to put class files in WEB-INF only.neways by putting class file /WEB-INF/classes, my code is working fine with package and without package.once again thanx dear...............
Garva at 2007-7-21 20:03:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 24
ok good.. hope my little tutorial help some one.. :)
G_Abubakra at 2007-7-21 20:03:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...