我想简单地将值 servlet 传递给 jsp 页面。我想运行 jsp 文件并通过获取 servlet 显示加载数据
但我得到了 null :"Servlet communicated message to JSP: null "
下面是我的代码。
爪哇代码
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 文件
<%@ 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
<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>