无法在jsp中使用$ {data}显示数据,而request.getAttribute(“data”)确实返回值

问题描述 投票:0回答:2

我正在尝试使用servlet构建一个Web应用程序,但面对这个问题,我只是在servlet类中设置我的数据并使用$ {}在jsp中进行处理

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 );
    }

}
java jsp servlets jstl
2个回答
3
投票

你需要在requestScope中使用c:out

<c:out value="${requestScope.data}">

有关详细示例,请参阅:https://www.journaldev.com/2090/jstl-tutorial-jstl-tags-example

我想你直接打电话给/new.jsp!要获得价值,您需要调用servlet URL。这将解决您的问题,因为在servlet请求被发送到具有属性值/new.jspdata


0
投票

在你的jsp文件中试试这个:添加isELIgnored = false

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" isELIgnored="false" %>

默认情况下,此属性应处于启用状态。您可以将其添加到每个jsp页面,以使EL(表达式语言)处理成功。

© www.soinside.com 2019 - 2024. All rights reserved.