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

1 Answer 1

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:

Sign up to request clarification or add additional context in comments.

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.