简体   繁体   中英

pass variable from scriplet to jsp - null pointer

Struggling to pass a variable from a scriplet in one jsp to the 'calling jsp'

assetedit.jsp posts a binary stream to upload.jsp. upload.jsp proceses that stream and determines a filepath which I want to make available in assedit.jsp but the returned value always produces null pointer.

I am obviously doing something fundamentally wrong, I even tried asynch in the post to 'stop' the jsp in case things were happening too fast.

Thoughts appreciated.

assetedit.jsp -

//post binary to upload.jsp
$.ajax({
url: '/wz/upload.jsp',
data: decodedstring,
type: 'POST',
contentType: false,
processData: false,
}).done(function(data) {
console.log(data);
});     

upload.jsp (some lines removed to make easier to understand -

<%
try {
String root = getServletContext().getRealPath("/uploads/"); 
BufferedInputStream bis = new BufferedInputStream(request.getInputStream());
String nm = System.currentTimeMillis() + ".jpg";
String filepath = root + "/" + nm;
String realpath = (filepath.substring(filepath.lastIndexOf("/") - 7));
System.out.println("realpath is   "  + realpath);
pageContext.setAttribute("pathtogo", realpath);

}
fos.close();
bis.close();
);
} catch (Exception ex) {
ex.printStackTrace();
}
%>

assetedit.jsp -

<%
String str = request.getAttribute("pathtogo").toString();
System.out.println("pathtogo is   "  + str);
%>  

also tried (in assetedit.jsp) -

<%
        String name=(pageContext.getAttribute("pathtogo").toString()); 
        System.out.println("pathtogo is "+name); 
%>

resulting in - Uncaught TypeError: getElemRefs(...) is null

Thoughts appreciated, do I need to do another post in upload.jsp to send pathtogo back to assetedit.jsp?

Ralph

Can you try to use Session Scope, in order to pass appropriate variables between different JSP Pages.

pageContext.setAttribute("pathtogo",realPath,PageContext.SESSION_SCOPE);  
    
pageContext.getAttribute("pathtogo",PageContext.SESSION_SCOPE);  

https://docs.oracle.com/javaee/6/api/javax/servlet/jsp/PageContext.html

---- Edited ----

ok lets try something else:

upload.jsp

session.setAttribute("pathtogo",realPath);

assetedit.jsp -

<%
String str = (String)session.getAttribute("pathtogo");

System.out.println("pathtogo is   "  + str);
%>  

or String.valueOf(session.getAttribute("pathtogo"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM