aaronyeo22
asked on
servlet pass data to jsp
when i try to upload two file and use
out.println(item.getName() ); it can display the two file name like : abc.gif and cde.gif
how do i display this two file name in jsp with two text input.
thanks
<fieldset>
<legend>Upload File</legend>
<form action="uploadservlet" 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.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();
while (iterator.hasNext()) {
FileItem item = (FileItem) iterator.next();
if (!item.isFormField()) {
String fileName = item.getName();
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", item);
response.sendRedirect("cde .jsp");
item.write(uploadedFile);
}
}
} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
FileUploadDemoServlet.java
out.println(item.getName()
how do i display this two file name in jsp with two text input.
thanks
<fieldset>
<legend>Upload File</legend>
<form action="uploadservlet" 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.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();
while (iterator.hasNext()) {
FileItem item = (FileItem) iterator.next();
if (!item.isFormField()) {
String fileName = item.getName();
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
sorry you should initialize i as 0;
i=0;
i=0;
String item1 = (String) request.getSession().getAt tribute("i tem1");
String item2 = (String) request.getSession().getAt tribute("i tem2");
String item2 = (String) request.getSession().getAt
>>String item1 = (String) request.getSession().getAt tribute("i tem1");
How will you know how many items there are?
How will you know how many items there are?
as he is expecting two .gif files.so we can hardcoded with item1 amd item2 etc..;but that is not the generic way.
he is having only textboxes so we have hardcodes.if he is having multiple text boxes then it is better to follow another approach.
2 textboxes
>>he is having only textboxes so we have hardcodes.
That's true. Probably better to do it generically though imo
That's true. Probably better to do it generically though imo
ASKER
thanks
First of all the above code will not execute for both the files as response.sendRedirect("cde .jsp"); is used inside the while loop,so the code will redirect before executing for both files.
To send the file names to jsp,you can simply add the file names in a list and set the list in session as it is done :
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();
List<String> filenames = new ArrayList<String>();
while (iterator.hasNext()) {
FileItem item = (FileItem) iterator.next();
if (!item.isFormField()) {
String fileName = item.getName();
String root = getServletContext().getRea lPath("/") ;
File path = new File(root + "/uploads");
if (!path.exists()) {
boolean status = path.mkdirs();
}
File uploadedFile = new File(path + "/" + fileName);
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();
}
}
**********JSP*********
<%
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 />
<%
}
%>
Hope this helps :)
To send the file names to jsp,you can simply add the file names in a list and set the list in session as it is done :
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();
List<String> filenames = new ArrayList<String>();
while (iterator.hasNext()) {
FileItem item = (FileItem) iterator.next();
if (!item.isFormField()) {
String fileName = item.getName();
String root = getServletContext().getRea
File path = new File(root + "/uploads");
if (!path.exists()) {
boolean status = path.mkdirs();
}
File uploadedFile = new File(path + "/" + fileName);
filenames.add(fileName);
item.write(uploadedFile);
}
}
request.getSession().setAt
response.sendRedirect("cde
} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
**********JSP*********
<%
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
<%
}
%>
Hope this helps :)
yes you are correct.there are so many approaches to send the list to jsp.this is one of them.even putting filenames to session you can print the filenames using request scope.
ASKER
sunu340 can help me
ASKER
really sorry . chaituu only can upload one file. but yours one is two files. but after i submit that becomes 4 text input. i just want two. thanks
ASKER
fix already. thanks sunu340
ASKER
chaituu: your solution only able to upload one file. sorry
>>chaituu: your solution only able to upload one file. sorry
Worries about the number of files it allows just show, as i mentioned earlier, that the solution should be generic - so it doesn't matter how many there are.
Worries about the number of files it allows just show, as i mentioned earlier, that the solution should be generic - so it doesn't matter how many there are.
i think you raised another question here.
https://www.experts-exchange.com/questions/27487493/jsp-to-servlet.html?cid=1576
https://www.experts-exchange.com/questions/27487493/jsp-to-servlet.html?cid=1576
Just pass the jsp a parameter called 'filenames' say. Concatenate the strings produced by the code i quoted into a comma-separated list