4

Here's what I've got going on. I have one .jsp file. However, I have two forms with multiple inputs inside those forms.

What is the best way to detect that one form was submitted but not the other? Here's an example: I have this form:

<form  name = "login" action="index.jsp" method="get">
Username: <input id="username" name="username" type="text"/><br/>
Password: <input id="password" name="password" type="password"/>
<input type="submit" Value="Login" ></input>
</form>

If that button is clicked, I'd like to run this code:

String username = request.getParameter("username");
String password = request.getParameter("password");
if((username!= null && !username.trim().equals("")) && (password != null && !username.trim().equals(""))) {
    DBentry DBentry=new DBentry();
    boolean flag = DBentry.isTaken(username);
    if(flag) {%><script type="text/javascript">alert("Login Successful!");</script><%
        }
    else { %><script type="text/javascript">alert("Unrecognized username.  Please register!");</script><% }
    }
else { %><script type="text/javascript">alert("Please enter both a username and password!");</script><% }

Further down I would have something exactly like it but submitting a different form. Thanks!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
joshft91
  • 1,755
  • 7
  • 38
  • 53

5 Answers5

5

Give the submit button an unique name. It becomes the request parameter name. This way you can just check if HttpServletRequest#getParameter() doesn't return null.

E.g.

<input type="submit" name="login" Value="Login" />

...

<input type="submit" name="somethingelse" Value="Something else" />

with

if (request.getParameter("login") != null) {
    // Login form submitted.
}
else if (request.getParameter("somethingelse") != null) {
    // Something else submitted.
}

Unrelated to the concrete problem, business logic doesn't belong in a JSP, but in a Servlet. I'd start working on that as well. This enables you to submit the forms to different URLs. Also, you should be using POST method for this, not GET method.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks for the response. This looks like the one most relevant to me - I'll give it a shot in a bit! Thanks again. – joshft91 Sep 19 '12 at 03:45
  • Works like a charm - should be able to work with this. In response to your note on the business log in JSP, that makes sense, but this first program is a sample program given to use by our instructor and that's how he has it laid out so I'll go with it for now. – joshft91 Sep 19 '12 at 03:53
  • You're welcome. Good luck learning JSP/Servlet. Hopefully you aren't been provided heavily outdated learning materials and you'll be put in the right direction. – BalusC Sep 19 '12 at 03:56
2

You can pass different parameter on the action for example action="index.jsp?form=userlogin" and something else for the other one

Orn Kristjansson
  • 3,435
  • 4
  • 26
  • 40
1
<input type='hidden' name='formNumber' value='form2' />
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

You add a hidden variable with some value in the form field variable list in one of the form. And in the JSP, check whether that value is populated or not. If populated means , the first form is submitted , else second form is submitted.For example:

<form  name = "login" action="index.jsp" method="get">
Username: <input id="username" name="username" type="text"/><br/>
Password: <input id="password" name="password" type="password"/>
<input type= "hidden" name="myform" value="form1"/>
<input type="submit" Value="Login" ></input>
</form>
UVM
  • 9,776
  • 6
  • 41
  • 66
-1

Copied this from another question, hope it helps

You can simply call your servlet method by javascript for example

in submit button i just use name attribute

   <li class="button-row">
  <input type="submit" value="ADD" id="add"class="btn-submit img-  swap" name="add" />
    <input type="submit" value="Delete"  class="btn-delete img-swap" name="delete" />
   <input type="submit" value="Update" class="btn-update img-swap" name="update" />
 <input type="submit" value="Search" class="btn-search img-swap" name="search" />
 </li>

you can call single servlet with multiple method in javascript by submit tag name as follows

   <script type="text/javascript">
var frm = document.forms[0];

if (request.getParameter("add") != null) {

    var pageName = "/DepartmentServlet?method=add"
    frm.action = pageName;
    frm.submit();

 }
 else if (request.getParameter("delete") != null) {
//likewise you can call your other method from DepartmentServlet
//Even you can pass parameter by onClick event
// Invoke action 2.
  }
 else if (request.getParameter("update") != null) {
// Invoke action 3.  }

 </script>
TrackmeifYouCan
  • 108
  • 3
  • 14