aaronyeo22
asked on
jsp to servlet
sunu340 : i need your help. i was succeeded to get that two file name. but how do i pass this two file names to servlet.
because in servlet i need string this two files.
because in servlet i need string this two files.
ASKER
sunu340 was provided the solution to me. In servlet i need to string request.getParameter that two files.
how do i string in servlet. thanks
how do i string in servlet. thanks
ASKER
<fieldset>
<legend>Upload File</legend>
<form action="asdas" method="post" enctype="multipart/form-da ta">
<label for="filename_1">File: </label>
<input id="filename_1" type="file" name="filename_1" size="50"/><br/>
<label for="filename_2">File: </label>
<input id="filename_2" type="file" name="filename_2" size="50"/><br/>
<br/>
<input type="submit" value="Upload File"/>
</form>
</fieldset>
index.jsp
-------------------------- ---------- ---------- ---------- ------
import org.apache.commons.fileupl oad.servle t.ServletF ileUpload;
import org.apache.commons.fileupl oad.FileIt emFactory;
import org.apache.commons.fileupl oad.FileUp loadExcept ion;
import org.apache.commons.fileupl oad.FileIt em;
import org.apache.commons.fileupl oad.disk.D iskFileIte mFactory;
import javax.servlet.http.HttpSer vlet;
import javax.servlet.http.HttpSer vletReques t;
import javax.servlet.http.HttpSer vletRespon se;
import javax.servlet.ServletExcep tion;
import java.io.IOException;
import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
public class asdas extends HttpServlet {
private static final long serialVersionUID = -3208409086358916855L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("t ext/plain" );
boolean isMultipart = ServletFileUpload.isMultip artContent (request);
if (isMultipart) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory) ;
try {
List items = upload.parseRequest(reques t);
Iterator iterator = items.iterator();
//int i=0;
List<String> filenames = new ArrayList<String>();
while (iterator.hasNext()) {
FileItem item = (FileItem) iterator.next();
if (!item.isFormField()) {
String fileName = item.getName();
//i=i+1;
String root = getServletContext().getRea lPath("/") ;
File path = new File(root + "/uploads");
if (!path.exists()) {
boolean status = path.mkdirs();
}
File uploadedFile = new File(path + "/" + fileName);
out.println(item.getName() );
filenames.add(fileName);
item.write(uploadedFile);
}
}
request.getSession().setAt tribute("i tem", filenames);
response.sendRedirect("cde .jsp");
} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
asdas.java
-------------------------- ---------- ---------- ----
<%
java.util.List<String> filenames = (java.util.List<String>)re quest.getS ession().g etAttribut e("item");
for(String filename: filenames) {
%>
<input id="filename_1" type="text" name="filename_1" size="50" value='<%=filename%>'/><br />
<%
}
%>
cde.jsp
-------------------------- --
filename_1 contains two files name and how do i pass to servlet. i need to string this two files.
thanks. please help.
<legend>Upload File</legend>
<form action="asdas" method="post" enctype="multipart/form-da
<label for="filename_1">File: </label>
<input id="filename_1" type="file" name="filename_1" size="50"/><br/>
<label for="filename_2">File: </label>
<input id="filename_2" type="file" name="filename_2" size="50"/><br/>
<br/>
<input type="submit" value="Upload File"/>
</form>
</fieldset>
index.jsp
--------------------------
import org.apache.commons.fileupl
import org.apache.commons.fileupl
import org.apache.commons.fileupl
import org.apache.commons.fileupl
import org.apache.commons.fileupl
import javax.servlet.http.HttpSer
import javax.servlet.http.HttpSer
import javax.servlet.http.HttpSer
import javax.servlet.ServletExcep
import java.io.IOException;
import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
public class asdas extends HttpServlet {
private static final long serialVersionUID = -3208409086358916855L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("t
boolean isMultipart = ServletFileUpload.isMultip
if (isMultipart) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory)
try {
List items = upload.parseRequest(reques
Iterator iterator = items.iterator();
//int i=0;
List<String> filenames = new ArrayList<String>();
while (iterator.hasNext()) {
FileItem item = (FileItem) iterator.next();
if (!item.isFormField()) {
String fileName = item.getName();
//i=i+1;
String root = getServletContext().getRea
File path = new File(root + "/uploads");
if (!path.exists()) {
boolean status = path.mkdirs();
}
File uploadedFile = new File(path + "/" + fileName);
out.println(item.getName()
filenames.add(fileName);
item.write(uploadedFile);
}
}
request.getSession().setAt
response.sendRedirect("cde
} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
asdas.java
--------------------------
<%
java.util.List<String> filenames = (java.util.List<String>)re
for(String filename: filenames) {
%>
<input id="filename_1" type="text" name="filename_1" size="50" value='<%=filename%>'/><br
<%
}
%>
cde.jsp
--------------------------
filename_1 contains two files name and how do i pass to servlet. i need to string this two files.
thanks. please help.
ASKER
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
performTask(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
performTask(request, response);
}
private void performTask(HttpServletReq uest request, HttpServletResponse response) throws ServletException,
IOException {
String pdfFileName = "ddd/cdee.pdf";
System.out.println(pdfFile Name);
String contextPath = getServletContext().getRea lPath(File .separator );
File pdfFile = new File(contextPath + pdfFileName);
response.setContentType("a pplication /pdf");
response.addHeader("Conten t-Disposit ion", "attachment; filename=" + pdfFileName);
response.setContentLength( (int) pdfFile.length());
FileInputStream fileInputStream = new FileInputStream(pdfFile);
OutputStream responseOutputStream = response.getOutputStream() ;
int bytes;
while ((bytes = fileInputStream.read()) != -1) {
responseOutputStream.write (bytes);
}
}
how do i use cde.jsp pass parameter to servlet string:
String pdfFileName = "ddd/cdee.pdf";
performTask(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
performTask(request, response);
}
private void performTask(HttpServletReq
IOException {
String pdfFileName = "ddd/cdee.pdf";
System.out.println(pdfFile
String contextPath = getServletContext().getRea
File pdfFile = new File(contextPath + pdfFileName);
response.setContentType("a
response.addHeader("Conten
response.setContentLength(
FileInputStream fileInputStream = new FileInputStream(pdfFile);
OutputStream responseOutputStream = response.getOutputStream()
int bytes;
while ((bytes = fileInputStream.read()) != -1) {
responseOutputStream.write
}
}
how do i use cde.jsp pass parameter to servlet string:
String pdfFileName = "ddd/cdee.pdf";
Please explain in more detail. What do you want to accomplish ? Describe the navigational flow of your web application.
For example, the code that you posted does is the following.
In index.jsp the user uploads his chosen files to the Servlet mapped to "asdas".
The Servlet receives the files and put the files into server file system.
The Servlet creates a list of the uploaded files and sets that list into session scope.
The Servlet redirects the user to cde.jsp
cde.jsp reads the gets the filenames list from the session scope.
cde.jsp prints out the file names in its response to the user.
Is this what you want your web application to do ?
Do you want to change it ?
Ok, I see your latest post. You have a second Servlet. I guess you want this second servlet to serve the files.
So, I guess you want cde.jsp to display links to the uploaded files. That way the users can chose which file to open up. Is this what you want to do ?
For example, the code that you posted does is the following.
In index.jsp the user uploads his chosen files to the Servlet mapped to "asdas".
The Servlet receives the files and put the files into server file system.
The Servlet creates a list of the uploaded files and sets that list into session scope.
The Servlet redirects the user to cde.jsp
cde.jsp reads the gets the filenames list from the session scope.
cde.jsp prints out the file names in its response to the user.
Is this what you want your web application to do ?
Do you want to change it ?
Ok, I see your latest post. You have a second Servlet. I guess you want this second servlet to serve the files.
So, I guess you want cde.jsp to display links to the uploaded files. That way the users can chose which file to open up. Is this what you want to do ?
If I guessed right then use the following in cde.jsp
<%
java.util.List<String> filenames = (java.util.List<String>)request.getSession().getAttribute("item");
for(String filename: filenames) {
%>
<a href="mappingSecondServlet?name=<%=filename%>">Read <%=filename%></a <br/>
<%
}
%>
and in second Servlet use
String pdfFileName = request.getParameter("name");
error
<a href="mappingSecondServlet ?name=<%=f ilename%>" >Read <%=filename%></a <br/>
should have been
<a href="mappingSecondServlet ?name=<%=f ilename%>" >Read <%=filename%></a> <br/>
<a href="mappingSecondServlet
should have been
<a href="mappingSecondServlet
ASKER
<a href="gettt?name=<%=filena me%>">Read <%=filename%></a> <br/>
thats true my second servlet name is gettt. but still got error http status 404
thats true my second servlet name is gettt. but still got error http status 404
>but still got error http status 404
Look at the source code that cde.jsp sent to the browser. Is the link's url right ?
Look at the source code that cde.jsp sent to the browser. Is the link's url right ?
I assume that you mapped your second Servlet to "gettt" in the same way that you mapped your first to "asdas".
Is that right ?
Is that right ?
ASKER
yes
Is the link's url right ? Show what is sent to browser.
ASKER
String pdfFileName = request.getParameter("uplo ads/name") ;
//String pdfFileName = "uploads/billing_398141815 _4ecc8eb8d 22f5.pdf";
// String []filename_1 = request.getParameterValues (""
// + "filename_1");
System.out.println(pdfFile Name);
String contextPath = getServletContext().getRea lPath(File .separator );
File pdfFile = new File(contextPath + pdfFileName);
response.setContentType("a pplication /pdf");
response.addHeader("Conten t-Disposit ion", "attachment; filename=" + pdfFileName);
response.setContentLength( (int) pdfFile.length());
FileInputStream fileInputStream = new FileInputStream(pdfFile);
OutputStream responseOutputStream = response.getOutputStream() ;
int bytes;
while ((bytes = fileInputStream.read()) != -1) {
responseOutputStream.write (bytes);
}
the code got any problem. i got error:java.io.FileNotFound Exception: C:\apache-tomcat-6.0.33\we bapps\NewA pp\null (The system cannot find the file specified)
i upload that file under C:\apache-tomcat-6.0.33\we bapps\NewA pp\uploads
please help.
//String pdfFileName = "uploads/billing_398141815
// String []filename_1 = request.getParameterValues
// + "filename_1");
System.out.println(pdfFile
String contextPath = getServletContext().getRea
File pdfFile = new File(contextPath + pdfFileName);
response.setContentType("a
response.addHeader("Conten
response.setContentLength(
FileInputStream fileInputStream = new FileInputStream(pdfFile);
OutputStream responseOutputStream = response.getOutputStream()
int bytes;
while ((bytes = fileInputStream.read()) != -1) {
responseOutputStream.write
}
the code got any problem. i got error:java.io.FileNotFound
i upload that file under C:\apache-tomcat-6.0.33\we
please help.
What does
System.out.println(pdfFile Name);
print out ?
System.out.println(pdfFile
print out ?
ASKER
null
ASKER
sorry. can already
ASKER
but my second servlet to serve these two files and i wanna use i submit button only
ASKER
but my second servlet need to serve these two files not one file and i wanna use one submit button only. sorry
C:\apache-tomcat-6.0.33\we bapps\NewA pp\null
I guess it is null.
Show us the link that made the request.
I guess it is null.
Show us the link that made the request.
ASKER
still got error : C:\apache-tomcat-6.0.33\we bapps\NewA pp\hero.pd f
System.out.println(pdfFile Name); = hero.pdf
because my file was uploaded in C:\apache-tomcat-6.0.33\we bapps\NewA pp\uploads /hero.pdf
System.out.println(pdfFile
because my file was uploaded in C:\apache-tomcat-6.0.33\we
>but my second servlet need to serve these two files not one file and i wanna use one submit button only. sorry
You want to download two files at once ?
I don't think that is possible. You would have to zip them into one.
You want to download two files at once ?
I don't think that is possible. You would have to zip them into one.
ASKER
i dont want to download. i just want the parameter pass to servlet. thanks
>http://localhost:8080/NewApp/gettt?name=hero.pdf
Instead of using
String []filename_1 = request.getParameterValues (" + "filename_1");
use
String pdfFileName = request.getParameter("name ");
Instead of using
String []filename_1 = request.getParameterValues
use
String pdfFileName = request.getParameter("name
ASKER
String pdfFileName = request.getParameter("name ");
yes i am using the above code, but still error
System.out.println(pdfFile Name); = C:\apache-tomcat-6.0.33\we bapps\NewA pp\hero.pd f
my file was uploaded in C:\apache-tomcat-6.0.33\we bapps\NewA pp\uploads /hero.pdf
yes i am using the above code, but still error
System.out.println(pdfFile
my file was uploaded in C:\apache-tomcat-6.0.33\we
The code of your gettt Servlet will download a file to the browser.
Instead of using
>File pdfFile = new File(contextPath + pdfFileName);
in your gettt servlet use
File pdfFile = new File(contextPath + "uploads/" + pdfFileName);
or maybe try
File pdfFile = new File(contextPath + "/uploads/" + pdfFileName);
>File pdfFile = new File(contextPath + pdfFileName);
in your gettt servlet use
File pdfFile = new File(contextPath + "uploads/" + pdfFileName);
or maybe try
File pdfFile = new File(contextPath + "/uploads/" + pdfFileName);
ASKER
ya. that's only an example. actually i wanna pass the first .pdf file and second certificate file to generate a new pdf. so i need get the parameter of this two file pass to my servlet. please help.
By the way,
>String contextPath = getServletContext().getRea lPath(File .separator );
I would use something like
String fileDirPath = getServletContext().getRea lPath("/up loads");
>String contextPath = getServletContext().getRea
I would use something like
String fileDirPath = getServletContext().getRea
ASKER
File pdfFile = new File(contextPath + "/uploads/" + pdfFileName);
the above code was solved it. no problem.
but how do i use one button to submit and pass the two parameter(two files name) to servlet. please help me.
the above code was solved it. no problem.
but how do i use one button to submit and pass the two parameter(two files name) to servlet. please help me.
>so i need get the parameter of this two file pass to my servlet.
In cde.jsp instead of using
<a href="gettt?name=<%=filena me%>">Read <%=filename%></a> <br/>
You could use
<a href="gettt?namePdf=<%=fil ePdf%>&nam eCert=<%=n ameCert%>" >Go to gettt</a> <br/>
you have to first create those Strings.
In cde.jsp instead of using
<a href="gettt?name=<%=filena
You could use
<a href="gettt?namePdf=<%=fil
you have to first create those Strings.
ASKER
your means is one by one create a string?
> and i wanna use one submit button only.
Make a HTML form instead of the links.
Make a HTML form instead of the links.
>your means is one by one create a string?
Yes.
Yes.
ASKER
import org.apache.commons.fileupl oad.servle t.ServletF ileUpload;
import org.apache.commons.fileupl oad.FileIt emFactory;
import org.apache.commons.fileupl oad.FileUp loadExcept ion;
import org.apache.commons.fileupl oad.FileIt em;
import org.apache.commons.fileupl oad.disk.D iskFileIte mFactory;
import javax.servlet.http.HttpSer vlet;
import javax.servlet.http.HttpSer vletReques t;
import javax.servlet.http.HttpSer vletRespon se;
import javax.servlet.ServletExcep tion;
import java.io.IOException;
import java.io.File;
import java.io.PrintWriter;
import java.util.List;
import java.util.Iterator;
public class FileUploadDemoServlet extends HttpServlet {
private static final long serialVersionUID = -3208409086358916855L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("t ext/plain" );
boolean isMultipart = ServletFileUpload.isMultip artContent (request);
if (isMultipart) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory) ;
try {
List items = upload.parseRequest(reques t);
Iterator iterator = items.iterator();
int i=0;
while (iterator.hasNext()) {
FileItem item = (FileItem) iterator.next();
if (!item.isFormField()) {
String fileName = item.getName();
i=i+1;
String root = getServletContext().getRea lPath("/") ;
File path = new File(root + "/uploads");
if (!path.exists()) {
boolean status = path.mkdirs();
}
File uploadedFile = new File(path + "/" + fileName);
out.println(item.getName() );
request.getSession().setAt tribute("i tem"+i, item.getName());
response.sendRedirect("cde .jsp");
item.write(uploadedFile);
}
}
} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
FileUploadDemoServlet.java
-------------------------- ---------- ---------- -----
<%
String item1 = (String) request.getSession().getAt tribute("i tem1");
String item2 = (String) request.getSession().getAt tribute("i tem2");
%>
<input id="filename_1" type="text" name="filename_1" size="50" value='<%=item1%>'/><br/>
<input id="filename_2" type="text" name="filename_2" size="50" value='<%=item2%>'/><br/>
cde.jsp
-------------------------- ---------- ----------
the above code that's more easier for me. but i dont know why it only can upload one file. can you please help me to look at. thanks
import org.apache.commons.fileupl
import org.apache.commons.fileupl
import org.apache.commons.fileupl
import org.apache.commons.fileupl
import javax.servlet.http.HttpSer
import javax.servlet.http.HttpSer
import javax.servlet.http.HttpSer
import javax.servlet.ServletExcep
import java.io.IOException;
import java.io.File;
import java.io.PrintWriter;
import java.util.List;
import java.util.Iterator;
public class FileUploadDemoServlet extends HttpServlet {
private static final long serialVersionUID = -3208409086358916855L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("t
boolean isMultipart = ServletFileUpload.isMultip
if (isMultipart) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory)
try {
List items = upload.parseRequest(reques
Iterator iterator = items.iterator();
int i=0;
while (iterator.hasNext()) {
FileItem item = (FileItem) iterator.next();
if (!item.isFormField()) {
String fileName = item.getName();
i=i+1;
String root = getServletContext().getRea
File path = new File(root + "/uploads");
if (!path.exists()) {
boolean status = path.mkdirs();
}
File uploadedFile = new File(path + "/" + fileName);
out.println(item.getName()
request.getSession().setAt
response.sendRedirect("cde
item.write(uploadedFile);
}
}
} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
FileUploadDemoServlet.java
--------------------------
<%
String item1 = (String) request.getSession().getAt
String item2 = (String) request.getSession().getAt
%>
<input id="filename_1" type="text" name="filename_1" size="50" value='<%=item1%>'/><br/>
<input id="filename_2" type="text" name="filename_2" size="50" value='<%=item2%>'/><br/>
cde.jsp
--------------------------
the above code that's more easier for me. but i dont know why it only can upload one file. can you please help me to look at. thanks
ASKER
please help rrz@871311
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
No that is wrong
As sunu340 posted in your other question, take
> response.sendRedirect("cde .jsp");
out of loop and place it after it.
> response.sendRedirect("cde
out of loop and place it after it.
ASKER
?? not understand
Take the redirect out of the while loop. You can only redirect once.
I have to go. I will be back in 3-4 hours.
ASKER
k
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks rrz@871311 and sunu340
https://www.experts-exchange.com/questions/27486012/servlet-pass-data-to-jsp.html?cid=748&anchorAnswerId=37266265#a37266265
What trouble are you having with sunu340's solutution ?
Please explain in detail. What do you want to do with the file names in the servlet ?