views:

48

answers:

1

I have created two JSP programs as follows.

First One: Addmulti.jsp

<html>
<head><title>Upload Excel File</title></head></p> <p><body>
<form action="Test2.jsp" method="post" enctype="multipart/form-data" name="form1" id="form1">
<br><br>
Upload File:
<input name="file" type="file" id="file"><br><br>
<input type="submit" name="Submit" value="Submit"/><br><br>
<input type="reset" name="Reset" value="Reset"/>
</form>
</body>
</html>

Second one: Test2.jsp

<%@ page import="myfirst.*" %>
<%

String filevar=request.getParameter("file");
String result="";
myfirst.SearchLink z=new myfirst.SearchLink();
result=z.addmultiple(filevar);
System.out.println(result);
out.println(result);
%>

SearchLink is a java program which contains the method as below,

public String addmultiple(String file)throws SQLException{
        return file;
}

I want the complete path of the file that is selected in the first jsp program mentioned above to be transmitted to the java method named addmultiple(String). Instead, null is getting printed in the browser as output once the Test2.jsp program is called.

What actually will be passed to the method with the parameter String in the code described above?

How to send the complete path of the file that is selected in the first jsp code to the java program? Please advise.

+2  A: 

You can't.

First, request.getParameter() will always return null in multipart/form-data requests. You need to parse the request body. How to do this is answered and commented in your previous question.

Second, the webbrowser is supposed to send only the file name along the file content. MSIE is the only webbrowser which (incorrectly) sends the full path along the name. You should not be relying on that. You should also not be interested in the file path. What can you do with it? Open a file using java.io.File or so? How would you get that to work when webbrowser and webserver runs at physically different machines?

See also:

BalusC
Using the filepath, I want my java program to access a excel file and compare its contents with the database.
LGAP
You should not be interested in the file path or name, but rather on the file content. Did you read and understand the answer including the links? This is a pretty common misunderstanding among starters. Say, I have a file here at my disk named `c:/path/to/passwords.txt` and I give you the path. Can you now tell me the contents? No? How would the webserver ever be able to do this?
BalusC
yes I read those links. I am quite unaware of the servelets and doPost() methods. I really dont know how to implement those to my present program. Could you please advise?
LGAP
The first link contains the complete example. You have only to expand it yet and register it in web.xml. There's a little tutorial with useful links in the [servlets tag info](http://stackoverflow.com/tags/servlets/info) page.
BalusC
I dont really deal with two different machines. All my executions said above are in a single machine only. Even in this case, do we use Servelets?
LGAP
In **real world** the webserver and webbrowser runs at a different machines. And yes, the servlet is the **normal practice**. Even more, Java code should **not** go in a JSP file. See also: [How to avoid Java code in JSP?](http://stackoverflow.com/questions/3177733/howto-avoid-java-code-in-jsp-files).
BalusC
Whether integrating the doPost() method of the "How to upload files in JSP/Servlet?" example to my SearchLink.java program will work? Or it has to be integrated in the JSP code itself? Please let me know.
LGAP
No, certainly no Java code in JSP file. Java code should go in a Java class. JSP should only contain template text. Create a class which `extends HttpServlet`. See also the [servlets tag info](http://stackoverflow.com/tags/servlets/info) for a helloworld example and tutorial links.
BalusC
Thanks Balus for your patient replies. i will definitely read those tutorials and let you know if I am done with the required task.
LGAP