get the complete file path in Current jsp - java

Hi I wrote the below code in a jsp file to upload a file. But before I upload I want to display the entire path of the file in the current jsp itself
<form method="post" action="SendTheFileName">
<div id="Files_to_be_shared">
<input type="file" id="File" name="FileTag" />
<input type="submit" value="Share" />
</div>
Complete path of the file is <%=request.getParameter("File");%>//Is this correct?
I am not getting the complete path value. I get null instead. Can you please let me know how to get the complete path in the current jsp itself

Jsp's are made on the server and file is uploaded by user, in browser, by the time it will be uploaded - there will be no scriplets on your html page at all, meaning those parameter value will be tried to obtain during jsp creating, where it is obviously null(unless request really contains such value). To run code on the client use javascript, for example here is how to do what you want using javascript.
P.S. consider using EL, scriplets are considered harmful, there are plenty of info about this on the net

Related

I would like to pass an Image tag data to servlet. How?

I have a jsp page where am displaying image (which is generated in servlet and passed with name in jsp) and accepting text. Now i would like to pass that image tag value and textarea value to another jsp page. I am able to get textarea value but how to retrieve image to servlet.
JSP code:
<form id="form1" action="CallServlet">
<img src="PieChartDemo" width="600" height="400" border="0" name="chartsample"/><br>
<textarea rows="4" cols="150" id=t1 name=t1></textarea>
<input type="Submit" value="Send"/>
</form>
Servlet Code:
PrintWriter out=response.getWriter();
PieChartDemo pcd=new PieChartDemo();
String textvalues=request.getParameter("t1");
System.out.println("Text values : "+textvalues);
out.println(textvalues);
Now i would like to display or store it into a variable that image. How to go about? Suggest.
Thanks in Advance.
you can not upload files using getParameter method.
for that u will have to use apache file upload.
Just go through the following link it will definatley solve your problem.
servlets-file-uploading

accessing information inside a jsp:include from the parent jsp

I'm trying to access form data that is filled out inside a jsp:included page from outside the page. I am using jquery and am open to using ajax. My code looks a little like this:
<form name=form1>
<jsp:include page="someFormData.jsp" />
//Other inputs out here in <% include %> files
<input type=button value=Submit onClick="return commonSubmit()"
</form>
I need to use the jsp:include style include because having everything on one page using
<%include...%> was causing an exception due to my jsp being too large. Alls I need to do is be able to submit the form and include the data inside "someFormData.jsp"

servlet not found by the browser when it is there

I am trying to handle the form-data and put it in the data base derbi that comes with netbeans.The server i am using is Glassfish. After filling the form when i click the submit data button, according to the action handler the request should follow to a servlet named FormHandler.do but when i try to reach out to the page FormHandler.do this is the error message displayed by the browser :
This webpage is not found
No webpage was found for the web address:
file:///W:/UnderTest/WebApplication_GLASSFISH/src/java/FormHandler/FormHandler.do
Error 6 (net::ERR_FILE_NOT_FOUND): The file or directory could not be found.
But the file is there in the same directory as the html file (that has form). This is the type of url i give in the form tag:
<form method="post" action="FormHandler.do">
<b>Name:</b><input type="text" name="Name" /> <br /> <br />
<b>Email:</b><input type="email" name="Email" /> <br /> <br />
<b>Password:</b><input type="password" name="Password" /> <br /> <br />
<input type="submit" value="Register" />
What could be the reason for this problem ?
(After the request goes to the servlet FormHandler.do it checks (as per logic) if the text fields are empty. If they are empty it forwards the request to a servlet that shows the errors and if correct it inserts that data into the database and shows the success servlet.)
No webpage was found for the web address:
file:///W:/UnderTest/WebApplication_GLASSFISH/src/java/FormHandler/FormHandler.do
You need to access web resources by a HTTP path, not by a local disk file system path. Something like as:
http://localhost:8080/WebApplication_GLASSFISH/FormHandler.do
The /WebApplication_GLASSFISH part is the context path. It's unclear which one you're using, but this information is printed in the server's startup log. Something like as this:
INFO: WebApplication_GLASSFISH was successfully deployed in 3,770 milliseconds.
You need to make sure that the URL in your browser address bar matches this.
Also, HTML files should go in "Web Pages", not in "Source Packages". After you move the Registration.html into "Web Pages", you can open it by
http://localhost:8080/WebApplication_GLASSFISH/Registration.html
This way the form will submit to the proper servlet URL assuming that you've a
<form action="FormHandler.do">
This all is rather trivial. I'd suggest to go through a bit decent Servlet book/tutorial once again. Put your mouse above the servlets tag which you put below the question and click at the info link to get a good starting point.

can we include both a file type and text in a form in sending email using jsp

I'm developing an email program using JSP. In that I need to send data as well as upload file.
<form name="email" enctype="multipart/form-data" method="post" action="/servlet/sendmail">
<input type="file" name="namefile" size="40">
<input type="text" size="100" name="sub">
<input type="submit" name="submit" value="send">
</form>
In servlet java program I can upload the file but the text fields returns null.
In doPost() method,
String msg=request.getParameter("sub");
Here getParameter() method returns null for text fields.
Can we include both file type and text in a single form with enctype="multipart/form-data"?
Yes, that's possible. You should obtain the text field using the same API as you've obtained the uploaded file. It's unclear which one you're using to obtain the uploaded file, so I can't give a detailed answer. But a defacto standard API to parse multipart/form-data requests is Apache Commons FileUpload and I've posted an answer before how to do that right here. To the point, you need to handle cases as well where FileItem#isFormField() returns true. This indicates a "regular" form field.

HTML Multiple form - Refresh Problem

I have got two forms in a page. The second form has got a file upload input component along with other controls. My requirement is to submit the second form with the file and refresh only that form without refreshing the other form. If it was only normal components, I could have done this easily in Ajax. But when the form is having a file component, I feel its not that straigh forward. Please suggest any ideas to do it???
You can still use AJAX on a form with file components. Maybe you can use the jQuery library (if you are not already) since that makes these tasks trivially easy.
Put the second form in an iframe.
The way I have done it in the past is to hide an iframe on the page. Then set the target of the file upload form to the name that was given to the iframe. If you need to be xhtml compliant you can use JavaScript to create the iframe after the page loads and to set the target on the form. The code will look something like this. You can apply css to the frame to hide it.
<iframe name="myFrame" src="blank.htm"></iframe>
<form action="uploadFile.php" method="post" enctype="multipart/form-data" target="myFrame">
<input type="file" name="myFile"/>
<input type="submit" value="Upload"/>
</form>

Categories