Search code examples
jspstruts

request.getAttribute in jsp is giving null value


So in my Action class I have the following code.

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

And in my jsp file, I have the following

<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>           

The problem is, I am getting the value in my java class but in my jsp file i am getting the value as null.


Solution

  • <html>String path = (String)session.getAttribute("path"); 
    String filePath = (String)request.getAttribute("filePath");</html>.
    

    What is this code ? You intended to write a java code inside scriptlet <% %>, but you have written in inside <html> tag. That won't set any variable in JSP. Instead this code will be displayed as it is on your page as text.

    If you want to access request attribute in JSP, best way would be Expression language. Also you won't need above code in scriptlet.

    Just use ${filePath}

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