0
votes

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

1

1 Answers

1
votes

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: