Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2004

Tip: Looking for answers? Try searching our database.

How to update  a file on a server from a JSP page

Thread view: 
J?r?me VUIBERT - 27 Feb 2004 15:44 GMT
Hi.

I have to update a file on the server where a JSP file is .

To read the previous value, I use an InputStream, and close it.
I then do the update in my object representing the data file.
I use an OutputStream, do the flush, close the stream, but the file still
stay unupdated, and no error is throwed

Can someone give me a solution ?

Jerome

======================

<%@ page language="java" %>
......
<%@ page import="......." %>
<%
 String                 l_szJalonFileName        =
"http://the_server_address/data.xml";
java.net.URL           l_fileURL                = null;
JAXBContext            l_jc                     = null;
DataClass l_data            = null;

// Read the old value
try
{
    l_jc = JAXBContext.newInstance("data.jaxb");
    Unmarshaller unmarshaller = l_jc.createUnmarshaller();
    l_fileURL = new java.net.URL(l_szJalonFileName);
    java.net.URLConnection l_connection = l_fileURL.openConnection();
    java.io.InputStream l_is = l_connection.getInputStream();
    l_data = (DataClass ) unmarshaller.unmarshal(l_is);
    l_is.close();
}
   catch(Exception e)
   {
    // -- study error messages
   }

   // Update the value in the l_data object

   // Write the new value in the previous file
   try
   {
    Marshaller marshaller = l_jc.createMarshaller();
    java.net.URLConnection l_connection = l_fileURL.openConnection();
    l_connection.setDoOutput(true);
    java.io.OutputStream l_os = l_connection.getOutputStream();
    marshaller.marshal(l_sessionList, l_os);
    l_os.flush();
    l_os.close();
}
   catch(Exception e)
   {
    // -- study error messages
   }
%
Oscar kind - 27 Feb 2004 17:25 GMT
> Hi.
>
[quoted text clipped - 4 lines]
> I use an OutputStream, do the flush, close the stream, but the file still
> stay unupdated, and no error is throwed

[...]
>  String                 l_szJalonFileName        =
> "http://the_server_address/data.xml";
[...]

Please note you don't have a filename: you have a URL. If you send data to
it, it'll just be ignored (as it's a file; not a servlet, JSP page or CGI
program). It's probably possible to write to it (maybe using Apache), but
then the entire world can write to it (with the right password probably).

I cannot but wonder: If the file is on the same server as the JSP, why not
simply use a file name? After all, a JSP page is executed on the server,
and thus a file on that same server is just a local file.

Oscar

Signature

Oscar Kind                                    http://home.hccnet.nl/okind/
Java/J2EE Developer                             email available on website

Chris Smith - 28 Feb 2004 18:43 GMT
Jérôme VUIBERT wrote:
> To read the previous value, I use an InputStream, and close it.
> I then do the update in my object representing the data file.
> I use an OutputStream, do the flush, close the stream, but the file still
> stay unupdated, and no error is throwed

Jerome,

You don't know if an error is thrown or not, because you have a catch
block that catches the error and ignores it.  Try commenting out that
try/catch and testing again.

Additionally, you seem to be assuming that JSP code runs on the client,
and trying to use a URLConnection to talk to the server.  That's not the
case; JSP code runs on the server, and you should use FileInputStream
and FileOutputStream to interact with files.  Using URLConnection means
that you're taking the round-about route and going through the web
server; no web server is likely going to let you modify a file.

Signature

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.