0

This is my servlet named showimage.java

 protected void doGet(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {

        session=sessionFactory.openSession();
        session.beginTransaction();         

        try{            
        Tab person = (Tab) session.get(Tab.class,new Integer(1));     
        byte[] photoBytes = person.getEImage();        
        try (ServletOutputStream sos = res.getOutputStream()) {
             sos.write(photoBytes);
            }
        }
        catch(Exception e){
        e.printStackTrace();        
        session.getTransaction().commit();
        session.close();
        a++;
        }        
}

And below is my jsp page named showimage.jsp.I'm getting same images from database in image colomn due to this Tab person = (Tab) session.get(Tab.class,new Integer(1));. Can someone suggest to replace new Integer(1) so that every time the servlet is called , i am able to get new Id from database dynamically.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP List Users Records</title>
</head>
<body>
    <sql:setDataSource
        var="myDS"
        driver="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/project"
        user="root" password="lovemapa!23"
    />

    <sql:query var="listUsers"   dataSource="${myDS}">
        SELECT * FROM tab;
    </sql:query>

    <div align="left">
        <table border="1" cellpadding="5">
            <caption><h2>List of users</h2></caption>
            <tr>
                <th>ID</th>
                 <th>Image</th>
            </tr>
            <c:forEach var="user" items="${listUsers.rows}">
                <tr>
                    <td><c:out value="${user.E_id}" /></td>
                   <td><img src="showimage?id=${user.E_id}"></td>
                </tr>
            </c:forEach>
        </table>
    </div>
</body>
</html>

1 Answer 1

1

You pass an id as URL parameter: showimage?id=${user.E_id}. In your servlet you can retrieve this value as

Integer id = new Integer(req.getParameter("id"));
Tab person = (Tab) session.get(Tab.class, id);
Sign up to request clarification or add additional context in comments.

2 Comments

one more doubt Sir, this "id" in showimage?id=${user.E_id} ... is this pre written or we have to define it? and how do I pass id in URL parameter ,Sir
Thank You very much Sir, That was really Useful . Thanks a ton :) God bless u :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.