0

I've been struggling with this web xml file for Tomcat for a while.

<context-param>
    <param-name>name</param-name>
    <param-value>Bob</param-value>
</context-param>

<servlet>
    <servlet-name>test</servlet-name>
    <servlet-class>TestServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>test</servlet-name>
    <url-pattern>/servlet/*</url-pattern>
</servlet-mapping>

I know that this file is being read because when i test using

public class TestServlet extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse res){
    res.setContentType("text/html");

    String name = getServletContext().getInitParameter("name");

    PrintWriter out = null;
    try{
        out = res.getWriter();
    }catch(Exception e){}

    out.println("<html><head></head><body><img src=\"/twitter.png\"><p>Hi my name is " + name + "</p></body></html>");
}

}

I am able to read the name I put into the context-param. My question for you guys is how do I create a URL mapping such that I do not have to go through /servlet/ to access my servlets in the URL? When I try to make a url-pattern such as

/test/*, i cannot access the servlet even if i say website/test/TestServlet. I get a 404 Not Found error from the browser

Greg Kopff
  • 15,945
  • 12
  • 55
  • 78
boxbag
  • 1
  • 3

1 Answers1

1

Put the servlet class in a package and do not rely on InvokerServlet. It's disabled since Tomcat 5.5 and removed in Tomcat 7.0.

Please do not read 10~15 year old tutorials/books. Technology changes every year.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Hey BalusC, thanks for the prompt response. I have another question after I got servlet 3.0. When I tried @WebServlet("/MyServlet"), how do I access my servlet? Do I go to website/servlet/MyServlet or can I access it with website/MyServlet? – boxbag Apr 29 '12 at 07:43