3

I am working on a project which has just one page(index.jsp) and initial load of the page an Ajax request is being sent and JSON data is retrieved. The AJAX call sent to my Servlet and Servlet returns JSON data,and i have only one Servlet. I am trying to send the some data to my JSP page to populate, so This is how i am writing my Servlet......

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws  ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out =response.getWriter();
    String queryString = request.getQueryString();
            ResourceBundle props = ResourceBundle.getBundle("jira");

    XmlMerge xmlMerge = new XmlMerge();
    String mergeFiles=xmlMerge.getJsonData();

    out.println(mergeFiles);
    out.close();
         //Debug Statement
        System.out.println(xmlMerge.getTodo());
        // *THIS IS THE WAY I AM SEND DATA TO JSP PAGE.*
    request.setAttribute("todo", xmlMerge.getTodo());
    request.getRequestDispatcher("/index.jsp").forward(request, response);
}

and in my index.jsp

<%=(String)request.getAttribute("todo")%>

I am trying to output the result.

What is going wrong?

pushya
  • 4,338
  • 10
  • 45
  • 54
  • @Sotirios request.getRequestDispatcher("/index.jsp").forward(request, response); Do i need to do this since i have only one JSP page. isn't it default do to index.jsp – pushya Oct 01 '13 at 18:48
  • 2
    Didn't you see those `IllegalStateException`s in the server log? This is very definitely not how a servlet should look while intercepting on a request which forwards to a JSP. That `null` is likely just coming from `mergeFiles` variable. When you ultimately need to forward to a JSP in a servlet, you should generally keep your fingers off from the `response` object in the servlet as it's then the JSP who will handle the response. – BalusC Oct 01 '13 at 18:51
  • I just did this: `request.setAttribute("todo", "10");` and int prints 10 in my index.jsp. Can you show your XmlMerge.getTodo() code? or debug it. Print it and tell us what is displayed. – porfiriopartida Oct 01 '13 at 18:54
  • I did System.out.println and it is "mergeFiles" outputting correct value – pushya Oct 01 '13 at 18:56
  • @BalusC their are no IllegalStateExceptions in server logs – pushya Oct 01 '13 at 18:58
  • Can you update the code of the question adding the debug statement? or XmlMerge src code – porfiriopartida Oct 01 '13 at 18:59
  • Well, apparently the way how you invoked it is wrong or you're not running the code you think you're running. It's by the way strange to see the `text/html` content type header being set while the servlet is supposed to return JSON data. Here are at least some links to get you started the right way: http://stackoverflow.com/tags/servlets/info and http://stackoverflow.com/questions/4112686/how-to-use-servlets-and-ajax/4113258#4113258 – BalusC Oct 01 '13 at 19:00
  • Yeah.. that may be inconsistent, but since you are callign a .jsp it should try to render anything it is passed. I think it would throw an error 406 if that was the root cause.. – porfiriopartida Oct 01 '13 at 19:03

1 Answers1

9

I just performed this change and it displays what you need:

  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html");
    request.setAttribute("todo", "10");
    request.getRequestDispatcher("/index.jsp").forward(request, response);
  }

This is the generated index.jsp:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<%=(String)request.getAttribute("todo")%>

</body>
</html>

There might be something wrong with your getTodo().. I don't know how it works but maybe this could help:

...
XmlMerge xmlMerge = new XmlMerge();
String todo = xmlMerge.getTodo();
...
request.setAttribute("todo", todo);

UPDATE:

PrintWriter out = response.getWriter();
out.println(...);
out.close();

This is your problem... you are getting the resource and close it. This might cause an illegal state exception issue..

You "don't need" the dispatcher to the index.jsp.. if you don't use a dispatcher but you want to render your page you can use something like this:

  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    response.setContentType("text/html");
    response.getWriter().write("<html><body>"+getSomething()+"</body></html>");
  }

Why isn't index.jsp a default call? because there might not even exists an index.jsp file and it may be the call for another servlet. You can have a configuration that maps the call to index.jsp to a servlet.

http://tutorials.jenkov.com/java-servlets/web-xml.html

I still don't know what is the purpose of using out.println but if you wanted it to be displayed in the jsp you can send it as argument as the "todo":

 request.setAttribute("mergeFiles", mergeFiles);

And then print it in the jsp as the "todo".

porfiriopartida
  • 1,546
  • 9
  • 17
  • What is the purpose of "request.getRequestDispatcher("/index.jsp").forward(request, response);". If i have only one JSP page then isn't it going to send that data same jsp page. what i do need to write dispatcher statement – pushya Oct 01 '13 at 19:09
  • @pushya The thing is that you don't need an index.jsp, that might be the default call for the root of your domain, but you can display html without the dispatcher.. something like: `response.getWriter().write("Not the index.jsp");` And since you can have mapping, index.jsp could be another servlet (and not an index.jsp file) and you may not want to call it. – porfiriopartida Oct 01 '13 at 19:15
  • <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> IS this s mandatory for a JSP page? – pushya Oct 01 '13 at 19:18
  • @pushya I don't think it is, it was a default add by the IDE, sometimes you have to add there few lines depending on what tags/commands you want to use. I don't think this is the case. – porfiriopartida Oct 01 '13 at 19:25