[英]servlet to jsp pass value
I want to simple pass value servlet to jsp page. 我想简单地将值传递给jsp页面。 I want to run jsp file and onload data is display from getting servlet
我想运行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 java代码
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 web.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>
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: 做出的改变:
replace 更换
RequestDispatcher reqDispatcher = getServletConfig().getServletContext().getRequestDispatcher("javaPapers.jsp");
with 同
RequestDispatcher reqDispatcher = request.getRequestDispatcher("javaPapers.jsp");
Passing the param using the request.setAttribute("message", message);
使用
request.setAttribute("message", message);
传递参数request.setAttribute("message", message);
should work using this code: 应该使用此代码:
RequestDispatcher rd = getServletContext().getRequestDispatcher("yourPage.jsp");
rd.forward(request,response);
You can also pass the attribute using the URL in 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");
You have to use context path to get message from servlet to jsp. 您必须使用上下文路径从servlet获取消息到jsp。 This will definitely work I have done it.
这绝对有效,我已经做到了。
String msg = "Message from servlet to jsp";
response.sendRedirect(response.encodeRedirectURL(contextPath+"/report/test.jsp?msg="+msg));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.