1
votes

How can I create a new project inside tomcat webapps folder for running Servlet's?

I followed the exact pattern as mention in lots of tutorials. Even I copy whole project from webapps(default 'examples' project ) and put at same place (webapps) with different name.

But when I trying to call it from browser (http:\localhost:8080\FIRST\MyServlet), I am getting same error as HTTP Status 404 The requested resource is not available.

What am I doing wrong here, or do I have to mention name of new project in some place in tomcat ??

Can someone help me for this issue please?

I have put my Servlet file inside webapps->first->WEB-INF->classes MyServlet.class

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class MyServlet extends GenericServlet
{
    public void service(ServletRequest req , ServletResponse res)
            throws IOException, ServletException
    {
        res.getWriter().write("<html><body>Hello</body> </html>");
    }
}

and this is my web.xml file at webapps->first->WEB-INF->web.xml

web.xml

<servlet>
      <servlet-name>MyServlet</servlet-name>
      <servlet-class>MyServlet</servlet-class>
</servlet>
<servlet-mapping>
      <servlet-name>MyServlet</servlet-name>
      <url-pattern>/MyServlet</url-pattern>
</servlet-mapping>
2
put your code here, that might be helpful for us. - Killer
Map your context correctly in web.xml file. - JDGuide
@Killer I don't think that his code is related to his question - msrd0
so you think that we don't need to check his web.xml, right... ??? - Killer
@Killer I wouldn't say that web.xml is code, but it could be a usefull information, thats right - msrd0

2 Answers

1
votes

You need to say tomcat where the Servlet is. Therefore, edit your WEB-INF/web.xml file and add:

<servlet>
    <servlet-name>your-servlet</servlet-name>
    <servlet-class>your.package.servlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>your-servlet</servlet-name> <!-- the same as above ! -->
    <url-pattern>/your_servlet</url-pattern>
</servlet-mapping>

Please note that the Servlet's class should be in the WEB-INF/classes/ folder, with the complete package structure.

If you don't want to do this hardcoded, you can use Eclipse JavaEE to generate this file for you. Don't forgett to restart the server in both cases!

0
votes

The directory is right , try calling this:

http:\localhost:8080\FIRST\MyFirstServlet\

instead of

http:\localhost:8080\FIRST\MyFirstServlet