Discussions

EJB design: Passing an object from jsp to servlet

  1. Passing an object from jsp to servlet (4 messages)

    Hi,
    I want to pass an object from a jsp to a servlet. I've tried to use request.setAttribute("Object", object) in the jsp page and call the object in my servlet using request.getAttribute("Object"). Unfortunately did work. Could anybody help me with this and give expample code? I'm new to JSP and please don't slaughter me when this is a pretty easy and stupid question ;-)

    Cheers

    Threaded Messages (4)

  2. Inside the JSP you want to use response.setAttribute("obj", obj); The response is what will be sent to the Servlet.

    You could also stick it in the session (just be sure to clean up afterwards or you will get an "over-stuffed session").

    Normally you get POST data from a form and create your objects in a Servlet (request.getParameter("")) instead of (request.getAttribute(""), assuming you set it).

    You should seek to minimize Java code in jsps (that's what tlds are for). This makes the front end easier for non-developers to create (what is all this junk in my HTML?). Otherwise, you are running into the danger of making big, complicated jsps that contain way too much java code. This is actually symptomatic of an anti-pattern (monolithic jsp). I have seen jsps that are several thousand lines of mostly java code. Don't let that happen to you!
  3. JSP TO SERVLET Passing Object[ Go to top ]

    hi,
      to pass a object from JSP Page to Servlet you use the method
    doPost(HttpServletRequest request, HttpServletResponse response)

    or
    doGet(HttpServletRequest request, HttpServletResponse response)




    eg.
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    import java.util.*;

    public class greetServlet extends HttpServlet
    {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
    {
    //Get Parameters from the request
    String name=request.getParameter("name");

    String email=request.getParameter("email");

    String message=null;

    }

    }
  4. please fwd the same info to me.
    thanking you.
    Sachin.
  5. hey dear.if u not getting with setAttribute and getAttribute than make use of usebean.take usebean in between and in that set a value for ur object and get in servlet page.u will get it.