1

i want write a jsp page which must have 3 buttons like $save$ to save the process we have done, to delete, to view what we have done.

i tried as :
<form action="go_save" method="post"> // how to go_view.jsp, go_delete.jsp
   <input type="submit" name="submit" value="SUBMIT">


code :
      <%@page contentType="text/html" pageEncoding="UTF-8"%>
     <!DOCTYPE html>
     <html>
     <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
    </head>
     <body>
     <h1>Hello World!</h1>
     <form action="go.jsp" method="post" >
         name<input type="text" name="name">
         age<input type="text" name="age">
         <input type="button" value="Save" name="Save" 
         onclick="document.forms[0].action = 'go_save.jsp'; return true;" />
          <input type="button" value="view" name="view" 
          onclick="document.forms[0].action = 'go_view.jsp'; return true;" />
       </form>
   </body>
</html>

but here the page is not redirecting to the given pages like go_view, go_save. i want to catch these values name, age in go_view, go_save . but how ?

Srinivas Thanneeru
  • 161
  • 1
  • 2
  • 12

2 Answers2

2

You can do something like this...have an onclick event

<button type="button" onclick="location = 'go_save.jsp'">Save</button>
<button type="button" onclick="location = 'go_view.jsp'">Save</button>

remove the "action="go_save""

0

You should do this job in a controller, probably a Servlet rather than the JSP.

Create a Servlet(say, RedirectServlet - you can naem), and in doPost method, redirect to the relevant JSP page, based on what button was clicked. You can give each button some name for this task.

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    if (request.getParameter("GoSave") != null) {
        request.getRequestDispatcher("/go_save.jsp").forward(request, response);

    } else if (request.getParameter("GoDelete") != null) {
        request.getRequestDispatcher("/go_delete.jsp").forward(request, response);

    }  else if (request.getParameter("GoView") != null) {
        request.getRequestDispatcher("/go_view.jsp").forward(request, response);
    }
}

Now, from your form, instead of redirecting to a jsp page, redirect to this servlet:

<form action="redirectServlet" method="post"> 
   name<input type="text" name="name">
   age<input type="text" name="age">

   <input type="submit" name="GoView" value="SUBMIT">
   <input type="submit" name="GoDelete" value="SUBMIT">
   <input type="submit" name="GoSave" value="SUBMIT">
</form>

Now, you can get the value of name and age is respective JSP page, where the servlets redirected the request to, using request.getParameter("name").

Or you can also use some pattern like - MVC's Front Controller Pattern

Community
  • 1
  • 1
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525