wood burning stoves 2.0*
The moose likes Servlets and the fly likes can't seem to get my java servlet to work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "can Watch "can New topic
Author

can't seem to get my java servlet to work

Devin Crane
Ranch Hand

Joined: Aug 01, 2012
Posts: 32
The default testing page for Tomcat7 works just fine, but I can't seem to get my java servlet to work. I get the following errors:

HTTP Status 404 - /hello/hello
type Status report
message /hello/hello
description The requested resource (/hello/hello) is not available.
Apache Tomcat/7.0.26


I attempt to access my servlet with this address: http://localhost:8080/hello/hello

web.xml is as follows:



my java file compiles with this command:
javac -cp '/usr/share/tomcat7/lib/servlet-api.jar' ./test/HelloServlet.java


and my java file is as follows:



I would greatly appreciate any help; I can't think of anything else to look at. I'm pretty sure my JAVA_HOME variable has been set correctly.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

I assume that your web app is rooted at webapps/hello in the Tomcat folders?

Have you verified that the app has been deployed via Tomcat's manager application?

Have you gone through the steps shown in the ServletsFaq to debug?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Devin Crane
Ranch Hand

Joined: Aug 01, 2012
Posts: 32
I'm not sure what actually qualifies as my 'web app', or what Tomcat's manager application is.

This is my folder hierarchy: '/var/lib/tomcat7/webapps/hello/WEB-INF/classes/test'.
'WEB-INF' contains my web.xml.
'test' contains my HelloServlet.class, HelloServlet.java, and HelloServlet.java.save, though I'm not sure where the .java.save came from.
I also have a war file in both my webapps and hello directory b/c I wasn't sure where exactly it was supposed to go.

I wasn't aware of the ServletsFaq; I'll take a look at it.
My JAVA_HOME environment variable is currently set at '/usr/bin/jvm/java-openjdk'.

Thanks,
Devin
Prajakta Acharya
Ranch Hand

Joined: Nov 08, 2012
Posts: 136

Can you verify that Tomcat is listening to 8080 port? Does http://localhost:8080/ open up tomcat home page?
If yes, can you put an index.jsp in hello folder and check? Hope your web.xml has index.jsp cpnfigured as welcome page.


Regards,
Prajakta
Devin Crane
Ranch Hand

Joined: Aug 01, 2012
Posts: 32
http://localhost:8080/ does indeed open up the tomcat home page.
I do not currently have an index.jsp in the hello folder; I'm looking up how to make one. Is there any particular format to it?
Also, how do I change my web.xml to have index.jsp configured as welcome page?

Thanks,
Devin
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

index.jsp is in the default list of welcome files. You don't need to do anything.

Here's the index.jsp I use to simply test if the app is deployed:


It should simply display 7.
Devin Crane
Ranch Hand

Joined: Aug 01, 2012
Posts: 32
Awesome. And how do I access index.jsp? Nothing I've tried so far works.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

Devin Crane wrote:I'm not sure what actually qualifies as my 'web app', or what Tomcat's manager application is.

These are things you need to know.

What version of Tomcat are you using?

This is my folder hierarchy: '/var/lib/tomcat7/webapps/hello/WEB-INF/classes/test'.

That should cause your web to be deployed with a context path of /hello.

I would strongly suggest using different names for your servlet path and context path. Making them both /hello is just asking for confusion (though it should work).

'WEB-INF' contains my web.xml.
'test' contains my HelloServlet.class, HelloServlet.java, and HelloServlet.java.save, though I'm not sure where the .java.save came from.

Remove everything but the class files. The other files are irrelevant. Keep your source files outside the web app.

Superflous files and code are just sources of confusion.

Let us know what you find after you go through the checklist in the ServletsFaq.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

Devin Crane wrote:Awesome. And how do I access index.jsp? Nothing I've tried so far works.


You need to make sure that the app is deployed. That's where the Manager app comes in. I can point you to a document that describes how to set it up for Tomcat 6. I'm pretty sure it hasn't changed much for Tomcat 7, but am not 100% sure.

The welcome file should be triggered by http://localhost:8080/hello/ when all is said and done.

By the way, have you checked the logs for any errors?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

