简体   繁体   English

将变量从脚本传递到 jsp - null 指针

[英]pass variable from scriplet to jsp - null pointer

Struggling to pass a variable from a scriplet in one jsp to the 'calling jsp'努力将一个 jsp 中的脚本中的变量传递给“调用 jsp”

assetedit.jsp posts a binary stream to upload.jsp. assetedit.jsp 将二进制 stream 发布到 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. 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.我显然在做一些根本性的错误,我什至在帖子中尝试异步“停止”jsp,以防事情发生得太快。

Thoughts appreciated.想法赞赏。

assetedit.jsp -资产编辑.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 - upload.jsp (删除了一些行以便于理解 -

<%
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 -资产编辑.jsp -

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

also tried (in assetedit.jsp) -也尝试过(在assetedit.jsp中) -

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

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

Thoughts appreciated, do I need to do another post in upload.jsp to send pathtogo back to assetedit.jsp?想法表示赞赏,我是否需要在upload.jsp 中再发一篇帖子以将pathtogo 发送回assetedit.jsp?

Ralph拉尔夫

Can you try to use Session Scope, in order to pass appropriate variables between different JSP Pages.您能否尝试使用 Session Scope,以便在不同的 JSP 页面之间传递适当的变量。

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 https://docs.oracle.com/javaee/6/api/javax/servlet/jsp/PageContext.html

---- Edited ---- ----已编辑----

ok lets try something else:好的,让我们试试别的:

upload.jsp上传.jsp

session.setAttribute("pathtogo",realPath);

assetedit.jsp -资产编辑.jsp -

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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