Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Need help with a simple jsp and servlet problem

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with a simple jsp and servlet problem

    Hello Java Gurus,

    I am working on a simple web project and need help with a problem.

    The index.jsp has 2 iframe components, where the src attribute for each iframe references to a separate jsp page (so the src for iframe1 is iframe1.jsp and for iframe2, its iframe2.jsp)

    Now, the iframe2.jsp has a search text and a submit button. On entering a search text, the control goes to a servlet, which then runs some logic and sends an object to be displayed in iframe1.

    In the servlet, I have used RequestDispatcher to forward the response to iframe1. Now, the problem here is that the response is getting populated in iframe2.jsp itself (from where the initial request originated). I need to response to go to iframe1.jsp

    Have verified all the links and paths in the code, so there isn’t any typo anywhere. Just wondering what might be wrong here. Is RequestDispatcher not the right way to send control to another jsp? If yes, what is the other alternative?

    Please advise.

    Thanks,
    B.S.S


  2. #2
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with a simple jsp and servlet problem

    Here are the code snippets

    First the index.jsp - the page that contains the 2 iframes
    HTML Code:
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        </head>
        <body>
            <h1>Hello World!</h1>
            <iframe name="frame1" id="frame1" src="frame1/frame1.jsp"/>
            <iframe name="frame2" id="frame2" src="frame2/frame2.jsp"/>
        </body>
    </html>
    next,frame1.jsp which is put in a folder named frame1.

    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        </head>
        <% if (request.getAttribute("data") == null) {%>
        <body>
            <h1>Hello World! from Frame1</h1>
        </body>
        <% } else if (request.getAttribute("data").toString().length() != 0) { %>
        <h1>Hello World! from Frame1 <%=request.getAttribute("data")%></h1>
        <% } %>
    </html>

    then we have frame2.jsp which is put in a folder named frame2.

    HTML Code:
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        </head>
        <form action="/Testing/Servlet">
        <body>
            <h1>Hello World from Frame2</h1>
            <input type="submit" name="submit" />
        </body>
        </form>
    </html>
    and finally the servlet
    import java.io.IOException;
    import java.io.PrintWriter;
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
     
    public class Servlet extends HttpServlet {
        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            String message = "Responding from servlet";
            try {
                request.setAttribute("data",message);
                RequestDispatcher dispatcher = request.getRequestDispatcher("/frame1/frame1.jsp");
                if (dispatcher != null){
                dispatcher.forward(request, response);
                }
            } finally { 
                out.close();
            }
        } 
     
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
            processRequest(request, response);
        } 
     
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
            processRequest(request, response);
        }
     
        public String getServletInfo() {
            return "Short description";
        }// </editor-fold>
    }

    Instead of frame1 getting the message from the servlet, the frame2 is always getting populated with this message.

    Please advise.

    Thanks,
    B.S.S

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Need help with a simple jsp and servlet problem

    Moved to Java Servlet

    RequestDispatcher forwards the responds to the client - in this case the client is the form submission frame (frame2). You will need some way to let the other frame know (I don't deal with frames often so won't try and advise beyond that).

  4. #4
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with a simple jsp and servlet problem

    Quote Originally Posted by copeg View Post
    Moved to Java Servlet

    RequestDispatcher forwards the responds to the client - in this case the client is the form submission frame (frame2). You will need some way to let the other frame know (I don't deal with frames often so won't try and advise beyond that).
    Anybody has any ideas on the same? Any help would be greatly appreciated.

    B.S.S

  5. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    11
    My Mood
    Relaxed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with a simple jsp and servlet problem

    try using response.sendRedirect("/frame1/frame1.jsp"); instead of RequestDispatcher.
    I hadn't deal with frames.
    may this will solve ur problem.
    best of luck.

Similar Threads

  1. collecting information/moving from servlet to servlet
    By CBird in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 1st, 2011, 07:04 PM
  2. Simple problem...
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 6th, 2011, 12:02 AM
  3. Authentication problem in a servlet
    By Asido in forum Java Servlet
    Replies: 3
    Last Post: September 17th, 2010, 05:57 AM
  4. Problem with the servlet and jdbc
    By manjukdvg in forum Java Servlet
    Replies: 3
    Last Post: September 8th, 2010, 08:44 AM
  5. Newbie needs help in simple servlet program
    By visitor1078 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 19th, 2009, 01:46 PM

Tags for this Thread