Skip to main content
edited tags
Link
Greg Kopff
  • 16.7k
  • 13
  • 57
  • 80
removed unnecessary thanks
Source Link

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

Thanks!

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

Thanks!

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

Source Link

Java Servlet Web XML URL Mapping

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

Thanks!