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

标签 java jsp servlets jstl

我正在尝试使用 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 );
    }

}

最佳答案

您需要使用requestScopec: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/

相关文章:

java - 每次启动应用程序时,数据库内容都会被删除

java - 使用改造后请求正文中的原始字符串

java - Servlet、JSP、JPA - 未找到属性异常

css - 在 Struts2 中滚动

java - 如何在 Spring 中为 JSP 表单使用绝对路径?

java - DataNucleus getObjectById 为外键对象返回 null

java - 为什么不能从android apk反编译这个方法?

java - 从 wiki 到 ourjsp 页面显示单词的含义

java - Jetty设置Servlet与JSP页面通信

java - Android 应用程序登录屏幕 - 错误变量和共享首选项比较