aspose file tools*
The moose likes JSP and the fly likes How to give Servlet response from Tag Handler class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "How to give Servlet response from Tag Handler class?" Watch "How to give Servlet response from Tag Handler class?" New topic
Author

How to give Servlet response from Tag Handler class?

sagar gangipamula
Greenhorn

Joined: Dec 19, 2008
Posts: 3
How to give servlet response form the dotag() method in which i am retrieving image from database and i have to display it on jsp page where i am calling this tag.

I am not geting the servlet response there .

Can any one help how to write that code.

//Code goes like this..

package com.bean;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ResourceBundle;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class DisplayTag extends SimpleTagSupport
{



public void doTag() throws JspException, IOException {


JspWriter out = getJspContext().getOut();
try {
ResourceBundle rb = ResourceBundle.getBundle("com.bean.messages");
String username = rb.getString("username");
String password = rb.getString("password");
String url = rb.getString("url");
String driver = rb.getString("driver");

Class.forName(driver).newInstance();
Connection con = (Connection) DriverManager.getConnection(url, username,password);
Statement st = con.createStatement();
ResultSet rs=null;

rs=st.executeQuery("select img from image ORDER BY RAND() LIMIT 1");

String imglen="";

while(rs.next())

{
imglen = rs.getString(1);
System.out.println(imglen.length());

int len=imglen.length();
byte [] b=new byte[len];

InputStream in=rs.getBinaryStream(1);

int i= in.read(b,0, len);

System.out.println("bytes"+b.length);
System.out.println("index"+i);
out.write("Hai this is sagar");





// here hw we can write......





}

}
catch(Exception e)
{
e.printStackTrace();
}
}


}


SAGAR GV
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 823

Hello and welcome to JavaRanch.
To help you I would say use the UBB code to post your source code.You can edit your code and make it more readable and for more responses to flow in for your question.

Thank you.


Regards, Sud.
SCJP 5 ScjpFAQ JLS
Ulf Dittmer
Rancher

Joined: Mar 22, 2005
Posts: 42952
    
  73
You can't. JSPs (and thus also JSP tags) are for creating text, not for streaming binary data. You will need to use a servlet to do this.
sagar gangipamula
Greenhorn

Joined: Dec 19, 2008
Posts: 3
Originally posted by Ulf Dittmer:
You can't. JSPs (and thus also JSP tags) are for creating text, not for streaming binary data. You will need to use a servlet to do this.


thank Q ,
i put that code in servlet then can i call that servlet in dotag() using the attribute src.
Ulf Dittmer
Rancher

Joined: Mar 22, 2005
Posts: 42952
    
  73
It sounds like you are trying to somehow embed an image in a web page - that is not going to work. You need to write an <img> tag into the web page, and the src attribute of that img tag then points to a servlet that serves the raw image data.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to give Servlet response from Tag Handler class?