1



I have a jsp page in which their is header, left and right panel, header and left panel is static. Left panel is having href, if we click on link a new page will be loaded in right panel. I want to achieve this task using Jquery Ajax without refreshing whole page.

Now my query is on click of link/button using jquery ajax I call servlet and I from servlet I want to set jsp dynamic values and then load the same in right panel with updated values.

I tried setting the values but I am getting null. Can you please assist me in setting dynamic values in jsp.

Below is my sample code on click of button I want to include hello.jsp with dynamic variable "message" if I succeed here I can solve my above problem.

index.jsp

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JQuery AJAX Testing</title>

<script src="js/jquery-1.11.0.min.js"></script>

<script>
$(document).ready(function() {
    $("button").click(function(){

        $.ajax({
            type : "post",
            url : "AjaxServlet",

            success : function(responseText) {

                $('#message').load("jsp/hello.jsp");
            },
            error : function(xhr, ajaxOptions, thrownError) {
                alert(xhr.status);
                alert(thrownError);
            }

        });
    });


});
</script>

</head>
<body>
<div id="ajaxtest">
<h2>AJAX Testing using JQuery</h2>
</div>
<div id="message"></div>
<button>Change Content</button>
</body>
</html>

AjaxServlet code:

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    System.out.println("Servlet post method is called");

    // I want to set this value in hello.jsp
    request.setAttribute("message", "I am from request get attribute");
    response.getWriter().write("This is reply from servlet");

}

hello.jsp

<h1>Hello this page is include from index JSP</h1>
<%=request.getAttribute("message")%>

I want to set message in hello.jsp from servlet on load from JQuery

Pise

pise
  • 849
  • 6
  • 24
  • 51

1 Answers1

1

What you want to do is, get the request param "message" on success not the "jsp/hello.jsp" and then return the request params to the template but not the entire jsp file.

I don't think can load a jsp file like that and have interpreted correctly, plus load() will load the file via a second ajax request and will never return anything to the template via the first request.

try:

success : function(responseText) {
        $('#message').html(responseText);
    }

Also look at this Q/A on stack to understand the principle of ajax return value: How do I return the response from an asynchronous call?

Community
  • 1
  • 1
mahatmanich
  • 10,791
  • 5
  • 63
  • 82
  • I know a way were in we can write whole html content in response.writer and the same I can get in responseText but instead I don't want to write any think in class and want to include jsp just by setting variable values in request.setAttribute. – pise Mar 26 '14 at 12:34
  • Ok check this out: http://stackoverflow.com/questions/4112686/how-to-use-servlets-and-ajax – mahatmanich Mar 26 '14 at 13:06
  • I got the point of using JSONObject but my aim is not to make some changes in current jsp but to load a new jsp. – pise Mar 26 '14 at 18:29
  • Please check http://www.tagindex.net/html/frame/example_t04.html this is done using frame but I want to go with ajax. I want when user clicks on left panel, right panel changes. I will call servlet from left panel and servlet will forward page to some.jsp and same should be displayed in right panel without refreshing left panel. – pise Mar 26 '14 at 18:30
  • please read up on ajax. what you are trying to do is not going to work with ajax. – mahatmanich Mar 26 '14 at 19:48