Java servlet 到 jsp 传递值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18585956/
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
servlet to jsp pass value
提问by Nick
I want to simple pass value servlet to jsp page. I want to run jsp file and onload data is display from getting servlet
我想简单地将值 servlet 传递给 jsp 页面。我想运行jsp文件并通过获取servlet显示onload数据
But I got null : "Servlet communicated message to JSP: null "
但我得到了 null : "Servlet communicated message to JSP: null "
below is my code.
下面是我的代码。
java code
代码
package api;
public class ServletToJSP extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//communicating a simple String message.
String message = "Example source code of Servlet to JSP communication.";
request.setAttribute("message", message);
RequestDispatcher reqDispatcher = getServletConfig().getServletContext().getRequestDispatcher("javaPapers.jsp");
reqDispatcher.forward(request,response);
}
}
jsp file
jsp文件
<%@ page import="api.ServletToJSP" language="java" %>
<html>
<body>
<%
String message = (String) request.getAttribute("message");
out.println("Servlet communicated message to JSP: "+ message);
// Vector vecObj = (Vector) request.getAttribute("vecBean");
// out.println("Servlet to JSP communication of an object: "+vecObj.get(0));
%>
</body>
</html>
web.xml
网页.xml
<servlet>
<servlet-name>ServletToJSP</servlet-name>
<servlet-class>api.ServletToJSP</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletToJSP</servlet-name>
<url-pattern>/ServletToJSP/*</url-pattern>
</servlet-mapping>
回答by jiacheo
replace
代替
RequestDispatcher reqDispatcher = getServletConfig().getServletContext().getRequestDispatcher("javaPapers.jsp");
with
和
RequestDispatcher reqDispatcher = request.getRequestDispatcher("javaPapers.jsp");
回答by MaVRoSCy
Passing the param using the request.setAttribute("message", message);
should work using this code:
使用request.setAttribute("message", message);
应该使用以下代码传递参数:
RequestDispatcher rd = getServletContext().getRequestDispatcher("yourPage.jsp");
rd.forward(request,response);
You can also pass the attribute using the URL in dispatcher :
您还可以使用 dispatcher 中的 URL 传递属性:
RequestDispatcher rd = getServletContext().getRequestDispatcher("yourPage.jsp?message=some message");
rd.forward(request,response);
Also you can share info using session object.
您也可以使用会话对象共享信息。
session.setAttribute("message","your message");
And then retrieve it in jsp using:
然后使用以下命令在jsp中检索它:
String message=(String)session.getAttribute("message");
回答by MaVRoSCy
There are few things to change:
有几件事要改变:
In servlet
在 servlet 中
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//communicating a simple String message.
String message = "Example source code of Servlet to JSP communication.";
request.setAttribute("message", message);
request.getRequestDispatcher("javaPapers.jsp").forward(request,response);
}
In jsp
在jsp中
<html>
<body>
Servlet communicated message to JSP: ${message}
</body>
</html>
Changes made:
所做的更改:
- In servlet, used request.getRequestDispatcher(String url)
- In jsp, removed servlet import.
- In jsp, used EL to get attribute value.
- 在 servlet 中,使用 request.getRequestDispatcher(String url)
- 在jsp 中,删除了servlet 导入。
- 在jsp中,使用EL来获取属性值。
回答by Rakesh Kumar DEo
You have to use context path to get message from servlet to jsp. This will definitely work I have done it.
您必须使用上下文路径从 servlet 获取消息到 jsp。这肯定会起作用,我已经做到了。
String msg = "Message from servlet to jsp";
response.sendRedirect(response.encodeRedirectURL(contextPath+"/report/test.jsp?msg="+msg));