wood burning stoves 2.0*
The moose likes Servlets and the fly likes servlet not able to run Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet not able to run" Watch "servlet not able to run" New topic
Author

servlet not able to run

Priyanka Dande
Ranch Hand

Joined: May 14, 2012
Posts: 53
Hi all,

I have written a class Servlet context i am able to compile it but not able to run, how shouild i run this below code, kindly guide me

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42285
    
  64
You need to add it to the web.xml file: http://www.mkyong.com/servlet/what-is-listener-servletcontextlistener-example/

And you need to put it into a package (something you should do with [b]all[/b| your classes).


Ping & DNS - my free Android networking tools app
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

Looks to me like the servlet-mapping against an incoming request url is missing in your web.xml file.


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42285
    
  64
Joe Harry wrote:Looks to me like the servlet-mapping against an incoming request url is missing in your web.xml file.

There is no servlet here, so that can't possibly be the problem.
Priyanka Dande
Ranch Hand

Joined: May 14, 2012
Posts: 53
Thank you for the response I am using Tomcat 6.0 for my application Do i need to put this listener tag in Tomcat--> webapps --> Project Folder -->WEB_INF--> web.xml or
Tomcat--> WEB_INF -->web.xml
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42285
    
  64
Tomcat keeps web apps in the "webapps" directory, so "Tomcat--> WEB_INF -->web.xml" is not the right place. (I assume "WEB_INF" is a copy/paste mistake instead of "WEB-INF".)
Priyanka Dande
Ranch Hand

Joined: May 14, 2012
Posts: 53
Thanks for the response should i put listener tag in Tomcat -->webapps-->ProjectFolder-->WEB-INF -->web.xml
or in Tomcat -->WEB-INF -->web.xml Kindly guide me
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42285
    
  64
Um, isn't that exactly what you asked before, and what I answered in my previous post?
Priyanka Dande
Ranch Hand

Joined: May 14, 2012
Posts: 53
Thanks i have put the listener tag in Project Folder --> WEB-INF -->web.xml

but below is the error messge given by tomcat 6.0 i am not able to understand the error

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42285
    
  64
The web.xml file is not well-formed XML - there's apparently a problem with what you just added.
Priyanka Dande
Ranch Hand

Joined: May 14, 2012
Posts: 53
I have not put my class in any package just copied into my Project Folder --> servlet -->Sample.java and put the listener tag in Web.xml as below .
am i doing wrong?? what should i write then? kindly help me ?

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42285
    
  64
The web.xml does not have an opening "web-app" tag, or is that a copy/paste mistake? If so, post the entire web.xml.
Priyanka Dande
Ranch Hand

Joined: May 14, 2012
Posts: 53
Here is my Web.xml
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42285
    
  64
Interesting. The error message talks about a problem in line 28 - you have far fewer than 28 lines. Something is amiss here.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

I do not see any problem with your web.xml. Can you perhaps try deleting the Tomcat caches and deploy your web app once again freshly?
Priyanka Dande
Ranch Hand

Joined: May 14, 2012
Posts: 53
from where i have to delete tomcat caches? Kindly let me know
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42285
    
  64
So your web app has only the servlet context listener? No servlets at all?
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

Priyanka Dande wrote:from where i have to delete tomcat caches? Kindly let me know


TOMCAT_HOME/work/Catalina/localhost and inside this directory, you should see your web application listed. Delete it completely and try re-deploying your app!
Priyanka Dande
Ranch Hand

Joined: May 14, 2012
Posts: 53
i have deleted the cache as per your suggestion Mr. Joe but still it is giving me error
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16145
    
  21

Um.

Let's back up and make sure everyone knows what they are talking about. Too many assumptions seem to be flying about.

FIRST. To run a servlet, you need a WAR. Forget all this stuff about directory-this and file-that. Think of the webapp as a unit, since that's what it is in the J2EE standard. The WAR has a certain structure and certain components that must be properly set up. The directories and files are simply how the WAR appears in "exploded" form. A J2EE-compliant WAR is a special form of JAR file, which, in turn is a special form of ZIP file. If the WAR is malformed, it may not deploy, and even if it does deploy, it may not run properly.

I'm not even going to talk about IDEs. IDEs can really mess things up here, so I'm going to talk stand-alone Tomcat and how it works.

When running Tomcat, WARs can live anywhere in the filesystem that there's no conflict. Meaning, for example, that I wouldn't keep a WAR file under TOMCAT_HOME/logs, for example. The default location, however, is the TOMCAT_HOME/webapps directory. A WAR can be in standard J2EE format (zipped) or it can be exploded. You can map a URL context path to that war by using a Tomcat Context element and that can be located in several different places, one of which in a file with the same name as the context plus ".xml" and located in the TOMCAT_HOME/conf/Catalina/localhost directory. The official term for a Context is "server-specific Deployment Descriptor". It's paired with the "server-independent Deployment Descriptor", better known as WEB-INF/web.xml to define how the webapp will be deployed.

If you do not define an explicit Context, one will be synthesized. If you drop a WAR into the TOMCAT_HOME/webapps directory, the synthesized Context will have a context name that's the same as the WAR name. By default, if you drop a WAR file into TOMCAT_HOME/webapps, Tomcat will automatically explode it, creating a directory with the same name as the WAR file (minus the ".war") to hold the exploded contents. Or you can simply copy or explode a directory into the webapps directory yourself as long as you follow the same structural constraints.

Here comes the tricky part. You dropped in a WAR file. Tomcat exploded it to make a WAR directory. What happens next?

It's pretty intuitive that the exploded WAR is the copy that will be executed, but what about when you drop in an updated WAR file? Answer: nothing. The exploded WAR is the definitive copy, so to get it to update, you'd have to delete it and let Tomcat explode the new WAR file (or explode it yourself). Tomcat will ignore a WAR file if there's an exploded WAR of the same name, even if the WAR file is newer.

For best results, then, do the following when deploying a new version of a WAR:

1. Delete the WAR directory
2. Delete the contents of TOMCAT_HOME/work
3. Delete the contents of TOMCAT_HOME/temp
4. Delete the contents of TOMCAT_HOME/logs

Do this while Tomcat is stopped. If you prefer not to perform wholesale destruction on the other directories, you can delete just their subtrees applying to the webapp you're developing, but there's never going to be anything in there that cannot be recreated automatically as the app runs, so it's just as easy to clear them entirely.

NOW that we have the what's-where covered, for a specific servlet in the WAR:

The mapping of an incoming servlet to a URL is done in stages, which I explained the other day in the Tomcat forum. You have the protocol/server/port part (http://www.javaranch.com:8080), the context path part ("/mywebapp") defined by the Context, as described about (this tells which webapp within the server handles the request), and the servlet-mapping part.

Servlets are mapped in the web.xml via a 2-level abstraction. First you map the URL path component to a logical servlet name, then you map the logical servlet name to a specific servlet. The servlet is identified by its fully-qualified classname, meaning that "MyServlet" isn't enough. It would be something like "com.javaranch.demoapp.MyServlet", if the package name is com.javaranch.demoapp, and as always in Java (even in Windows), upper/lower case MUST match exactly and you don't include the ".class" or ".java" on the classname, since Java already knows about that part.

So given a Servlet URL mapping of "/growflowers" in web.xml mapped to a logical servlet name of "Test App" (spaces are OK here), mapped to "com.javaranch.demoapp.MyServlet", all bundled under a context (WAR) pathname of "/mywebapp", the URL that should invoke the servlet would be in the form:



Customer surveys are for companies who didn't pay proper attention to begin with.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42285
    
  64
Did you rename directories? Earlier your web app was named "Project Folder", but now it seems to be either that or "Material-Issue". Are those two different web apps, and both are causing problems?

In any case, I advise to shut down Tomcat (not just the web app), and restart it. And I still think you should investigate why Tomcat thinks your web.xml has an error in line 28 when what you posted has only 14 lines.
Priyanka Dande
Ranch Hand

Joined: May 14, 2012
Posts: 53
Hi all, i have installed logging.properties in my Project Folder -->WEB-INF-->classes -->logging.properties and also made changes to my Web.xml Tomcat is giving me below error kindly guide me to resolve my error
This is the error log from localhost My Project Folder name is Material-Issue and i have made a ServletContextListener Class i am able to compile it and 2 .class files are created which i put in my Project Folder i. e Material-Issue -->Servlet-->Sample.java here .class files are also present



also my Web.xml file from my Project Folder is my web.xml is correct What is the problem i am not able to figure out ?

Priyanka Dande
Ranch Hand

Joined: May 14, 2012
Posts: 53
This is my Sample.java Class
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42285
    
  64
The error message seems pretty clear:

java.lang.ClassNotFoundException: Material-Issue.servlet.Sample

The Java code you posted does not have any package statement, so its package is not "Material-Issue.servlet" as you have declared it in web.xml. Assuming that you meant for it to be in package "servlet", it should have "package servlet;", and would need to be declared as "servlet.Sample" in your web.xml
Priyanka Dande
Ranch Hand

Joined: May 14, 2012
Posts: 53
thanks for your prompt response i have added package servlet in my Sample. java and also made changes to Web.xml file but still it is giving me error please help

error log


My web.xml is


Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42285
    
  64
Apparently the class file that was compiled with the new package statement was not deployed. Where is that class file? It should be in WEB-INF/classes/servlet.
Priyanka Dande
Ranch Hand

Joined: May 14, 2012
Posts: 53
I have put the servlet folder in my Project Folder i.e Material-Issue-->servlet-->Sample.java , (Sample.class, Sample$MyTimerTask.class) two class files are created after compiling and also shifted this servlet folder in Material-Issue-->WEB-INF-->classes-->servlet
but still getting error


My Web.xml is

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42285
    
  64
You still have the wrong class file - the one you're using was compiled without the package statement. As I suggested, delete all class files from everywhere and then recompile and redeploy.
Priyanka Dande
Ranch Hand

Joined: May 14, 2012
Posts: 53
thank you very much for your valuable help i am able to run my application now thank so much
 
 
subject: servlet not able to run