• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Code in servlet - doubt

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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/";
}
}
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ();
}
}

 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags.
 
Ranch Hand
Posts: 608
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)....
 
Khushi Kapoor
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You so much:) Problem resolved. Just changed the directory to the one on my local machine.
 
reply
    Bookmark Topic Watch Topic
  • New Topic