andra kan

Greenhorn
+ Follow
since May 06, 2005
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by andra kan

If Incase cookies are disabled can we use the getsession functionality to
check if the request is a first time request or not.

request.getsession(false) will return a session object if one exists already.

For a first time request, this would be null and from this we can identify
a first time request.

( create a new session if null)

For subsequent requests, this wont be null..

I hope this will work.
Thanks ,
Andra
13 years ago
JSP
From my understanding,

HTML and Javascript run on client side , So if your form needs to process both Javascript and Servlets, the javascript functions that have
been called will normally execute, and only on form submit, the Servlet will be invoked ( if the form action is the servlet ).

And in the servlet, the form fields can be accessed as request parameters.

In the Javascript the form fields are accessed as form fields only....

Hope this is Useful,
Regards,
Andra
13 years ago
JSP
Found some related material. Hope this is useful for you.

PUT
The PUT type request is a means of uploading files to the server. While uploading is its original intent, I have not seen it used much. Instead, POST is generally used to upload files. The PUT handler has this syntax:

public void doPut(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{ //your code here}
The doPut() method is called by the server (via the service method) to handle a PUT request. Uploading files from a browser has always been difficult. The idea behind the PUT operation is to make uploading straightforward. It is supposed to allow a client to place a file on the server, just like sending a file by FTP. The javadoc for this method warns that when overriding this method, you should leave intact any content headers sent with the request (including Content-Length, Content-Type, Content-Transfer-Encoding, Content-Encoding, Content-Base, Content-Language, Content-Location, Content-MD5, and Content-Range). This method is rarely used, but it is powerful if you need it.

Listing 4.5 is a simplified HTML page that creates a file upload page that will direct the file contents to a servlet.

Listing 4.5 HTML Form Example
<html>
<body>
<form enctype="multipart/form-data" method="PUT"
action="localhost:8080/examples/servlet/UploadServlet">
<input type="file" size="20" name="FileToUpload"
value="Select File">
<input type="submit" name="UploadFile" value="Upload">
<input type="reset" value="Reset">
</form>
</body>
</html>
Listing 4.6 is a servlet that can accept an uploaded file.

Listing 4.6 Servlet That Handles a File Upload from the Client
import java.io.*;
import java.util.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class UploadServlet extends HttpServlet
{
static final String dir="C:/temp";

public void doPut(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException
{
PrintWriter outHTML = res.getWriter();
outHTML.println("done");

try
{
int i;
InputStream input;
input = req.getInputStream();
BufferedInputStream in =
new BufferedInputStream(input);
BufferedReader reader =
new BufferedReader(
new InputStreamReader(in));
File outputFile =
new File("c:/temp/out.txt");
FileWriter out =
new FileWriter(outputFile);

while ((i = reader.read()) != -1)
{
out.write(i);
}

out.close();
in.close();

}
catch (IOException e) {}
}
}
You need to account for the header and footer lines that the stream attaches to the actual file contents. The stream looks like this:

------------BB1rHqKAOHkiUoiFS3VI6v
Content-Disposition: form-data; name="FileToUpload";
filename="Candle.txt"
Content-Type: application/octet-stream; name="Candle. txt"

// ...
//actual file content here
// ...

------------BB1rHqKAOHkiUoiFS3VI6v
Content-Disposition: form-data; name="UploadFile"

Upload
------------BB1rHqKAOHkiUoiFS3VI6v
Therefore, you will need to search (indexOf) for the start of the actual file content by looking for the Content-Type and subsequent name parameters like so:

int headerEnd = line.indexOf("Content-Type: ");
headerEnd = line.indexOf("name=\"", headerEnd);
headerEnd = line.indexOf("\"", headerEnd + 7); //last quote
Likewise, you need to search the end of the file for the telltale Content-Disposition and preceding "------------" marker like so:

int footerStart =
line.lastIndexOf ("Content- Disposition: ");
footerStart = line.lastIndexOf ("---------", footerStart);
Lastly, you will grab the text between the two like so:

fileContent = line.substring(headerEnd, footerStart);
You can refer to RFC 1867 to learn more about uploading files through an HTML form (http://www.servlets.com/rfcs/rfc1867.html). This is all tedious, so you might just grab an open source (http://www.servlets.com/cos/index.html) or commercial Bean that uploads files such as uploadBean (http://www.javazoom.net/jzservlets/uploadbean/uploadbean.html) or jspSmartUpload (http://www.jspsmart.com/).

Listing 4.6 worked when placed in the doPost() method (and the form method of Listing 4.5 is set to post), but did not work in the doPut() method using IE or Opera against Tomcat (version 4). I verified that the doPut() method is called as expected in the servlet. However, even after much tweaking, this file upload code failed when placed in the doPut method as shown previously. If you only change doPut to doPost it works?! Although I need to research this problem with Tomcat, you do need to understand that PUT is used to upload files to a Web server and that this is usually done by non-browser, client-side Web content development tools.
If the jar file is to be used by the EJB , then when the EJB jar is being created this file should also be included.

Hope this helps.
13 years ago