Thread: Servlet and jsp

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2001
    Location
    FRANCE
    Posts
    0
    Rep Power
    0

    Question


    I have 2 questions.
    First how can I call a servlet from a frame?
    Second, how can I passed data from a servlet to a jsp?
    Thanks in advance.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2001
    Posts
    68
    Rep Power
    14
    as for the jsp part, I will be learning that in a couple of weeks when I get some projects done. To call a servlet from html frame, you just call the class file.

    <FRAME SRC="/servlets/My_Servlet" NAME="body" NORESIZE
    frameborder="YES" scrolling="YES" bordercolor="#999999">

    Usually this is all inside frameset tags or whatever.
    I hope this is what you were asking...

  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2001
    Location
    NY
    Posts
    0
    Rep Power
    0

    Thumbs up add it into request object...


    hi there,

    you can add your objects into the HttpServletRequest object
    and then redirect from your servlet to the jsp page.

    e.g. from "AServlet.java", passing a String object to the "a.jsp" file.

    In "AServlet.java":
    ....
    ...doGet(HttpServletRequeet req, HttpServletResponse res)
    {
    // the jsp file i wanna redirect to
    String target = "a.jsp";

    // creating a String object
    String hello = "Hello people!";

    // add it into the HttpServletRequest object
    req.setAttribute("hello", hello);

    // create a dispatch from the ServletContext object
    // to do the redirection for me =)
    // don't forget to specify where you wanna go.
    RequestDispatcher rd = getServletContext().getRequestDispatcher(target);

    // now here is the magic... you are passing the
    // HttpServletRequest obj and HttpServletResponse obj
    // to the jsp page
    rd.forward(req, res);

    .....
    // code here will not be execute since you have
    // pass the control to "a.jsp"
    }
    ....


    In the "a.jsp" page, you can retrieve the "hello" object
    by doing this:
    <%
    String hello = (String)request.getAttribute("hello");
    out.println(hello);
    %>

    It's very helpful by doing this because you can hide
    all the business logic inside the Servlet and the JSP
    file will just be doing printing...

    hope it help.
    -hoi =)

IMN logo majestic logo threadwatch logo seochat tools logo