Java 如何从 Servlet 向 JSP 发送数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19123573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send Data to JSP from a Servlet?
提问by pushya
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......
我正在开发一个只有一页 (index.jsp) 的项目,并且页面的初始加载正在发送 Ajax 请求并检索 JSON 数据。发送到我的 Servlet 和 Servlet 的 AJAX 调用返回 JSON 数据,而我只有一个 Servlet。我正在尝试将一些数据发送到我的 JSP 页面以进行填充,所以这就是我编写 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
在我的 index.jsp 中
<%=(String)request.getAttribute("todo")%>
I am trying to output the result.
我正在尝试输出结果。
What is going wrong?
出了什么问题?
回答by porfiriopartida
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:
这是生成的 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:
您的 getTodo() 可能有问题。我不知道它是如何工作的,但也许这会有所帮助:
...
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:
您“不需要” index.jsp 的调度程序。
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.
为什么 index.jsp 不是默认调用?因为甚至可能不存在 index.jsp 文件,它可能是对另一个 servlet 的调用。您可以有一个配置,它将对 index.jsp 的调用映射到一个 servlet。
http://tutorials.jenkov.com/java-servlets/web-xml.html
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":
我仍然不知道使用 out.println 的目的是什么,但是如果您希望它显示在 jsp 中,您可以将其作为“todo”作为参数发送:
request.setAttribute("mergeFiles", mergeFiles);
And then print it in the jsp as the "todo".
然后在jsp中打印为“todo”。