deepak
deepak

Reputation: 133

How to properly map Servlet?

I want to map my servlet properly 1)i want it should come to that servlet when url is like "show.jsp/anything" i.e when user enter "show.jsp/"+anystring consisting of "alphabets and numbers" but not "symbols" i.e when user enter "show.jsp/1232342$3=4" it should throw an error my Mapping

<servlet>
        <servlet-name>show</servlet-name>
        <jsp-file>/show.jsp</jsp-file>
 </servlet>
<servlet-mapping>
        <servlet-name>show</servlet-name>
         <url-pattern>/show.jsp/*</url-pattern>
</servlet-mapping>

when user enters "show.jsp/asd%&hdfsjf" it should go to another url i.e what should i write in place of

<url-pattern>/show.jsp/*</url-pattern>

Upvotes: 0

Views: 58

Answers (1)

Serge Ballesta
Serge Ballesta

Reputation: 149185

It may not be the expected answer, but you will have to use what Java EE offers. The Servlet 3.0 specification says :

In the Web application deployment descriptor, the following syntax is used to define mappings:

  • A string beginning with a / character and ending with a /* suffix is used for path mapping.
  • A string beginning with a *. prefix is used as an extension mapping.
  • The empty string ("") is a special URL pattern that exactly maps to the application's context root, i.e., requests of the form http://host:port//. In this case the path info is / and the servlet path and context path is empty string ("").
  • A string containing only the / character indicates the "default" servlet of the application. In this case the servlet path is the request URI minus the context path and the path info is null.
  • All other strings are used for exact matches only.

That mean that you cannot do what you want, except that you can use the fact that the container will use :

  • first an exact match
  • next the longest match

For example, if you want to map /show.jsp/asd to a servlet and /show.jsp/* to another one, it is possible. But there is no way to distinguish the type of the characters.

It does not match what you want, but it is the way it works ...


If you really want to do that, you will have to develop a relay servlet mapped to `/show.jsp/*'. It will analyze the PathInfo string and forward to one of the other (unmapped) servlets depending if it contains or not non alphanum characters.

In web.xml :

<servlet>
        <servlet-name>relay</servlet-name>
        <servlet-class>distinguished.classof.Relay</servlet-class>
 </servlet>
<servlet>
        <servlet-name>show</servlet-name>
        <jsp-file>/show.jsp</jsp-file>
 </servlet>
<servlet>
        <servlet-name>other</servlet-name>
        <jsp-file>/other.jsp</jsp-file>
 </servlet>
<servlet-mapping>
        <servlet-name>relay</servlet-name>
         <url-pattern>/show.jsp/*</url-pattern>
</servlet-mapping>

Relay :

public class Relay extends HttpServlet {
    static final String SHOW = "show";
    static final String OTHER= "other";
    private Pattern pattern;


    @Override
    protected void service(HttpServletRequest hsr, HttpServletResponse hsr1) throws ServletException, IOException {
        String info= hsr.getPathInfo();
        RequestDispatcher dispatcher = getServletContext.getNamedDispatcher(
                pattern.matcher(info).matches() ? SHOW : OTHER);
        dispatcher.forward(hsr, hsr1);
    }

    @Override
    public void init() throws ServletException {
        pattern = Pattern.compile("\\w*");
    }
}

Upvotes: 1

Related Questions