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 the current page I want to print that in a <div>
?
Asked
Active
Viewed 2,245 times
2 Answers
1
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.
@BalusC's answer sketches how you would implement this the right way ... using a servlet and a JSP.
-
Sir the eg is right i use UploadBean and whene i upload a file the resulte is some informations about the file and i want to print this informations in a div in the current html page not in a new page , is there any way to do it ?? – Dilllllo May 21 '11 at 01:46
-
If you want to display information in the current page, you can't do it using JSPs. JSPs always involve sending stuff to the browser in new page ... even if that page has the same URL as the one the user was previously looking at. You need to look at an AJAX technology to do this kind of thing. – Stephen C May 21 '11 at 02:14
0
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)