0

I am making a simple servlet for hello world to check that servlet is working properly or not but after making servlet and mapping web.xml file when I run the servlet it gives me an error.

enter image description here

My code:

enter image description here

Web.xml enter image description here

Please help me to solve my problem ??

チーズパン
  • 2,752
  • 8
  • 42
  • 63
Rehmat Sayany
  • 67
  • 1
  • 2
  • 12

6 Answers6

10

I came across the same problem. Removing any reference to the servlet in the web.xml solved the issue (these mappings are already defined in your servlet class with annotations)

Effie Makri
  • 399
  • 1
  • 4
  • 12
3

Follow this steps :

  1. Right click on "Tomcat v8.0 Server at localhost", Click the delete option.
  2. Click on the link "No servers are available. Click this link to create a new server" that will be provided in the Servers tab. A box will open.
  3. Now click the blue link "Configure runtime environments", a box will open. Click the apache tomcat server and press the "Remove" button. Apply changes.
  4. Then add the server you are using (v8.0) and click the finish button.

Now run the program. Hope it helps :)

Dipen Gajjar
  • 1,338
  • 14
  • 23
2

Try this, Right-click on your Server and select clean tomcat work directory, it will stop automatically to clean the tomcat work directory and starts by itself.

It worked for me

Anne Lingesh
  • 65
  • 1
  • 9
1

The problem occurs because your web.xml is referencing the servlet annotated with @WebServlet annotation.

If you want to register your servlet using deployment descripter(web.xml), you have to remove @WebServlet annotation in your servlet class.

@WebServlet(
    urlPatterns = { "/HelloWorld" },
    initParams = {
            @WebInitParam(name = "name", value = "helloworld")
    })

Or delete <servlet> and <servlet-mapping> defined in web.xml in order to use @WebServlet.

<servlet>
    <servlet-name>HelloWorld</servlet-name>
    <servlet-class>com.jsp.first.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>HelloWorld</servlet-name>
    <url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>

And it should work.

J.doe
  • 11
  • 3
1

I cloned a default created Servlet and forgot to rename @WebServlet("/MyServlet"). After I renamed that, it worked for me. :D

bvg
  • 146
  • 13
0

One reason you get this error is "In the very beginning" when you start creating your project. When you open File>>New>>"Dynamic Web Project"

On "Dynamic Web Project" Pop-up window: Target Runtime: Apache Tomcat v7.0 or Apache Tomcatv8.0

Dynamic web module version: "IT SHOULD BE SET 2.5" if its above "2.5", you will get Tomcat error because latest versions support annotations and don't need web.xml descriptor file which you will attach in next step.

Keep Configuration to: custom

Keep everything else same. Do check the web.xml checkbox before hitting Finish.

Hope this helps.

Vishal Sharma
  • 175
  • 1
  • 3
  • 8