我正在尝试使用 servlet 构建一个 Web 应用程序,但面临这个问题,我刚刚在 servlet 类中设置了数据,并使用 ${} 在 jsp 中对其进行了 fectching
JSP --->
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<c:out value="${data}"/>
<%= request.getAttribute("data") %>
</body>
</html>
Servlet 类:
public class Naveen extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Object data = "Some data, can be a String or a Javabean";
request.setAttribute( "data", data );
RequestDispatcher rd = request.getRequestDispatcher( "/new.jsp" );
rd.forward( request, response );
response.getWriter().append( "Served at:" ).append( request.getContextPath() );
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet( request, response );
}
}
最佳答案
您需要使用requestScope
在c:out
<c:out value="${requestScope.data}">
详细示例可以引用:https://www.journaldev.com/2090/jstl-tutorial-jstl-tags-example
我认为您直接调用/new.jsp
!
为了获取值,您需要调用 servlet URL。
这将解决您的问题,因为 servlet 请求被发送到 /new.jsp
属性值 data
.
关于java - 无法在 jsp 中使用 ${data} 显示数据,而 request.getAttribute ("data")确实返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55826572/