0

here is my code:

<form method="POST" action="">
<div class="col-lg-4 pull-right">
            <div class="form-group">
                <label for="txtUserId">ID:</label>
                <input type="text" name="txtUserId" id="txtUserId" class="form-control input-lg" />
            </div>
            <div class="form-group">
                <label for="txtUserName">NAME:</label>
                <input type="text" name="txtUserName" id="txtUserName" class="form-control input-lg" />
            </div>
            <div class="form-group">
                <label for="txtUserLastName">LastName:</label>
                <input type="text" name="txtUserLastName" id="txtUserLastName" class="form-control input-lg" />
            </div>
        </div>
</form>

First question: Can we handle post request at .jsp file? or we must get it from java class only?

Second question: how can check which button is clicked?

like: if(isset($_POST['btnSAVE']) in php.

best regards.

Saqib Omer
  • 5,387
  • 7
  • 50
  • 71
msDead
  • 35
  • 10
  • You can write a Javascript function and call onclick of the button. You should be able to do what you want in the function and then submit the form. – Basu Nov 20 '17 at 15:06
  • hmm, i think its another way , i need normal way that get post data in the function and do other stuff – msDead Nov 20 '17 at 15:15

2 Answers2

0
  1. can we handle post request at .jsp file
    • Yes. Read values sent in request from previous JSP using "request.getParameter"
  2. how can check which button is clicked
<FORM NAME="form1" METHOD="POST" Action="SomePage.jsp">
    <INPUT TYPE="SUBMIT" NAME="submit" VALUE="Button 1">
    <INPUT TYPE="SUBMIT" NAME="submit" VALUE="Button 2">
    <INPUT TYPE="SUBMIT" NAME="submit" VALUE="Button 3">
</FORM>
String submit = request.getParameter("submit");

submit will contain value of button clicked.

However, I will recommend to use servlet for form post processing.

0

Yes it can be done.

There are two JSP pages that are required, first one is to display the form and second one is the to process the form submission. We can use same JSP to process the form as well, but then we would need to verify if the page was requested by form submission.

Suppose the JSP for the form content is

<form method="POST" action="/processForm.jsp">
 ...

Then processForm.jsp

<%@ page import="java.io.*,java.util.*, javax.servlet.*"%>
<%@ page import="javax.servlet.http.*"%>
<%
   System.out.println("HTTP method:" + request.getMethod());

   if("POST".equals(request.getMethod()) {
       System.out.println(request.getParameter("txtUserId"));
       System.out.println(request.getParameter("txtUserName"));
       System.out.println(request.getParameter("txtUserLastName"));

       //Do something with the parameters
   }
%>

Answer to the second question is to set the value of the button input to something that signifies a click before form submission, then value of this button input in the JSP can be accessed as below

String clicked = request.getParameter("btnSAVE");

Or we can include a hidden input. On click of the btnSAVE value of this input is set before the form submission and the same is received by processForm.jsp

Since JSP's dynamic content (scriplet) is processed on the server, we can do things like reading a file or connecting to database in there.

11thdimension
  • 10,333
  • 4
  • 33
  • 71