0

I have retrieved url to an image from database.Now,i want to use this url to display an image on the JSP page.

I am retrieving the value of url using this code--

<% 
try{
        Class.forName("com.mysql.jdbc.Driver");
        try(Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/asad","root","1234");){
            //database connection

            System.out.println("driver registered");
                        Statement s=conn.createStatement();  
        String searchby=request.getParameter("searchby");
        String param=request.getParameter("name");
        String q="";
        System.out.println("before pstmt");
           q="select * from asad.book where genre"+"=?";
         PreparedStatement pstmt=(PreparedStatement)conn.prepareStatement(q);
            pstmt.setString(1,param);
            System.out.println("after pstmt");
        System.out.println("param is "+param+"  searchby is "+searchby);
        ResultSet rs =(ResultSet) pstmt.executeQuery();
//      ResultSetMetaData rsmd=(ResultSetMetaData) rs.getMetaData();
        while(rs.next())
        {
            System.out.println(rs.getString(1));

            String url=rs.getString(3);
            %>

To do this,i wrote --

<img src="<% url %>" style=width:350px;height:350px>

Eclipse is showing error in the above img tag statement.Can someone help me with this ?

  • Possible duplicate of [How to retrieve and display images from a database in a JSP page?](http://stackoverflow.com/questions/2340406/how-to-retrieve-and-display-images-from-a-database-in-a-jsp-page) – Taha Jun 14 '16 at 11:41

1 Answers1

0

<% ...%> is for code in a jsp, while here you want a value so you should write <img src="<%= url %>" style=width:350px;height:350px>,

But anyway, it is now considered as bad practice to have so much java code in a scriptlet. If you want to use best practices, you should move it into a servlet (easier to write and test in an IDE like Eclipse), store relevant values in request attributes and only do the display part in JSP. If you did it, and stored the url in "url" request attribute, the line would become

<img src="${url}" style=width:350px;height:350px>
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252