Opa…
tenho o seguinte servlet:
[code]package fso;
import java.io.;
import java.sql.;
import javax.servlet.;
import javax.servlet.http.;
import java.sql.ResultSet;
/**
*
- Author: Glauber
- Scope: Servlet de geração de imagem
- Version: 1.0
*/
public class GeraImagem extends HttpServlet {
private Connection con;
private Statement stmt;
private String trace;
/**
* Setup database connection and create SQL statement
*/
public void init( ServletConfig config ) throws ServletException
{
// Try database connection
try{
// The call to Class.forName explicitly loads the driver class
//Class.forName("org.gjt.mm.mysql.Driver");
Class.forName("org.postgresql.Driver").newInstance();
//jdbc:mysql://<HOST>:<PORT>/<DB>
//Connection con = DriverManager.getConnection("jdbc:mysql://localhost/mydb?user=''&password=''");
con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/sistemas","postgres","senhabanco");
// Once a connection has been established we can create an instance
// of Statement, through which we will send queries to the database.
stmt = con.createStatement();
}
catch (Exception exception ) {
exception.printStackTrace();
throw new UnavailableException(exception.getMessage());
}
} // End init()
/**
* Forward to doGet
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doPost(request, response);
}
/*
* NOTE: doPost -> HTTP post requests | doGet -> HTTP get resuest
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
// Simple trace variable for debugging...
trace = "";
try {
String SQL = "select * from figuras where id=";
ResultSet rs = stmt.executeQuery(SQL);
rs.next();
int intId = rs.getInt("id");
int intCt = 0;
//Blob blob = null;
//blob = rs.getBlob("imagem");
response.setContentType("image/jpeg");
//InputStream in = blob.getBinaryStream();
InputStream in = rs.getBinaryStream("im_figura");
String strTeste= rs.getString("im_figura");
OutputStream out = response.getOutputStream();
int b;
while ((b = in.read()) != -1) {
out.write(b);
intCt++;
}
//salvar("c:/teste.txt",strTeste, false);
in.close();
out.flush();
out.close();
}
catch (Exception exception ) {
System.out.println("Erro---> " + exception);
throw new ServletException("Error");
}
} // end doPost()
public static void salvar(String arquivo, String conteudo, boolean adicionar) throws IOException {
FileWriter fw = new FileWriter(arquivo, adicionar);
fw.write(conteudo);
fw.close();
}
/*
* Close SQL statements & database connection
*/
public void destroy()
{
// Attempt to close statements and database connection
try{
stmt.close();
con.close();
}
// Handle database exceptions by returning error to client
catch (SQLException sqlException){
sqlException.printStackTrace();
}
} // End destroy()
} // End class showImage
[/code]
Daí preciso que este servlet receba um valor através da URL, e o coloque do lado da query:
String SQL = "select * from figuras where id="+valor recebido;
Pra gerar uma imagem de acordo com o ID.
Sugestões?!
Tentei assim:
String strId = String.valueOf(getExternalContext().getRequestParameterMap().get("figId"));
Mas não deu, porque, obviamente, não achou o método e o import não funcionou…
Enfim… aceito dicas!
valeu!