0

I have 2 jsp pages, one called as MyPage.jsp and other as View.jsp. View.jsp has a tree structure. MyPage.jsp has some text fields called as number and design which need to be populated through a bean via servlet. On click of any of the tree node in View.jsp, MyPage.jsp should be rendered with the text fields values set. Now what is happening is since MyPage.jsp is being called twice, i.e. once in View.jsp(in ajax function) and second in the request dispatcher in the servlet so the bean values being set in the servlet are lost. Please suggest a better way so as to retain the values throughout and that on click of the tree node MyPagejsp is rendered with the field values set.

responseBean.setNumber("220");
responseBean.setDesign("xyz");
response.setContentType("text/html");                        
response.setCharacterEncoding("UTF-8");
request.setAttribute("responseBean", responseBean);
RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher("/MyPage.jsp");
requestDispatcher.include(request, response);
response.getWriter().write("Success");

The jsp page from where MyPage.jsp is called with the bean values set has following code

View.jsp

$.ajax({
 url : AJAX_SERVLET,
type: "GET",
data: "Number="+node.data.title,
success : function(output) {                                
$("[id=content]").attr("src", '/Test-portlet/MyPage.jsp');
}
});
}

MyPage.jsp

<jsp:useBean id="responseBean" class="com.web.bean.ResponseBean" scope="request">

<jsp:setProperty name="responseBean" property="*"/>

</jsp:useBean>
<body>
<%System.out.println("Values"+responseBean.getNumber()); %>
</body>

In the above MyPage.jsp code, System.out.println is printing the value twice; once as Values 202 and second as Values null. Since it replaces the original value with null just because MyPage.jsp is called twice and so the second time value is lost. Please help

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Buzz3110
  • 15
  • 1
  • 6
  • The functional requirement is far from clear. Please edit and improve/elaborate. – BalusC Jul 19 '11 at 08:25
  • @BalusC Please see my edited question. I hope I am now clear. – Buzz3110 Jul 19 '11 at 09:06
  • Your point is understood, but your `System.out.println()` example is bad as it would never find and print the bean which is set as request attribute by either servlet or `jsp:useBean`. It would only result in a `NullPointerException`. – BalusC Jul 19 '11 at 09:39

1 Answers1

1

I believe that you're confusing/misunderstanding some basic concepts, particularly how HTTP works and how Ajax is supposed to work.

What is happening here is that you're effectively firing two HTTP requests. One by $.ajax() and other by element.attr('src', url). Each request results in a fully distinct bean instance being created and set. You're completely ignoring the bean data in the callback of the $.ajax() request. I am not sure what HTML element [id=content] represents, but I guess that it's an <iframe>. That's not entirely the right way.

You should end up with effectively firing one HTTP request. There are basically 2 solutions:

  1. Forget the $.ajax() and send the request by element.attr('src', url).

    $("[id=content]").attr("src", "/Test-portlet/MyPage.jsp?number=" + encodeURIComponent(node.data.title));
    

    You can also change the URL to be a servlet one so that you have a bit more preprocessing control and finally use RequestDispatcher#forward() instead of include(). Do NOT write HTML to the response in the servlet. Let JSP do it.

  2. Forget the <iframe> thing and process the response fully by Servlet/Ajax without intervention of JSP. You would need to convert the bean into other data format which is easily parseable by JavaScript/jQuery. I would suggest to use JSON for this.

    $.get(AJAX_SERVLET, { "number": node.data.title }, function(response) {
        $("#number").text(response.number);
        $("#design").text(response.design);
    });
    

    with in HTML for example

    <div id="number"></div>
    <div id="design"></div>
    

    and in servlet

    // ... (create ResponseBean the way as you want)
    String json = new Gson().toJson(responseBean);
    
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(json);
    

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555