views:

233

answers:

3

Hi,

For a customer, I need to write a Servlet which can print values in a form post. The form is hosted on another server and looks somewhat like this:

<form action="http://myserver/myServlet" method="POST">
<input type="text" id="someName" value="someInterestingValue"/>
<input type="submit" value="submit" />
</form>

I have a Tomcat 5.0.28 server available, running on a Java 1.4 jdk, so I made a simple servlet:

public class ProxyServlet extends HttpServlet {
    public void doPost(HttpServletRequest req, 
                       HttpServletResponse res) 
                       throws ServletException, IOException {
     PrintWriter out = res.getWriter();  
     Enumeration a =req.getAttributeNames(); 
     while (a.hasMoreElements()){
      String attrname = (String) a.nextElement();
      out.println(attrname+"="+req.getAttribute(attrname)+"<br/>");
     }
     out.close();
    }
}

When I go to the servlet by URL everything looks as expected. When I send a GET request with some parameters, I can see those as attributes in the debugger in the doGet() method (method was left out for brevety).

However, in the doPost(), my form fields seem to be missing. I've looked into the Tomcat logfiles, and nothing special is logged. I tried to add a crossdomain.xml to some directories but did not find a way to change this behaviour.

So to be clear: The Form as listed above is on server A. My servlet runs on an existing legacy Tomcat/Java application hosted on server B. When the form is of type "POST" none of the fields arrive at the Servlet on server B. Apache is NOT "in front" of Tomcat.

Is there some configuration setting I am missing in Tomcat? Any tips or suggestions where to look next? Help is greatly appreciated.

+1  A: 

Request attributes? Don't you need to access them as request parameters using HttpServletRequest#getParameter() and so on?

Which adds more confusion is that you said that it works in the doGet(). How does its code look like then? Does it also access them as attributes? That would in a normal JSP/Servlet environment (i.e. you aren't using some filter which copies the parameters to attributes or so) have been impossible.

BalusC
A: 

This has nothing to do with cross-site. As BalusC said, use getParameter... instead of getAttribute... methods

irreputable
A: 

Found the problem. After a whole day of searching and coding, it all boils down to the fact that my code was working fine. The problem is in the form. the line:

<input type="text" id="someName" value="someInterestingValue"/>

Should read:

<input type="text" name="someName" value="someInterestingValue"/>

For people mentioning "getParameter" instead of "getAttribute" you are totally correct. In my test code I had both just to be sure (because I thought I lost it...) but both were not returning results, as the browser was simply not sending the name/value pairs.

I guess posting this on Stackoverflow did help, because I had to explain and re-read my problem I thought the "id=" looked funny. Coding is finding bugs in pieces of text you are overlooking...

Rolf
Ah, that missed my eye as well. It would've been too obvious yes. It would have helped more if you actually said that the `getParameter()` stuff also didn't work. Nevertheless glad you solved it by yourself.
BalusC
Yes, my bad, I had the getParameter() code in there originally but thought I could loose it to make the question a bit shorter.
Rolf