/    Sign up×
Community /Pin to ProfileBookmark

Print content of a file in an areatext on same page

I need a single page with a file upload and a text area where the contents of the file are printed there.

At the moment I have a jsp file and a servlet:

Part of index.jsp :

<form action=”FileReader” ENCTYPE=”multipart/form-data” method=”POST”>
<textarea name=”textinputarea” rows=”14″ cols=”130″ readonly>
Some text </textarea>

</td>
</tr>

<!– Tools Selection –>
<tr>
<td valign=”top” align=”left” height=”200″ width=”33%”>
<img class=”start_img” src=”file_Selections.jpg”> <br> <br>
<input type=”hidden” name=”MAX_FILE_SIZE” value=”100000″ />
<input type=”file” name=”user_file” accept=”text/xml”>
<input type=”submit” value=”Validate” /> <br>

</form>

Here is the “POST” part of the servlet:

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String name = request.getParameter(“textinputarea”);

boolean isMultipart = ServletFileUpload.isMultipartContent(request);
response.setContentType(“text/html”);
PrintWriter out = response.getWriter();

try {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
Iterator<FileItem> iterator = upload.parseRequest(request).iterator();
File uploadedFile = null;
String dirPath=”C:\fileuploads”;
while (iterator.hasNext()) {

FileItem item = iterator.next();
if (!item.isFormField()) {

String fileNameWithExt = item.getName();

File filePath = new File(dirPath);

if (!filePath.exists()) {
filePath.mkdirs();
}

uploadedFile = new File(dirPath + “/” + fileNameWithExt);
item.write(uploadedFile);
}
else {
String otherFieldName = item.getFieldName();
String otherFieldValue = item.getString();}}

FileInputStream fstream = new FileInputStream(uploadedFile);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine = null;

out.println(“<html>”);
out.println(“<head>”);
out.println(“<title>Processing get requests with data</title>”);
out.println(“</head>”);

// body section of document
out.println(“<body>”);
while ((strLine = br.readLine()) != null) {

// Print the content on the console
out.println(strLine + “</br>”);
}
out.println(“</body>”);

// end of html document
out.println(“</html>”);
out.close();

} catch (Exception e) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
};
}

This is actually prints the content of file in a new page. I tried to give the same name of text area and the “String name = request.getParameter(“textinputarea”); “..

Thanks for your time!

to post a comment
HTML

0Be the first to comment 😎

×

Success!

Help @AngCy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...