users@glassfish.java.net

Re: Servlets 3.0: Default servlet hides static resources

From: <glassfish_at_javadesktop.org>
Date: Thu, 28 Jan 2010 07:00:09 PST

I use annotations instead of web.xml, but you are correct with your assumptions, my servlet is mapped to "/".

Mapping to "" solved the problem, but revealed another problem:
When mapping a servlet to "", the container doesn't seem to accept POST requests to this servlet. The doGet() method is always invoked, and any parameters are stripped away.

Here's a servlet and html file that reproduces the problem:

[b]#### ee6test.TestServlet ####[/b]

package ee6test;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

@WebServlet("")
public class TestServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        dump("doGet", request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        dump("doPost", request, response);
    }

    private void dump(String method, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/plain");
        response.getWriter().println(method + "\n");
        response.getWriter().println("HEADERS:");
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();
            response.getWriter().println(name + ": " + request.getHeader(name));
        }

        response.getWriter().println("\nPARAMETERS:");
        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String name = parameterNames.nextElement();
            response.getWriter().println(name + ": " + request.getParameter(name));
        }
    }
}

[b]#### test.html ####[/b]

<html>
  <body>

  <form action="/EE6Test_war_exploded" method="GET">
      <fieldset>
          <legend>GET</legend>
          <input type="text" name="test" value="test"/>
          <input type="submit"/>
      </fieldset>
  </form>

  <form action="/EE6Test_war_exploded" method="POST">
      <fieldset>
          <legend>POST</legend>
          <input type="text" name="test" value="test"/>
          <input type="submit"/>
      </fieldset>
  </form>

  </body>
</html>
[Message sent by forum member 'toker5' (java.net_at_reinseth.org)]

http://forums.java.net/jive/thread.jspa?messageID=383486