request.getAttribute in jsp 给出空值

request.getAttribute in jsp is giving null value

所以在我的操作中 class 我有以下代码。

public ActionForward detailsForUploadForm(ActionMapping mapping,
        ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws IOException, ServletException {

    try {
        response.setHeader("Cache-Control", "no-store");
        response.setHeader("Cache-Control", "no-cache"); // HTTP 1.1
        response.setHeader("Pragma", "no-cache"); // HTTP 1.0
        response.setDateHeader("Expires", 0); // prevents caching at the
        // proxy server



        request.setAttribute("filePath", filePath);

    } catch (Exception ex) {}

在我的 jsp 文件中,我有以下内容

<html>String path = (String)session.getAttribute("path"); 
String filePath = (String)request.getAttribute("filePath");</html>
<form> <table>
 <input type="hidden" name="filePath" id="filePath" value="<%=filePath%>"/>
 </form>
 </table>           

问题是,我在 java class 中获取值,但在 jsp 文件中获取的值为空。

如果您使用 struts 来显示值,那么使用它来显示

<s:property value="%{#request.AttributeName}" />
<html>String path = (String)session.getAttribute("path"); 
String filePath = (String)request.getAttribute("filePath");</html>.

这是什么代码?您打算在 scriptlet <% %> 内编写 java 代码,但您已在 <html> 标记内编写。这不会在 JSP 中设置任何变量。相反,此代码将以文本形式显示在您的页面上。

如果您想访问 JSP 中的请求属性,最好的方法是表达式语言。此外,您在 scriptlet 中不需要上述代码。

只需使用${filePath}

 <input type="hidden" name="filePath" id="filePath" value="${filePath}"/>