looking for a servlet expert

can anyone explain to me how to write a file to the server using a servlet?everything i've tried as not been workingthanksAndy
[162 byte] By [andyhuck] at [2007-9-19]
# 1
HiServlets run on the server side, so the normal way to write filesshould work ? What are the issues that you are running into ?-Manish
manishdixit at 2007-7-4 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 2

thats what i thought about the way to write to the files, but i believe that i'm having troubles actually getting to the servlet code.

here is what i have so far...

//*********applet***********/

try

{

URL baseURL = getCodeBase();

String protocol = baseURL.getProtocol();

String host = baseURL.getHost();

int port = 808;

String urlSuffix = "/servlet/contactlist.servletOne";

URL url = new URL(protocol, host, port, urlSuffix);

URLConnection conn = url.openConnection();

// inform the connection that we will send output and accept input

conn.setDoInput(true);

conn.setDoOutput(true);

// Don't use a cached version of URL connection.

conn.setUseCaches (false);

conn.setDefaultUseCaches (false);

InputStream inputStreamFromServlet = conn.getInputStream();

inputStreamFromServlet.read();

inputStreamFromServlet.close();

System.out.println("testing for the end8");

}

catch(IOException io) { io.printStackTrace(); }

//********servlet***********/

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

{

System.out.println("get");

try

{

FileWriter f = new FileWriter("G:\\Intranet\\wwwroot\\Contact\\contactlist\\Departments\\test.htm");

f.write("testing");

f.flush();

f.close();

}

catch(IOException io) { io.printStackTrace(); }

}

i've never been able to see the "get" in the log.

andyhuck at 2007-7-4 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 3

i check tomcat's localhost_access_log and it tells me this

10.1.2.230 - - [09/Apr/2002:13:59:28 -0500] "GET /servlet/contactlist.servletOne HTTP/1.1" 200 -

this means that i am calling the servlet? doesn't it?

so i'm thinking that the applet and servlet are talking to each other, but i just am not calling the doGet method correctly.

Andy

andyhuck at 2007-7-4 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 4

Yes, it means that tomcat received an HTTP GET request for the "/servlet/contactlist...", and that the URL was found. (200 == OK).

This kind of problems can be somewhat tricky. I suggest:

1) in your servlet, write something to the response - response.getWriter().write("Hi there!")

2) call the servlet from a browser. if you see the message, your servlet is called.

3) If your servlet is not called, tell me what response you get in the browser.

Note that the System.out.println() probably does not write to the standard log - it will most likely either write to the DOS console or to a special system_out log.

johannef at 2007-7-4 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 5
ok, i tried what you suggested johannef, butthe java plugin console, and the browser java console gave me no output.
andyhuck at 2007-7-4 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 6
The servlet is running in your server, so System.outs will never reach your clients plugin console.The way johannef does it, should show you the string in your browser window just like if you called an html page. It's not printed in any console window.
abnormal at 2007-7-4 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 7

ok, i tried to typed in the address of the servlet into my browser

but i just got a blank screen

i didn't get an error, so this means that the file is there.

could this mean that i do not have the proper permissions?

or will typing the address into the browser, not have the same effect as running it through the applet?

andyhuck at 2007-7-4 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 8
Isn't there an error log in the server you can look at. If it couldn't find the source you tried to load in the browser, there should be an error in the log.Perhaps you haven't configured the servlet properly.
abnormal at 2007-7-4 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 9
i check the error logs for tomcat and the server and no errors were there.the only configuration as far at the servlet goes has only been done in the code that is posted above.is that what you mean by configuring the servlet? or is there something else i should do?
andyhuck at 2007-7-4 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 10
Did you searched for the test.htm file which you are creating while serving get request. Its presence with the required content will mark your servelet OK.
rabhishek at 2007-7-4 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 11
nevermind, i found the error. my server's system administrator isn't too familiar with tomcat and when i just got him to restart the service, everything is working great now.Andy
andyhuck at 2007-7-4 > top of java,Enterprise & Remote Computing,Web Tier APIs...