joshft91
joshft91

Reputation: 1885

Two forms but only 1 jsp file

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!

Upvotes: 4

Views: 26010

Answers (5)

Andrew Thompson
Andrew Thompson

Reputation: 168815

<input type='hidden' name='formNumber' value='form2' />

Upvotes: 1

BalusC
BalusC

Reputation: 1108557

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.

Upvotes: 5

UVM
UVM

Reputation: 9914

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>

Upvotes: 0

TrackmeifYouCan
TrackmeifYouCan

Reputation: 108

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>

Upvotes: -1

Orn Kristjansson
Orn Kristjansson

Reputation: 3485

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

Upvotes: 2

Related Questions