The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Writing on Server
Discuss Writing on Server in the Java Help forum on Dev Shed. Writing on Server Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 25th, 2001, 04:38 PM
|
|
Contributing User
|
|
Join Date: Jul 2000
Posts: 34
Time spent in forums: < 1 sec
Reputation Power: 13
|
|
How can I write information into a file on my server in java servlets?
|

January 25th, 2001, 06:22 PM
|
 |
film at 11
|
|
Join Date: Aug 2000
Location: Portland, OR
Posts: 413
Time spent in forums: < 1 sec
Reputation Power: 13
|
|
go to the Java API and look up the java.io package. you write to a file from a servlet the same way you write to a file from any other java code (except applets). i advise using a FileWriter wrapped in a BufferedWriter. good luck. here's another link to the servlet API. the APIs are your best friends. when i have a question about how to implement something, i go there and 90% of the time I find the answer.
|

January 25th, 2001, 09:37 PM
|
|
Contributing User
|
|
Join Date: Jul 2000
Posts: 34
Time spent in forums: < 1 sec
Reputation Power: 13
|
|
I've already tryed a FileWriter wrapped in a BufferOutputStream. I have the following code:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.net.*;
public class MindmapWWWBoard extends HttpServlet {
String username;
String message;
private Writer out2;
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
postReceipt(request, out);
}
private void postReceipt(HttpServletRequest request,
PrintWriter out) {
username = request.getParameter("username");
message = request.getParameter("message");
out.println("<html>n" +
"<head>n" +
"<title>n" +
"Your Postn" +
"</title>n" +
"</head>n" +
"<body>n" +
"<b>Username:</b> " + username + "<br>" +
"<b>Message:</b> " + message +
"</body>n" +
"</html>");
try {
FileWriter fw = new FileWriter("mindmap.html", true);
BufferedWriter out2 = new BufferedWriter(fw);
out2.write(message);
out2.flush();
out2.close();
}
catch (IOException e) { }
}
}
Does anyone know why this isn't working? Thanks in advance.
|

January 26th, 2001, 10:05 AM
|
 |
film at 11
|
|
Join Date: Aug 2000
Location: Portland, OR
Posts: 413
Time spent in forums: < 1 sec
Reputation Power: 13
|
|
|
what is the behavior? are there any error messages?
|

January 26th, 2001, 10:17 AM
|
|
Contributing User
|
|
Join Date: Jul 2000
Posts: 34
Time spent in forums: < 1 sec
Reputation Power: 13
|
|
It compiles ok, but when I run it on the server, it doensn't write to the HTML file. It shows the username and message after post.
|

January 26th, 2001, 12:19 PM
|
 |
film at 11
|
|
Join Date: Aug 2000
Location: Portland, OR
Posts: 413
Time spent in forums: < 1 sec
Reputation Power: 13
|
|
how about this:
Code:
File file = new File("/home/your/path", "mindmap.html");
byte[] fileBuffer;
FileOutputStream outstream;
FileInputStream instream;
int fileLength;
if (file.exists()) fileLength=(int)file.length();
else fileLength=0;
// buffer for your message + file's contents
fileBuffer = new byte[message.length() + fileLength];
// stream in the file to the buffer
if (file.exists()) {
try {
inStream = new FileInputStream(file);
int numRead = 0;
int counter = 0;
while (numRead!=-1 && counter<fileLength) {
numRead = inStream.read(fileBuffer, counter, fileLength-counter);
counter += numRead;
}
}
catch...
}
byte[] messageBytes = message.getBytes();
//add message to the file buffer
for (int j=fileLength; j<fileBuffer.length; j++)
fileBuffer[j] = messageBytes[j-fileLength];
//shove fileBuffer back into file
try {
outStream = new FileOutputStream(file);
outStream.write(fileBuffer);
outStream.close();
}
catch...
that's bound to have a bug or two but i'm sure you can fix it up.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|