Page 2 of 2 FirstFirst 12
Results 21 to 26 of 26

Thread: creating a file

  1. #21
    aruna.hcu is offline Member
    Join Date
    Dec 2009
    Posts
    24
    Rep Power
    0

    Default

    Is there a way to specify the image to be retrieved from jsp in
    Java Code:
    while(rst.next()){
      <img src="<s:url action="myAction"/>" style="margin-right:20px;" />
      }
    and also i wrote 2 classes as mentioned above MyAction.java and MyBytesResult.java, can I combine them to one and return an image from that so that it can be used to display the image. I think i have just got the essence but not the perfect procedure of doing that. In the img src="", I just want it to go and pick up the image, I am not sure how to do that.

  2. #22
    aruna.hcu is offline Member
    Join Date
    Dec 2009
    Posts
    24
    Rep Power
    0

    Default

    I figured that the imageAction should return a null so that it is not forwarded to a jsp..but do not have idea how to implement it..

  3. #23
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Do as you did in the other class (MyBytesResult) and get the ServletActionContext to give you the HTTPResponse. From there you get the output stream, and feed the input stream from the database call to it.

  4. #24
    aruna.hcu is offline Member
    Join Date
    Dec 2009
    Posts
    24
    Rep Power
    0

    Default

    I got it working with struts 2 and tiles. the code is as below:

    Java Code:
              <%
                    while(rst.next()){
    	                 img=rst.getBlob(11);
    	                 imgData = img.getBytes(1,(int)img.length());
    	    	         url=rst.getString("url");	
    	  	 
               %>
    		      
    		      
    		      <img src="<s:url action="myAction">[B]<s:param name="id" value="%{imgData}" />[/B]</s:url>" style="margin-right:20px;"  /><img src="<s:url value='images/books/bowling_robert_small.jpg'/>" />  <br />Title: <%out.println(rst.getString("title"));%> <br/>Authors: <%out.println(rst.getString("author"));%>
    	   	 <%} %>
    part of my struts.xml

    Java Code:
    <action name="myAction" class="com.icensa.action.MyAction">             
                   <result type="tiles">books</result>
           </action>
    MyAction.java

    Java Code:
    import java.io.OutputStream;
    import java.sql.Blob;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    
    import javax.servlet.http.HttpServletResponse;
    import org.apache.struts2.ServletActionContext;
    import com.opensymphony.xwork2.ActionSupport;
    
    public class MyAction extends ActionSupport {
    	
        private static final long serialVersionUID = 1L;
        private byte[] img=null;
        Blob image = null;
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        byte[] imgData = null;
        OutputStream o = null;
    
        HttpServletResponse response = ServletActionContext.getResponse();
       
        public String execute() {
    		
    	try {
    		 Class.forName("com.mysql.jdbc.Driver");
    		 con = DriverManager.getConnection("jdbc:mysql://localhost:3306/project","root","test");
    		  stmt = con.createStatement();
    		  rs = stmt.executeQuery("select * from books_tb where category='Social and Economic' order by publish_date desc");
    		      
                     while (rs.next()) {
    	                image = rs.getBlob(11);
    	                imgData = image.getBytes(1,(int)image.length());		         
    	         } 
    
            	response.setContentType("image/jpeg");				
    
    		OutputStream out = response.getOutputStream();
    		out.write(img);
    		out.close();
    		return null;
             }
    	catch (Exception e) {
    		          System.out.println(e.getMessage());	         
    	    } 
    	  return null;
    	}
    As you could see, always the first image is returned. Is there a way to pass the image blob from jsp using <s:url> tag to Action class. I have tried to add
    <s: param> and send the image, but do not know how to retrieve it in Action class.
    Last edited by aruna.hcu; 01-05-2010 at 11:43 PM.

  5. #25
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    OK. You're pretty close to what I was expecting.

    First, and this is a coding standards thing, don't have code in your JSPs. I'm assuming that rst is a resultset from some earlier query. Rather than having this, it ought to be in whatever Action was called to create this part of the page (at least as a start point), and provide a List of things so you can use a simple tag to iterate over it. The list, in this case, should simply be a list of whatever data is required to identify an individual image. This data will form your parameters for the URL used in the <img> tag.

    Finally, all the MyAction (you should really rename that to something meaningful) action should do is take those parameters and query the database to get a single image, which it then streams back.

    So, for the jsp bit you'd have something like this (note the tag name is undoubtedly wrong, but there is one in the standard taglib, and this should be treated more like pseudo-code):

    Java Code:
    <c:list>
    <img src="<s:url action="myAction"><s:param name="id" value="%{id}" /></s:url>" style="margin-right:20px;"  />
    </c:list>
    The MyAction action code will simply "select img_data from some_table where id = ?", and stream that img_data back.

  6. #26
    aruna.hcu is offline Member
    Join Date
    Dec 2009
    Posts
    24
    Rep Power
    0

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Creating JAR file goes wrong.
    By dudejonne in forum New To Java
    Replies: 6
    Last Post: 11-02-2009, 05:32 PM
  2. Creating a .jar file
    By Wataru in forum New To Java
    Replies: 3
    Last Post: 07-22-2009, 07:02 AM
  3. Creating jar file
    By Heather in forum Advanced Java
    Replies: 4
    Last Post: 02-11-2009, 10:58 AM
  4. creating jar file
    By po0oker in forum Advanced Java
    Replies: 3
    Last Post: 04-04-2008, 08:49 PM
  5. creating .ear file
    By psn in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 01-06-2008, 06:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •