Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to give Servlet response from Tag Handler class?

 
sagar gangipamula
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
}


}
 
sudipto shekhar
Ranch Hand
Posts: 823
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic