[英]JSP response in the current HTML page
I want to add a file uploader to my JSP page. 我想将文件上传器添加到我的JSP页面。 Inside the JSP, it returns a new page containing text (result). 在JSP内部,它返回一个包含文本的新页面(结果)。 Is there a way to print the text in the current page I want to print that in a <div>
? 有没有一种方法可以在当前页面中打印文本,而我想在<div>
打印呢?
Here is an example to get you started: http://www.java-tips.org/java-ee-tips/javaserver-pages/uploading-file-using-jsp.html 这是一个入门的示例: http : //www.java-tips.org/java-ee-tips/javaserver-pages/uploading-file-using-jsp.html
For the rest of it, I can't figure out what you are asking. 对于其余的内容,我无法弄清楚您的要求。
FWIW, it is not a great idea to do this kind of thing in JSP. FWIW,在JSP中做这种事情不是一个好主意。 It is better to put your business logic in a servlet and use JSPs just for rendering the output. 最好将业务逻辑放在servlet中,并仅将JSP用于呈现输出。
@BalusC's answer sketches how you would implement this the right way ... using a servlet and a JSP. @BalusC的答案描述了如何使用servlet和JSP正确地实现此目标。
Just submit to a servlet which forwards back to the same JSP with the result. 只需提交到一个servlet,该servlet就将结果转发回同一JSP。
Eg this /upload.jsp
例如,这个/upload.jsp
<form action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" />
</form>
<div>${result}</div>
with a servlet which is mapped on an URL pattern of /upload
and does the following in doPost()
method: 与一个servlet映射到/upload
的URL模式,并在doPost()
方法中执行以下操作:
// (process file upload)
// ...
request.setAttribute("result", "File upload finished!");
request.getRequestDispatcher("/upload.jsp").forward(request, response);
This way the message "File upload finished!"
这样消息"File upload finished!"
will be available by ${result}
in the JSP and printed as such. 将在JSP中以${result}
形式提供并按原样打印。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.