views:

311

answers:

1

I would like to get the name of the form used to post parameters from a Java HTTPServletRequest object. Is this possible. I don't see any method that looks like it will return it to me.

+7  A: 

HTML form name is NOT submitted as part of the request. If you need it (why?) you can submit it as hidden input instead:

<form name="myForm" action="/my_servlet">
  <input type="hidden" name="htmlFormName" value="myForm"/>
  ...

In your servlet:

String htmlFormName = request.getParameterValue("htmlFormName");
ChssPly76
I agree that it should not be needed. Recently I have encountered a situation where several pages submit to the same controller. The construction is ugly, meaning you have to know 'where you came from' in order to interpret the get/post vars. In this particular case I had to know the form name, but since it is not posted (as you said) I had to go for your suggestion (worked fine of course).Is there any lecture about posting, and why the form name is not submitted? (I mean, why not? ;))
Stefan Hendriks
@Stefan I'd guess the form name is not submitted because form itself is not an input and thus isn't associated with any value - so what's the point of submitting it? But I don't know for sure. What I do know is that "name" was originally added so forms can be addressed from javascript and that has now been superseded by "id" attribute; in fact form "name" attribute is not allowed by HTML 4.01 Strict.
ChssPly76