File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Code in servlet - doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Code in servlet - doubt" Watch "Code in servlet - doubt" New topic
Author

Code in servlet - doubt

Khushi Kapoor
Greenhorn

Joined: Apr 18, 2010
Posts: 11
Hi,

I had posted my problem on this forum earlier- about a deployment issue and a code problem. Since that post has been moved to the IDE category, so I'm posting my servlet code specific problem here.

Please explain how the servlet is using the fileroot and webroot ? What is the concept behind this? The only fact that I'm aware of is that these Webroot and Fileroot were specified in a conf.prop file that was a part of the sample application.


Functionality of the following lines in the blogservlet code- the concept of fileroot, webroot etc.


public void init() throws ServletException {
InputStream in = getClass().getResourceAsStream("/conf.prop");
Properties prop = new Properties ();
try {
prop.load (in);
fileroot = prop.getProperty("Fileroot");
webroot = prop.getProperty("Webroot");
} catch (Exception e) {
e.printStackTrace ();
fileroot ="/Users/juntao/Java/tomcat/jakarta-tomcat-4.1.24/webapps/BlogServer/content/";
webroot = "/BlogServer/content/";
}
}
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Please explain how the servlet is using the fileroot and webroot ?


Can't do it because you only show how the values for fileroot and webroot are read from a properties file or set to default values if the properties read fails.

Presumably the actual USE of these variables is somewhere else.

Bill

Khushi Kapoor
Greenhorn

Joined: Apr 18, 2010
Posts: 11
Hi ,

Thanks for the quick reply. Sorry. I should have stated my problem in a better way.

I am trying to deploy a sample multimedia application given in the NokiaSeries40 book codes, using NetBeans. The client end needs to submit a photo, an audio file and a message at the server end. I was not able to generate the .war file successfully as the webroot, file root concept was not clear.
I have uploaded the server end code of the application at the URL -(http://sites.google.com/site/cuteteddy02/file-cabinet)

From the source code files, here is the entire servlet code which is using webroot and fileroot .

public class BlogServlet extends HttpServlet {

private static int NO_OBJECT = 0;
private static int PHOTO_ONLY = 1;
private static int AUDIO_ONLY = 2;
private static int PHOTO_AUDIO = 3;

private static int SUCCESS = 1;
private static int FAILURE = 2;

private static String fileroot = null;
private static String webroot = null;

public void init() throws ServletException {
InputStream in =
getClass().getResourceAsStream("/conf.prop");
Properties prop = new Properties ();
try {
prop.load (in);
fileroot = prop.getProperty("Fileroot");
webroot = prop.getProperty("Webroot");
} catch (Exception e) {
e.printStackTrace ();
fileroot ="/Users/juntao/Java/tomcat/jakarta-tomcat-4.1.24/webapps/BlogServer/content/";
webroot = "/BlogServer/content/";
}

}

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

response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println(readTextFile("header.html"));
out.println(readTextFile("entries.html"));
out.println(readTextFile("footer.html"));

out.flush ();
out.close ();
}

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

response.setContentType("application/binary");

InputStream in = request.getInputStream();
OutputStream out = response.getOutputStream();
DataInputStream din = new DataInputStream(in);
DataOutputStream dout = new DataOutputStream(out);

String current =
Long.toString((new Date()).getTime());
String body = "<b>Posted at</b>: " +
(new Date()).toString() + "<br/>";
String filename;

// Get the opcode
int opcode = din.readInt();
// Get the message body
body += din.readUTF();
body += "<br/>";

if (opcode == NO_OBJECT) {
// Save the message body
saveMessage (body);
dout.writeInt(SUCCESS);

} else if (opcode == PHOTO_ONLY) {
// Get photo data
String photoType = din.readUTF ();
byte [] photoData = receiveObject (din);

// Save the photo data
filename = current + "." + photoType;
saveObject (photoData, filename);
body = body + photoHtml(filename);

// Save the message body
saveMessage (body);
dout.writeInt(SUCCESS);

} else if (opcode == AUDIO_ONLY) {
// Get the audio data
byte [] audioData = receiveObject (din);

// Save the audio data in a file
filename = current + ".wav";
saveObject (audioData, filename);
body = body + audioHtml(filename);

// Save the message body
saveMessage (body);
dout.writeInt(SUCCESS);

} else if (opcode == PHOTO_AUDIO) {
// Get the photo data
String photoType = din.readUTF ();
byte [] photoData = receiveObject (din);
// Get the audio data
byte [] audioData = receiveObject (din);

// Save the photo data in a file
filename = current + "." + photoType;
saveObject (photoData, filename);
body = body + photoHtml(filename);

// Save the audio data in a file
filename = current + ".wav";
saveObject (audioData, filename);
body = body + audioHtml(filename);

// Save the message body
saveMessage (body);
dout.writeInt(SUCCESS);

} else {
dout.writeInt(FAILURE);
}
dout.flush();
dout.close();
din.close();
in.close();
out.close();
}

private byte [] receiveObject (DataInputStream din)
throws IOException {
int length = din.readInt();
byte [] buf = new byte [length];
din.readFully (buf);
return buf;
}

private void saveObject (byte [] data, String name)
throws IOException {
FileOutputStream fout =
new FileOutputStream(fileroot + name);
fout.write(data, 0, data.length);
fout.flush ();
fout.close ();
}

private void saveMessage (String body)
throws IOException {
body = body + "<hr/>";

FileWriter writer =
new FileWriter (fileroot + "entries.html", true);
writer.write(body, 0, body.length());
writer.flush ();
writer.close ();
}

private String photoHtml (String filename) {
return "<img src=\"" + webroot + filename +
"\"/>" + "<br/>";
}

private String audioHtml (String filename) {
return "<a href=\"" + webroot + filename +
"\">" + "Audio file (wav format)</a>" + "<br/>";
}


private String readTextFile (String name)
throws IOException {
// FileInputStream fin =
// new FileInputStream(fileroot + name);
// ByteArrayOutputStream baos =
// new ByteArrayOutputStream ();
// byte [] buf = new byte [256];
// int i = 0;
// while ( (i = fin.read(buf)) != -1 ) {
// baos.write(buf, 0, i);
// }
// baos.flush();
// byte [] result = baos.toByteArray();
// baos.close();
// fin.close();
// return result;

StringBuffer result = new StringBuffer ();
FileReader reader =
new FileReader (fileroot + name);

char [] buf = new char [256];
int i = 0;
while ( (i = reader.read(buf)) != -1 ) {
result.append(buf, 0, i);
}
reader.close();
return result.toString ();
}
}

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

Please UseCodeTags.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 605

Ok - here is a guess without giving the code a read.

The Webroot as the name suggest is the location where the web application is deployed. Its being used to store the uploaded multimedia.
The FileRoot is the location where it reads a text file -- I saw it being used in readTextFile/writeTextFile.

To be honest the example looks way complicated than it should have been .... If your on a windows machine then you'll need tio change the webroot/fileroot to something more suitable than that in the example -- something starting with C:/ (probably)....


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Khushi Kapoor
Greenhorn

Joined: Apr 18, 2010
Posts: 11
Thank You so much Problem resolved. Just changed the directory to the one on my local machine.
 
 
subject: Code in servlet - doubt