Submit a text file to an applet or servlet?

  • Thread starter Per Magnus L?vold
  • Start date
P

Per Magnus L?vold

Hi!
I am trying to figure out how to process data submitted with a text
file in either an applet or servlet.

In the browser the user should be able to submit the text file (to be
processed) through a form. By specifying the file in the form and
clicking "submit", the applet or the servlet should parse the content
of the text file and store it in a database (using JDBC).

I was thinking of making a JSP page first, but I am not sure if it is
possible to submit a text file from the client to the serverside JSP
page so that it can be parsed in the JSP page. This would seem to me
as the easiest solution.

Hope someone can help me!

Regards, Per Magnus
 
A

Andrew Thompson

I am trying to figure out how to process data submitted with a text
file in either an applet or servlet.

The applet would generally require server side
functionality to back it up, read JSP, Servlet,
ASP..

I suggest you dump the applet and go straight
I was thinking of making a JSP page first, but I am not sure if it is
possible to submit a text file from the client to the serverside JSP
page so that it can be parsed in the JSP page.

It is. But ..well, do you have a *book* on
web-apps? Getting parameters form the URL
(included in an HTTP GET) or the POST information
is fairly trivial.

E.G. in a JSP..
String text = request.getParameter("text");
 
P

Per Magnus L?vold

Thanks, Andrew!
Let me see if I've got it right:

First I need a HTML page wich submits the file til the JSP: (This is
trivial)
***
* Submitting page:
***
..
..
<form action="receiver.jsp" method=get>
<input type="file" name="myFile">
<input type=submit>
..
..

***
* receiver.jsp
***
..
..
File myFile = new File(request.getParameter("myFile");
..
..

I understand this doesn't work, and wondered what 'magic code' I have
to add to be able to parse the text from the submitted file?
P.S: I wish not to save the submitted text file on the server. Only
parse it's contents and process some logic without having to save it.

Regards, Per Magnus
 
Z

zoopy

Thanks, Andrew!
Let me see if I've got it right:

First I need a HTML page wich submits the file til the JSP: (This is
trivial)
***
* Submitting page:
***
..
..
<form action="receiver.jsp" method=get>
<input type="file" name="myFile">
<input type=submit>
..
..

***
* receiver.jsp
***
..
..
File myFile = new File(request.getParameter("myFile");
..
..

I understand this doesn't work, and wondered what 'magic code' I have
to add to be able to parse the text from the submitted file?
P.S: I wish not to save the submitted text file on the server. Only
parse it's contents and process some logic without having to save it.

Regards, Per Magnus

Probably the easiest is to use a library capable of parsing and handling file-uploads. Jakarta
Commons provides such a library: FileUpload <http://jakarta.apache.org/commons/fileupload/>. See
user guide for how to use it <http://jakarta.apache.org/commons/fileupload/using.html>.

HTH,
Z.
 
S

Sudsy

Per Magnus L?vold wrote:
<form action="receiver.jsp" method=get>

<form action="receiver.jsp" method="POST" enctype="multipart/form-data">

File myFile = new File(request.getParameter("myFile");

This would depend on your servlet container. I use Struts so everything
gets wrapped nicely as a FormFile. There are alternatives.
 
T

Tor Iver Wilhelmsen

<form action="receiver.jsp" method=get>
<input type="file" name="myFile">
<input type=submit>

No: You will need to use method="post" (not get) and set
enctype="multipart/form-data" to put the values into the body in a way
that file input is designed for.

To receive the data, you will either need to trust the servlet
container to parse the multipart data, or use Java Activation
Framework's MIME APIs to parse it yourself.
 
C

Chris Smith

Per said:
File myFile = new File(request.getParameter("myFile");
.
.

I understand this doesn't work, and wondered what 'magic code' I have
to add to be able to parse the text from the submitted file?
P.S: I wish not to save the submitted text file on the server. Only
parse it's contents and process some logic without having to save it.

There is no magic code. It's a rather complicated process, and one for
which you'd typically use a third-party library. See
http://jakarta.apache.org/commons/fileupload/

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top