You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Matteo Di Palma <di...@gmail.com> on 2008/01/30 13:24:44 UTC

[FileUpload] Passage of values from a servlet to a jsp

Hi,

I have a file upload Servlet that insert image into DB and I would see a
link to these images in a JSP, or at least a list.
The insert work but the list does not, because I do not know how to send
these parameters from a Servlet to a JSP.
Into HTML form I enter only the picture and not even his name.
Here is an extract from the Servlet:
***********************************************************************
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List /* FileItem */ items = upload.parseRequest(request);

Iterator iter = items.iterator();
   while (iter.hasNext()) {
      FileItem item = (FileItem) iter.next();
      name = item.getName();
      img  = item.getInputStream();
      size = item.getSize();
   }

Image image = new Image(name, img, size);  // Creo un oggetto Image che è un
JavaBean
ImageCRUD ic = new ImageCRUD();             // Classe CRUD per le operazioni
nel DB
ic.addImage( image );                                 // Inserisco
l'immagine nel DB
ResultSet rs = ic.listImage();                      // Seleziono le immagini

RequestDispatcher dispatcher;
dispatcher = getServletContext().getRequestDispatcher("/ShowImage.jsp");
dispatcher.forward(request,response);
***********************************************************************

Now in JSP would like a list of images (in another words the variable name
of the Servlet). Something like:
***********************************************************************
<% String name = request.getParameter ( "name");%>

/ / On a cycle for the recurrence of cells of the table
<tr>
     <td Align="right"> Name: </ td> <td> <=% name%> </ td>
</ Tr>
***********************************************************************

Of course, this code does not work and is probably nonsense, but I put to
the idea.
I am sorry for this approximation.

Thank you for your attention,
Matteo

Re: [FileUpload] Passage of values from a servlet to a jsp

Posted by Matteo Di Palma <di...@gmail.com>.
Briefly my classes:

1) MyFileUpload/WEB-INF/classes/data/ImageCRUD

---------------------------------------------------------------------------------------------------------
public class ImageCRUD{
   ...
   public ResultSet listImage(){
   try{
     Class.forName(...);
     if(conn == null){
     conn = DriverManager.getConnection(...);
     ps = conn.prepareStatement("select idImage, Name from images");
     rs = ps.executeQuery();
     while(rs.next()){
        int numColumn = rs.getMetaData().getColumnCount();
        for(int i=1; i <= numColumn; i++){
           System.out.println(rs.getObject(i));
        }
     }
    ...
    return rs;
}
-------------------------------------------------------------------------------------------------------------------

2) MyFileUpload/WEB-INF/classes/ImageServlet.java
--------------------------------------------------------------------------------------------------------------------
public class ImageServlet extends HttpServlet{
   ...
   try{
      // The code for add an image to DB. Work well

      ic.addImage( image );   // ic = new ImageCRUD Object
      ResultSet rs = ic.listImage();

      RequestDispatcher dispatcher;
      dispatcher = getServletContext().getRequestDispatcher("/ShowImage.jsp");
      request.setAttribute("rs", rs);
      dispatcher.forward(request,response);
    }catch(...){
      ...
    }
...
---------------------------------------------------------------------------------------------------------------------

3) MyFileUpload/ShowImage.jsp
----------------------------------------------------------------------------------------------------------------------
...
<%
rs = request.getAttribute("rs");
while(rs.next()){
  int numColumn = rs.getMetaData().getColumnCount();
  for(int i=1; i <= numColumn; i++){
     out.println(rs.getObject(i));
  }
}
%>
...
-------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [FileUpload] Passage of values from a servlet to a jsp

Posted by Michael Rasmussen <ra...@gmail.com>.
How does your servlet forward to your JSP?

On Jan 30, 2008 10:33 AM, Matteo Di Palma <di...@gmail.com> wrote:

> 1) In the class ImageCRUD.java I have the following code:
> _____________________________________________________________________
> | public ResultSet listImage(){
> | ...
> | ps = conn.prepareStatement("select idImage, Name from images");
> | return ps.executeQuery();
> | ...
> |_}___________________________________________________________________
>
> 2) In the Servlet ImageServlet.java I have the following code:
> _____________________________________________________________________
> | ResultSet rs = ic.listImage();     // ic = new ImageCRUD Object
> | ...
> | request.setAttribute("rs", rs);
> |_____________________________________________________________________
>
> 3) In the JSP ShowImage.jsp I have the following code.
> ______________________________________________________________________
> | <%
> | rs = request.getAttribute("rs");
> | while(rs.next()){
> |    int numColumn = rs.getMetaData().getColumnCount();
> |    for(int i=1; i <= numColumn; i++){
> |    out.println(rs.getObject(i));
> | }
> |}%>
> |______________________________________________________________________
>
>
> I think that rs is null and also I see a blank page.
> Where mistake?
>
> Thanks in advance.
> |
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>

