[英]pass variable from scriplet to jsp - null pointer
努力將一個 jsp 中的腳本中的變量傳遞給“調用 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.
我顯然在做一些根本性的錯誤,我什至在帖子中嘗試異步“停止”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 (刪除了一些行以便於理解 -
<%
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();
}
%>
資產編輯.jsp -
<%
String str = request.getAttribute("pathtogo").toString();
System.out.println("pathtogo is " + str);
%>
也嘗試過(在assetedit.jsp中) -
<%
String name=(pageContext.getAttribute("pathtogo").toString());
System.out.println("pathtogo is "+name);
%>
導致 - Uncaught TypeError: getElemRefs(...) is null
想法表示贊賞,我是否需要在upload.jsp 中再發一篇帖子以將pathtogo 發送回assetedit.jsp?
拉爾夫
您能否嘗試使用 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
----已編輯----
好的,讓我們試試別的:
上傳.jsp
session.setAttribute("pathtogo",realPath);
資產編輯.jsp -
<%
String str = (String)session.getAttribute("pathtogo");
System.out.println("pathtogo is " + str);
%>
或String.valueOf(session.getAttribute("pathtogo"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.