Document for setting up Tomcat 6 manager:
  • Go to jQuery in Action (2nd Ed) (yeah, this is not about jQuery)
  • Download the source zip and unpack.
  • Look for tomact.pdf in the chapter8 folder.


  • This is a document I wrote to help readers of my book set up Tomcat.
    Devin Crane
    Ranch Hand

    Joined: Aug 01, 2012
    Posts: 32
    Would you mind defining a few things for me, unless you really insist on looking things up on my own. It would save me time and extra headache.

    webapp?
    manager application?
    servlet and context path?
    How do i configure web.xml for index.jsp?

    http://localhost:8080/hello/ is not opening index.jsp.
    I appreciate the document link.

    FAQs results:

    * Make sure that your servlet class is in a package other than the default. This is not optional. As of JDK 1.4, classes in the default package cannot be imported and it can cause issues with deploying the servlet. Package your servlet class! In fact, package all of your classes -- it's a good practice.
    Yes - see my code above; package = test

    * Make sure that the servlet class is properly placed under the WEB-INF/classes hierarchy, or properly packaged in a jar file in WEB-INF/lib.
    It's in my test folder, which is in my classes folder

    * Make sure that the servlet is properly declared in the deployment descriptor. The full package path to the class must be used.
    Assuming 'servlet' means 'servlet class', I don't know what 'deployment descriptor' means.

    * Make sure that there is a servlet mapping for the servlet in the deployment descriptor. The name must match the servlet declaration and the path must start with a /.
    Not fully sure I understand this, but I think there is and does.

    * Make sure that the URL that you are using starts with the context path of the web application, which should be followed by the servlet mapping. For example: "/context-path/servlet-path". Using page-relative addressing in an anchor tag, or as a form action, is almost guaranteed to cause issues. Even if you can get it to work, it is fragile and can break with the least little of changes.
    It does, I believe, but I don't know what servlet-path means. The example I used had them both the same name.
    Devin Crane
    Ranch Hand

    Joined: Aug 01, 2012
    Posts: 32
    I have not checked the logs for errors, trying to find where they're stored.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60048
        
      65

    logs/catalina.out
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60048
        
      65

    Devin Crane wrote:webapp?

    Also called a context. The app deployed in a folder with a WEB-INF. The folder containing WEB-INF is the context root

    manager application?

    A Tomcat-provided web app that shows the deployed contexts, and allows you to manage them.

    servlet and context path?

    All explained in the ServletsFaq.

    How do i configure web.xml for index.jsp?

    Already answered. You don't. It's a default.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60048
        
      65

    Devin Crane wrote:servlet and context path?


    I have enhanced the FAQ entry to further define these terms.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60048
        
      65

    If you are using Tomcat 7, here's the instructions to set up the Manager application.
    Devin Crane
    Ranch Hand

    Joined: Aug 01, 2012
    Posts: 32
    Thank you for those clarifications. They were helpful. Near as I can tell, I'm doing all of them.
    I've now changed my web.xml to the following, changing the servlet-path a bit:



    So now I access it all with this web address:
    http://localhost:8080/hello/helloServlet
    Unfortunately I still get the same error as before.

    I found one bug from looking at the logs, but now I don't understand anything the log says. I'm not sure which major/minor version it would be talking about.

    Jul 1, 2013 2:13:43 PM org.apache.catalina.startup.HostConfig deployWAR
    INFO: Deploying web application archive /var/lib/tomcat7/webapps/hello.war
    Jul 1, 2013 2:13:43 PM org.apache.catalina.core.ContainerBase addChildInternal
    SEVERE: ContainerBase.addChild: start:
    org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/hello]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:895)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:871)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:615)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:958)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1599)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:679)
    Caused by: java.lang.UnsupportedClassVersionError: test/HelloServlet : Unsupported major.minor version 51.0 (unable to load class test.HelloServlet)
    at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2840)
    at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1160)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1668)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1546)
    at org.apache.catalina.startup.WebAnnotationSet.loadApplicationServletAnnotations(WebAnnotationSet.java:108)
    at org.apache.catalina.startup.WebAnnotationSet.loadApplicationAnnotations(WebAnnotationSet.java:58)
    at org.apache.catalina.startup.ContextConfig.applicationAnnotationsConfig(ContextConfig.java:381)
    at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:858)
    at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:345)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5161)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 11 more
    Jul 1, 2013 2:13:43 PM org.apache.catalina.startup.HostConfig deployWAR
    SEVERE: Error deploying web application archive /var/lib/tomcat7/webapps/hello.war
    java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/hello]]
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:898)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:871)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:615)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:958)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1599)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:679)


    Thanks for all your help!
    Devin
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60048
        
      65

    Ah!

    This tells you that the context never started:
    SEVERE: ContainerBase.addChild: start:
    org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/hello]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)


    This tells you why:
    Caused by: java.lang.UnsupportedClassVersionError: test/HelloServlet : Unsupported major.minor version 51.0 (unable to load class test.HelloServlet)


    The error means that you have compiled the class with a later version of Java than the container is running.

    You need to compile the class with the same version of Java that the container is using.
    Devin Crane
    Ranch Hand

    Joined: Aug 01, 2012
    Posts: 32
    Hmm, I should be running Java 7 update 9. 'Container' is not a term I understand. Is that referring to tomcat7? The servlet?
    How do I figure out which version the container is running?

    Thanks,
    Devin
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60048
        
      65

    Have you got the manager app up yet? You really need to do that before doing anything else.

    It would how that the context was not deployed, and it will show information on which Java version it is using.
    Devin Crane
    Ranch Hand

    Joined: Aug 01, 2012
    Posts: 32
    I'm working on it. I don't much enjoy reading pages like that.
    Devin Crane
    Ranch Hand

    Joined: Aug 01, 2012
    Posts: 32
    I found tomcat-users.xml, and added my own username and password to it, along with some roles (assuming I did correctly), but it's difficult to understand just what I'm supposed to do with it.
    Devin Crane
    Ranch Hand

    Joined: Aug 01, 2012
    Posts: 32
    Ok. I tried
    http://localhost:8080/manager/text/deploy?hello/helloServlet

    following this pattern:
    http://{host}:{port}/manager/text/{command}?{parameters}

    but I'm just getting the same error as always.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60048
        
      65

    Devin Crane wrote:I'm working on it. I don't much enjoy reading pages like that.


    Yeah -- the Tomcat docs aren't paragons of readability. That's why I wrote up a doc for readers of my book.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60048
        
      65

    Just try hitting: http://localhost:8080/manager/html and see if you get a display.
    Devin Crane
    Ranch Hand

    Joined: Aug 01, 2012
    Posts: 32
    Nope. Same error. Man, I make things difficult.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60048
        
      65

    Devin Crane wrote:'Container' is not a term I understand. Is that referring to tomcat7?

    "Container" is short for "servlet container" -- the part of servers that's implements the servlet capabilities.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60048
        
      65

    Tomcat 6 or 7?
    Devin Crane
    Ranch Hand

    Joined: Aug 01, 2012
    Posts: 32
    Would it help to know that I'm also getting errors in the log that files in every folder couldn't be completely deleted?
    Devin Crane
    Ranch Hand

    Joined: Aug 01, 2012
    Posts: 32
    I'm running Tomcat7.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60048
        
      65

    Hmm, later tonight if I get a chance, I'll try setting up a Tomcat 7 instance and see what's involved in getting something working. I haven't bothered yet as all my clients are still using Tomcat 6.
    Devin Crane
    Ranch Hand

    Joined: Aug 01, 2012
    Posts: 32
    I finally got it working!! Both the servlet and index.jsp. It was pointing to the wrong jdk I think; I had two versions in my jvm folder.

    I've done both
    export JAVA_HOME=/usr/lib/jvm/jdk1.7.0_09

    and
    export PATH=$PATH:JAVA_HOME/bin

    Why, despite setting the JAVA_HOME variable as such, when I type 'which java', it says /usr/bin/java? That doesn't make any sense to me.

    I've also inserted that path into dot.bash_profile and /etc/default/tomcat7. Not sure which, if either, or both of those made the difference.
    Strange that the Manager app still doesn't seem to be working though. Is something still wrong if that's not working?

    Thanks for all your help,
    Devin
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60048
        
      65

    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60048
        
      65

    P.S. You should try and get the manager app running. It's essentially useful.
    Devin Crane
    Ranch Hand

    Joined: Aug 01, 2012
    Posts: 32
    I'd be happy to, but I thought it would start working once the servlet did, and it's not. Any suggestions?

    Even http://localhost:8080/manager/html doesn't work.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: can't seem to get my java servlet to work
     
    Similar Threads
    requested servlet is not available
    HTTP Status 404 - Servlet 'x' is not available.
    Cant Run Simple Jersey REST Webservice Example
    The requested resource is not available
    HTTP Status 404 in jboss-as-7.1.1.Final (Brontes)