Claude
Claude

Reputation: 1784

How to redirect pure servlet url to the same servlet with default parameters?

We have a web application provided by an external supplier. There is a servlet which - if called without parameters - raises an exception. We have access to web.xml.

Is it possible to redirect the servlet url http://<server>:8080/<app>/<servlet> to the same servlet with default parameters, e.g. to http://<server>:8080/<app>/<servlet>?p1=0?

I know that a redirect is possible using Apache and .htaccess using the rewrite engine. However, I wonder if there are other means relying on tomcat only.

Upvotes: 0

Views: 1031

Answers (3)

Claude
Claude

Reputation: 1784

Just for the sake of completeness. Based on the answer from Nikos Paraskevopoulos I found a simpler solution:

public class MyFilter implements Filter
{
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
    {
        if (request instanceof HttpServletRequest && response instanceof HttpServletResponse)
        {
            HttpServletRequest httpRequest = (HttpServletRequest) request;
            HttpServletResponse httpResponse = (HttpServletResponse) response;
            if ( ! httpRequest.getParameterNames().hasMoreElements())
            {
                String redirectURL = httpRequest.getRequestURL() + "?p1=0";
                httpResponse.setStatus(404); // Not found
                httpResponse.sendRedirect(redirectURL);
                return;
            }
        }
        chain.doFilter(request, response);
    }
    ... // implement other interface methods
}

Upvotes: 0

Nikos Paraskevopoulos
Nikos Paraskevopoulos

Reputation: 40318

For the filter solution that you seem to prefer, you first need an implementation of HttpServletRequestWrapper that knows about your default values. It could be as:

public class SpecializedHttpServletRequestWrapper extends HttpServletRequestWrapper {

    private static final Map<String,String> DEFAULT_VALUES = new HashMap<>();
    static {
        Map<String,String> defaultValues = new HashMap<>();
        defaultValues.put("p1", "0");
        // add other defaults here...
        DEFAULT_VALUES = java.util.Collections.unmodifiableMap(defaultValues);
    }

    public SpecializedHttpServletRequestWrapper(HttpServletRequest request) {
        super(request);
    }

    public String getParameter(String name) {
        String result = super.getParameter(name);
        if( name == null && DEFAULT_VALUES.containsKey(name) ) {
            result = DEFAULT_VALUES.get(name);
        }
        return result;
    }

    public Map<String,String[]> getParameterMap() {
        // needs implementing!
    }

    public Enumeration<String> getParameterNames() {
        // needs implementing!
    }

    public String[] getParameterValues(String name) {
        // needs implementing!
    }

    ...
}

The implementation of the unimplemented methods above is more complicated, but still doable.

Then the filter code would simply do:

void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    chain.doFilter(new SpecializedHttpServletRequestWrapper(request), response);
}

Upvotes: 1

worpet
worpet

Reputation: 3893

There is not a way to force a redirect solely via a configuration file, as is possible with Apache. Using Tomcat only you would have to do this via Java code (write a Filter or Servlet to add the missing parameter values or to redirect to a URL that contains the paramters).

Upvotes: 0

Related Questions