Salve ho un semplice progetto web che dovrebbe pescare il nome delle immagini da un db la classe funziona perche mi restituisce la stringa che mi interessa , l'index va bene , il problema penso sia la servlet che dovrebbe restituire nella url l'immagine da utilizzare nel tag img che invece non restituisce niente ,perchè nella sorgente della pagina immagine.jsp il src manca della parte fiinale (relativa al nome dell'immagine) :
Classe Connesione:
index.jsp:codice:package classi; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.logging.Level; import java.util.logging.Logger; public class Connessione { private Connection connect; private String immagine; public Connection connettialdatabase(String server,String nomedb,String username,String password){ try { try { Class.forName("com.mysql.jdbc.Driver") ; // Imposta la connessione al server e al database } catch (ClassNotFoundException ex) { Logger.getLogger(Connessione.class.getName()).log(Level.SEVERE, null, ex); } // prova="jdbc:mysql://"+server+"/"+nomedb+",\""+username+"\",\""+password+"\""; connect = DriverManager.getConnection("jdbc:mysql://"+server+"/"+nomedb,username,password); } catch (SQLException ex) { Logger.getLogger(Connessione.class.getName()).log(Level.SEVERE, null, ex); } return connect; } public String restituisciImmagine(int numero) throws SQLException{ Connection con =connettialdatabase("localhost","Imagine","root","root"); Statement stmt=con.createStatement(); String sql="Select * from Immagini where id ='"+numero+"'"; ResultSet rs=stmt.executeQuery(sql); rs.first(); int numeroRighe=rs.getRow(); if(numeroRighe==1){ immagine=rs.getString("Immagine"); return immagine; } else { return "Immagine non prestente"; } } public String getImmagine() { return immagine; } public void setImmagine(String immagine) { this.immagine = immagine; } public static void main(String args[]) throws SQLException{ Connessione con = new Connessione(); con.connettialdatabase("localhost", "Imagine", "root", "root"); //System.out.println(con); System.out.println(con.restituisciImmagine(1)); } }
ImmaginiServlet:codice:<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Immagini</title> </head> <body> <form name="immagini" action="ImmaginiServlet" method="post"> Inserisci il numero dell'immagine che vuoi visualizzare<input type="text" name="immagine"> <input type="submit" value="invia"> </form> </body> </html>
immagine.jsp :codice:package Immagini; import java.io.IOException; import java.sql.SQLException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import classi.Connessione; /** * Servlet implementation class ImmaginiServlet */ @WebServlet("/ImmaginiServlet") public class ImmaginiServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ImmaginiServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int img=Integer.parseInt(request.getParameter("immagine")); Connessione con=new Connessione(); try { con.setImmagine(con.restituisciImmagine(img)); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } RequestDispatcher dispatcher =getServletContext().getRequestDispatcher("/immagine.jsp?immagineRestituita="+con.getImmagine()); dispatcher.forward(request, response); } }
codice:<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Ecco la tua immagine</title> </head> <body> <img src ="/home/Stefano/Immagini/<% request.getParameter("immagineRestituita") ; %>.jpg"> </body> </html>

Rispondi quotando