Skip to content Skip to sidebar Skip to footer

How Can I Access The Html Form Name From The Java Servlet?

I am facing an issue where a single java servlet needs to handle multiple HTML forms. So, I thought of using Form names in the HTML to pass it on to the server. Like, Form 1: form

Solution 1:

Form name is not sent with the HTTP request on submit, thus it cannot be used on the server side.

Consider adding hidden field with the same name but different values in both forms:

<formmethod="POST"action='Controller'><inputtype="hidden"name="type"value="form1" /><!-- ... --></form><formmethod="POST"action='Controller'><inputtype="hidden"name="type"value="form2" /><!-- ... --></form>

And in your servlet:

request.getParameter("type");

Solution 2:

Basics:

When we submit a form all the values of that form will only get submitted.. So even if you are having two forms and you are submitting one of them, then only parameters of submitted form will be available to your controller.

<formmethod="POST"action='Controller'name="form1"><inputtype="text"name="type"value="form1Text" /><inputtype="text2"name="type"value="form1Text2" /><inputtype="submit"/><!-- ... --></form><formmethod="POST"action='Controller'name="form2"><inputtype="text"name="type"value="form2Text" /><inputtype="text2"name="type"value="form2Text2" /><inputtype="submit"/><!-- ... --></form>

Pressing Submit button with submit the respective form.

Post a Comment for "How Can I Access The Html Form Name From The Java Servlet?"