4

When I use the following web.xml, my project runs fine and I can see "Hello World" getting displayed via index.jsp page. I am using Netbeans 7.4 and Apache tomcat 6.0.41

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
 </web-app>

However, when I created my own servlet and use the following web.xml,=

 <?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>

 <servlet>
    <servlet-name>TEST_Authenticate</servlet-name>
    <servlet-class>restapi.TEST_Authenticate</servlet-class>
</servlet>  
<servlet-mapping>
    <servlet-name>TEST_Authenticate</servlet-name>
    <url-pattern>/TEST_Authenticate</url-pattern>
</servlet-mapping>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>


    <scope>provided</scope>
</dependency>

I end up getting the following error in the Apache Tomcat Log:

Jul 24, 2014 9:36:14 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet TEST_Authenticate
java.lang.ClassNotFoundException: TEST_Authenticate
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1128)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:827)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:129)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:744)

I have checked the following post but still getting the errors.

Project Structure :

enter image description here

Community
  • 1
  • 1
John
  • 1,210
  • 5
  • 23
  • 51

2 Answers2

6

First of all remove

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
</dependency>

from the web.xml and put it in your pom.xml if it is a Maven project.

Secondly, ClassNotFoundException is thrown when the class cannot be found in the specified location. In your case, as you have

<servlet-class>restapi.TEST_Authenticate</servlet-class>

So, I am assuming that you have a package of name restapi and under which you have the class TEST_Authenticate.

So, if it is a Maven project you should have scr/main/java/yourpackagename/yourClass and if it is a simple web-project then it should be like src/yourpackagename/yourClass.

diogo
  • 3,769
  • 1
  • 24
  • 30
SparkOn
  • 8,806
  • 4
  • 29
  • 34
  • Yup, removed the `` tag completely which removed the red cross mark over `web.xml`. Yes, `restapi` is a package as shown in the structure image in my question. It's a web project so what do you eman by `src/yourpackagename/yourClass` ? – John Jul 25 '14 at 04:24
  • how you are trying to access the servlet? refer [this example](http://www.studytonight.com/servlet/creating-servlet-in-netbeans.php) to run a servlet you will get to know what you are missing – SparkOn Jul 25 '14 at 04:27
  • 1
    I think you are right. I was accessing servlet in a different manner. When I did what's mentioned in the link after creating fresh web project, although I got an error in Apache Tomcat Log `java.io.IOException: Server returned HTTP response code: 500 for URL: http://myurl.com` the URL is the content of my `TEST_Authenticate` servlet. So, it's accessing my servlet now. Figuring out that problem should be my next step now. Thanks ! – John Jul 25 '14 at 04:45
1

You haven't closed the <web-api> in web.xml using /web-api> at the end. SO,it might be giving error. Also,it is better to keep <session-config> after the servlet-mapping parameters.

Just try the following piece of code in web.xml(COPY-PASTE my code in your web.xml) :-

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.1"  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee  http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">

<welcome-file-list>
<welcome-file>restapi/TEST_Authenticate.java</welcome-file>   
</welcome-file-list>

<servlet>
<servlet-name>TEST_Authenticate</servlet-name>
<servlet-class>restapi.TEST_Authenticate</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>TEST_Authenticate</servlet-name>
<url-pattern>/TEST_Authenticate</url-pattern>
</servlet-mapping>

<session-config>
<session-timeout>30</session-timeout>
</session-config>

</web-app>

EDIT --->

By default,the welcome page is set to index.jsp in a web-application project in NetBeans IDE. So,if you don't specify anything even then it'll search for that file only and open it. If you want to open a specific page,edit the

<welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list>

and replace the login.jsp with your_required_jsp_page.

I hope it answers your question. Feel free to comment if it doesn't help.

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
  • Thanks shekhar. I already had the closing tag for `web.xml` but I forgot to include it in my question. Right now I don't know why, I am getting directed to `http://localhost:8080/testapi/` URL on my browser and it's executing the `index.jsp` page and displaying `Hello World` on the browser. Overall, my servlet is not executing.Do you know why? – John Jul 25 '14 at 03:39
  • @John-I request you to clean and build the project afresh in netbeans using the option provided in netbeans! Please comment after doing this and executing the project. – Am_I_Helpful Jul 25 '14 at 03:48
  • Now the web browser isn't opening however the build is successful.Not sure why but I still have red cross mark over my `web.xml` – John Jul 25 '14 at 03:54
  • I meant to say that when I was deploying the project, I was getting Hello World in the web browser before which is not happening now.Yes, it matches exactly same as you mentioned. – John Jul 25 '14 at 03:56
  • When I hover over the `web.xml` I see an error message `Invalid content was found starting with element dependency and some file path` – John Jul 25 '14 at 03:59
  • When I commented out <`dependency> tag`, I am again getting HelloWorld on my browser. – John Jul 25 '14 at 04:01
  • I suggest you deleting the contents of `web.xml` and again copy-pasting the contents from this answer! ALso,it'll always display index.jsp! Because that's the default welcome-list of any web-application project! – Am_I_Helpful Jul 25 '14 at 04:01
  • Did that but it's still taking me to Hello World page. – John Jul 25 '14 at 04:04
  • Check my edited answer! It'll answer your query,READ THE EDITED SECTION below the web.xml file configuration.I hope you get it! – Am_I_Helpful Jul 25 '14 at 04:07
  • @John-Did you get it?Please comment as to I would elaborate! – Am_I_Helpful Jul 25 '14 at 04:12
  • I haven't found solution of my problem yet. – John Jul 25 '14 at 04:16
  • You simply replace content with your servlet name,here `restapi.TEST_Authenticate`! – Am_I_Helpful Jul 25 '14 at 04:16
  • When I added that, it's taking me to `http://localhost:8080/testapi/` and showing following on browser: `HTTP Status 404 -/testapi/ ; type Status report;message /testapi/;description The requested resource is not available ;Apache Tomcat /6.0.41` – John Jul 25 '14 at 04:21
  • @John-Also,remove out that `` tag from web.xml that is of no use here.And,check the edited answer.HOW to add servlet to welcome-file.Please re-run and comment after updating the change in welcome-file-list(see my edited web.xml)-->Clean and build-->Re-execute. – Am_I_Helpful Jul 25 '14 at 04:22
  • @John-Do as `restapi/TEST_Authenticate` in the welcome-file as here comes a directory thing!You need to be careful with that! – Am_I_Helpful Jul 25 '14 at 04:25