简体   繁体   中英

How to send Data to JSP from a Servlet?

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?

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".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM