Two forms but only 1 jsp file

This is what happens to me. I have one .jsp file. However, I have two forms with several inputs inside these forms.

What is the best way to discover that one form has been presented but not the other? Here is 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 this button is pressed, I want 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, I would have something similar, but it would have seemed a different form. Thanks!

+4
source share
5 answers

Give the submit button a unique name. It becomes the name of the request parameter. That way you can just check if the HttpServletRequest#getParameter() null .

eg.

 <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 a specific problem, business logic does not belong in JSP, but in Servlet . I will also start working on this. This allows you to submit forms to different URLs. In addition, for this you should use the POST method, not the GET method.

+5
source

You can pass another parameter to the action, for example action = "index.jsp? Form = userlogin" and something else for another

+2
source
 <input type='hidden' name='formNumber' value='form2' /> 
+1
source

You add a hidden variable with some value to the list of variables of the form field in one of the forms. And in the JSP, check if this value is populated or not. If filled means the first form will be sent, otherwise the second form will be sent. For instance:

 <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> 
0
source

Copied this from another question, hope it helps

You can just call your servlet method using javascript like

in the submit button, I just use the 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 invoke a single servlet with several methods in javascript by specifying the tag name as shown below.

  <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> 
-1
source

Source: https://habr.com/ru/post/1434982/


All Articles