Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSP - check form submission

Let say I have a simple form with no required fields:

<form action="index.jsp" method="post">
    <input type="text" name="firstName" />
    <input type="text" name="lastName" />
    <input type="text" name="email" />

    <input type="submit" value="submit" />
</form>

I want to check if the form was submitted by checking the submit parameter (because it's always present). In PHP I can do a simple

if ( $_POST['submit'] )

but the request.getParameter("submit") doesn't seem to work.

So what's the best way to check if a form was submitted?

like image 391
Zoltan Toth Avatar asked Dec 13 '22 05:12

Zoltan Toth


2 Answers

You need to give the input element a name. It's the element's name which get sent as request parameter name.

<input type="submit" name="submit" value="submit" />

Then you can check it as follows:

if (request.getParameter("submit") != null) {
    // ...
}

You perhaps also want to check if "POST".equalsIgnoreCase(request.getMethod()) is also true.

if ("POST".equalsIgnoreCase(request.getMethod()) && request.getParameter("submit") != null) {
    // ...
}

Better, however, would be to use a servlet and do the job in doPost() method.

like image 184
BalusC Avatar answered Jan 01 '23 06:01

BalusC


You can try this way:-

if ("POST".equalsIgnoreCase(request.getMethod())) {
    // Form was submitted.
} else {
    // It may be a GET request.
}
like image 29
Siva Charan Avatar answered Jan 01 '23 06:01

Siva Charan