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.
My code:
Please help me to solve my problem ??
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.
My code:
Please help me to solve my problem ??
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)
Follow this steps :
Now run the program. Hope it helps :)
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
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.
I cloned a default created Servlet and forgot to rename @WebServlet("/MyServlet"). After I renamed that, it worked for me. :D
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.