Re: [FileUpload] Passage of values from a servlet to a jsp

Posted by Matteo Di Palma <di...@gmail.com>.
1) In the class ImageCRUD.java I have the following code:
_____________________________________________________________________
| public ResultSet listImage(){
| ...
| ps = conn.prepareStatement("select idImage, Name from images");
| return ps.executeQuery();
| ...
|_}___________________________________________________________________

2) In the Servlet ImageServlet.java I have the following code:
_____________________________________________________________________
| ResultSet rs = ic.listImage();     // ic = new ImageCRUD Object
| ...
| request.setAttribute("rs", rs);
|_____________________________________________________________________

3) In the JSP ShowImage.jsp I have the following code.
______________________________________________________________________
| <%
| rs = request.getAttribute("rs");
| while(rs.next()){
|    int numColumn = rs.getMetaData().getColumnCount();
|    for(int i=1; i <= numColumn; i++){
|    out.println(rs.getObject(i));
| }
|}%>
|______________________________________________________________________

	
I think that rs is null and also I see a blank page.
Where mistake?

Thanks in advance.
|

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [FileUpload] Passage of values from a servlet to a jsp

Posted by Matteo Di Palma <di...@gmail.com>.
Work! :)	
You are right, I made a mistake in the copy / paste.
Now I must understand how to show not only the last image inserted, but all
	
You have any advice? Unfortunately is the pattern MVC that confuses me :(

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [FileUpload] Passage of values from a servlet to a jsp

Posted by Tahir Akhtar <ta...@spectrum-tech.com>.
Matteo Di Palma wrote:
> In the first place thanks for reply.
> I think that with your council I'm approaching the answer to a
> problem, but unfortunately does not work.
> In the end I see only a blank page : (
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>
>   
Have you tried printing the value of the attribute through a logger or 
System.out to confirm whether jsp received the attribute or not.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [FileUpload] Passage of values from a servlet to a jsp

Posted by Matteo Di Palma <di...@gmail.com>.
In the first place thanks for reply.
I think that with your council I'm approaching the answer to a
problem, but unfortunately does not work.
In the end I see only a blank page : (

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [FileUpload] Passage of values from a servlet to a jsp

Posted by Tahir Akhtar <ta...@spectrum-tech.com>.
Matteo Di Palma wrote:
> Hi,
>
> I have a file upload Servlet that insert image into DB and I would see a
> link to these images in a JSP, or at least a list.
> The insert work but the list does not, because I do not know how to send
> these parameters from a Servlet to a JSP.
> Into HTML form I enter only the picture and not even his name.
> Here is an extract from the Servlet:
> ***********************************************************************
> boolean isMultipart = ServletFileUpload.isMultipartContent(request);
> FileItemFactory factory = new DiskFileItemFactory();
> ServletFileUpload upload = new ServletFileUpload(factory);
> List /* FileItem */ items = upload.parseRequest(request);
>
> Iterator iter = items.iterator();
>    while (iter.hasNext()) {
>       FileItem item = (FileItem) iter.next();
>       name = item.getName();
>       img  = item.getInputStream();
>       size = item.getSize();
>    }
>
> Image image = new Image(name, img, size);  // Creo un oggetto Image che è un
> JavaBean
> ImageCRUD ic = new ImageCRUD();             // Classe CRUD per le operazioni
> nel DB
> ic.addImage( image );                                 // Inserisco
> l'immagine nel DB
> ResultSet rs = ic.listImage();                      // Seleziono le immagini
>
> RequestDispatcher dispatcher;
> dispatcher = getServletContext().getRequestDispatcher("/ShowImage.jsp");
> dispatcher.forward(request,response);
> ***********************************************************************
>
> Now in JSP would like a list of images (in another words the variable name
> of the Servlet). Something like:
> ***********************************************************************
> <% String name = request.getParameter ( "name");%>
>
> / / On a cycle for the recurrence of cells of the table
> <tr>
>      <td Align="right"> Name: </ td> <td> <=% name%> </ td>
> </ Tr>
> ***********************************************************************
>
> Of course, this code does not work and is probably nonsense, but I put to
> the idea.
> I am sorry for this approximation.
>
> Thank you for your attention,
> Matteo
>
>   
Perhaps this will help.
Before dispatching:
request.setAttribute("fileName", name);

In jsp:
name= request.getAttribute("fileName");

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org