开发者

JSP response in the current HTML page

I want to add a file uploader to my JSP page. Inside the JSP, it returns a new page containing text (result). Is there a way to print the text in th开发者_开发百科e current page I want to print that in a <div>?


Here is an example to get you started: 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. It is better to put your business logic in a servlet and use JSPs just for rendering the output.

  • Servlets and JSP Pages Best Practice
  • why business logic should be moved out of JSP?

@BalusC's answer sketches how you would implement this the right way ... using a servlet and a JSP.


Just submit to a servlet which forwards back to the same JSP with the result.

E.g. this /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:

// (process file upload)
// ...
request.setAttribute("result", "File upload finished!");
request.getRequestDispatcher("/upload.jsp").forward(request, response);

This way the message "File upload finished!" will be available by ${result} in the JSP and printed as such.

See also:

  • Our Servlets wiki page - contains a Hello World (with messaging)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