How to pass parameter to servlet

17,412

Solution 1

You kind of mess things here.

onclick() is Javascript and executed on client side. It has no (direct) way to update session-scoped bean. That bean is left on server-side, and was used when the HTML page was generated. To pass parameters back to servlet you need to use good old form fields, and submit the form.

Add more fields to the form, set their values before submit, then submit.

In Servlet call request.getParameter("name");

P.S. To automate this kind of things USE STRUTS. :-) Struts does exactly what you want: before passing the parameters to action, it populates the bean with those parameters. Transparently.

Solution 2

It depends exactly what you are trying to do. The

<jsp:useBean id = "userBean" scope = "session" class = "org.project.User" />

tag will allow you to use the userBean attribute of the session in your jsp. If there is not a userBean attribute in the session, it will create a new one (using the default constructor for org.project.User) and place it in the session.

Then, when you get to the servlet, you can retrieve it with:

User user = (User)request.getSession().getAttribute("userBean");

Solution 3

 getServletConfig().getServletContext().getRequestDispatcher("servlet path & name");         
 dispatcher.forward (request, response);
Share:
17,412
pypmannetjies
Author by

pypmannetjies

Software Engineer obsessed with quality and best practices

Updated on June 23, 2022

Comments

  • pypmannetjies
    pypmannetjies almost 2 years

    How do I pass a parameter from a page's useBean in JSP to a servlet in Java? I have some data in a form that gets passed no problem with a submit button, but no way to send anything else. Please help? Here is my code:

    <input name = "deleteGameButton" type = "submit" value = "Delete"
     onclick = "submitToServlet('DeleteGameServlet');">
    

    Here is the corresponding javascript:

     function submitToServlet(newAction)
     {
       document.userGameForm.action = newAction;
     }
    

    I'd like the servlet to have access to userBean

     <jsp:useBean id = "userBean" scope = "session" class = "org.project.User" />
    
  • Vladimir Dyuzhev
    Vladimir Dyuzhev over 15 years
    This tag will update bean on servers side, during the page generation. The guy (apparently; it's hard to be sure) wants a form submitted to update the bean which is in session.
  • Charlie
    Charlie over 15 years
    Ok, then AJAX (or any Framework as Suggested STRUTS) is the solution, cause the communication between the server side and the client has to be done behind the scene, right?
  • pypmannetjies
    pypmannetjies over 15 years
    Hi, thanks... Yes, I need something that passes parameters transparently from the jsp code to a servlet. I will try to use STRUTS, thanks.
  • pypmannetjies
    pypmannetjies over 15 years
    Hi, thanks. I did use extra fields in the form in the end. But I thought there must be something easier... I guess that would be STRUTS. But it works as is now, so I'll use STRUTS in future, the deadline is looming. Thanks.