Results 1 to 13 of 13
  1. #1
    kalyana is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default Please help : JSP ,Submit button and servlet

    hi ...
    i am new to JSP , i have problem when accessing submit button from servlet .i want ,if button pressed there some action in servlet .
    But Why there only work for button1 ? and didn't work for button 2 & 3.
    how the best way to handling submit button that more than one in a form ?
    all help would be appreciated

    for example :
    index.jsp :
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
    </head>
    <body>
    <form name="mainform" action="servletControl" method="POST">
    <input type="submit" value="button1" name="button1" />
    <input type="submit" value="button2" name="button2" />
    <input type="submit" value="button3" name="button3" />
    </form>
    </body>
    </html>

    servletControl :
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    String button1Click = request.getParameter("button1");
    String button2Click = request.getParameter("button2");
    String button3Click = request.getParameter("button3");
    try {
    if(button1Click.equals("button1")){
    RequestDispatcher dispatcher = request.getRequestDispatcher("/outputControl.jsp");
    dispatcher.forward( request, response);
    }
    if(button2Click.equals("button2")){
    RequestDispatcher dispatcher = request.getRequestDispatcher("/out2.jsp");
    dispatcher.forward( request, response);
    }
    if(button3Click.equals("button3")){
    RequestDispatcher dispatcher = request.getRequestDispatcher("/out3.jsp");
    dispatcher.forward( request, response);
    }
    } finally {
    out.close();
    }
    }

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,085
    Rep Power
    20

    Default

    What happens with that code?
    You never said (apart from it not working).

  3. #3
    kalyana is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default

    my problem :
    When button2 pressed , servlet didn't forward the page to out2.jsp.
    just blank page when button2 pressed ,it's also occur for button 3.
    i think the servlet can't get the request parameter when button pressed .
    but for button1 , there work well.....

    i need suggestion for this

    thanks

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,085
    Rep Power
    20

    Default

    Have you debugged to see what's being received?
    Printing out all request parameters?
    For each button push?

    ETA: Might I also suggest loggin any exceptions, because at the moment you aren't.
    Last edited by Tolls; 03-28-2011 at 05:21 PM.

  5. #5
    kalyana is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default

    Thanks toll

    not yet , but how to do that ?

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Only the button that is clicked has its parameter set so the rest of them are null.
    When other buttons are clicked, button1Click will be null so if(button1Click.equals("button1")){ ... will throw a N.P.E

  7. #7
    kalyana is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default

    thanks r035198x ...
    how to solve that ? to makes servlet handling when jsp have many button which name are different each other .
    i have try , when i change the name of all button in similar name.servlet can handling well...
    But actually i need handling for button which name are different each other .

    thanks .
    all button => name : button1

    <body>
    <form name="mform" action="servletControl" method="POST" >
    <input type="submit" value="button1" name="button1" />
    <input type="submit" value="button2" name="button1" />
    <input type="submit" value="button3" name="button1" />
    </form>
    </body>

    servletControl :
    String button1Click = request.getParameter("button1");
    try {
    if(button1Click.equals("button1")){
    RequestDispatcher dispatcher = request.getRequestDispatcher("/outputControl.jsp");
    dispatcher.forward( request, response);
    }
    if(button1Click.equals("button2")){
    RequestDispatcher dispatcher = request.getRequestDispatcher("/out2.jsp");
    dispatcher.forward( request, response);
    }
    if(button1Click.equals("button3")){
    RequestDispatcher dispatcher = request.getRequestDispatcher("/out3.jsp");
    dispatcher.forward( request, response);
    }

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Check for null first

    Java Code:
    if(button1Click != null && button1Click.equals("button1")){

  9. #9
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,085
    Rep Power
    20

    Default

    Which you would have noticed if you'd done as I suggested and debugged your code.
    That's a skill you are going to have to learn anyway.

  10. #10
    kalyana is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default

    i have try whit check null first , but the output is same before . when button 2 pressed , just only blank page ,also button 3 . for button 1 no problem . i see on http server monitor : when button 2 pressed , there nothing request attribute before & after .but for button 1 pressed , it's has.......

  11. #11
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You should do the null checks for all your buttonXXClick variables.
    Then you need to be checking your logs to see what is going on. If you get a blank page, it's highly likely that tomcat has logged the full exception trace in the log files. e.g an exception might be being thrown from your out2.jsp and out3.jsp.
    Also structure your code so that you only call
    Java Code:
    dispatcher.forward( request, response);
    once.

  12. #12
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,085
    Rep Power
    20

    Default

    So.
    Show the code.
    Do you now handle exceptions properly (ie logging them, or printing them to the screen), complete with stack trace?

    Have you got decent debugging logging going on oin the servlet?

  13. #13
    kalyana is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default

    thanks for all ..
    this case solve by added check null first and applied to all button .

Similar Threads

  1. How to handling submit button from servlet
    By kalyana in forum New To Java
    Replies: 1
    Last Post: 03-27-2011, 03:10 PM
  2. JSP program to handle submit button
    By gurpreet.singh in forum JavaServer Pages (JSP) and JSTL
    Replies: 6
    Last Post: 03-09-2011, 05:46 AM
  3. passing data to two servlets uing one submit button
    By Cashley in forum Java Servlet
    Replies: 2
    Last Post: 04-12-2010, 01:41 PM
  4. invoking servlet from jsp form submit using web.xml
    By java_hyd in forum Java Servlet
    Replies: 1
    Last Post: 11-24-2009, 08:57 PM
  5. Submit Button Help
    By Jay-1.1 in forum New To Java
    Replies: 2
    Last Post: 05-06-2008, 04:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •