Retrieve image from oracle database and show image in jsp - oracle

I want to retrieve image from table named Image and column named image with type blob, but while running the code its giving the blob code like this-
"oracle.sql.BLOB#193242d"
Please see my code for fetching and show the image on jsp page/browser:-
ResultSet resultset =
statement.executeQuery("select * from image where id = '" + id + "'") ;
Browser page:-
<TABLE border="1">
<TR>
<TH>ID</TH>
<TH>picture</TH>
</TR>
<TR>
<TD> <%= resultset.getString(1) %> </TD>
<TD> <%= resultset.getBlob(2) %> </TD>
</TR>
</TABLE>
<BR>

First:
oracle.sql.BLOB#193242d is the response of toString() method, not the image.
Second:
ResultSet resultset =
statement.executeQuery("select * from image where id = '" + id + "'") ;
For sake of SQL Injection, please!
Finally
You cannot response image with text data in heart! Image must be retrieved via a separated cgi/servlet where the content type is set to image, so for your case.
<TD> <%= resultset.getString(1) %> </TD>
<TD> <img src="/get_img?id="<%=id%>/> </TD>
And later you need a servlet/cgi to respond the image as get_img
#WebServlet(name = "img", urlPatterns = {"/get_img"})
public class get_img extends HttpServlet{
doGet(HttpServletRequest req, HttpServletResponse resp){
...
resp.setContentType("image/jpg");//or image/whatever
try(ResultSet rs=ps.executeQuery()){//where ps is a PreparedStatement
//set the content length, and respond image data
//example: http://stackoverflow.com/a/4332975/5266804
}
...
}
}

You can either create a servlet to serve the images from DB, see How to retrieve images from database and place on JSP?
Or you can embed the image data directly into the src attribute of an image tag: I want to retrieve image from mysql database using servlet and show that image along with that user details in table - although this is recommendable only for small images (icons etc.).

Related

How to access the table,tr,td which has no attributes using Watir

I am trying to access the table as well it rows and table data(td),generally the table,tr and td has no attributes to use I tried with index but still no use,even I tried with the parent element of table still I was unable to access it.
<div class = "Links"></div>
<div class = "board">
<table>
<tbody>
<tr><td>Name</td><td>Value</td></tr>
<tr><td>shaik</td><td>500</td></tr>
<tr><td>shahrukh</td>900<td><span style="" has css properties ></span></td></tr>
</tbody>
</table>
</div>
I tried in the following way
#ie.table(index,0) and
#ie.div(:class,'Links').table(:index,0)
returned an error as unable to locate the element
#ie.table by itself should work.
#ie.div(:class, 'Links') is not a parent of the table element since the </div> comes before the <table> in the dom.
Try this,
myelem = browser.tr(:text, /Name/).td(:index => 2).text
#or you can use the more user-friendly aliases for those tags
myelem = browser.row(:text, /Name/).cell(:index => 2).text

Identifying an WebTable/WebGrid Object in QTP

I have the below code in html for a WebTable(Web Grid).
<table width="90%">
<div class="greybox" style="margin-top:2%;">
<table class="datagrid" width ="100%">....<table>
</div>
</table>
I tried providing exactly the same(all) properties in my descriptive programming but the Web Element(DIV) is not being identified by QTP. Is there a unique way to identify this?
Note: The Web page is developed a single page application
Edit:
So I think I have resolved the issue with the below code. There were two Objects being identified without the "Unique Text" if clause. First Object was parent of the DIV object so had to use a "Unique text" from the first object which wouldn't be part of any other object. I am currently trying with different data to see if it's working fine
Browsername = Browser("micClass:=Browser").GetROProperty("name")
Pagename = Browser("micClass:=Browser").Page("micClass:=Page").GetROProperty("name")
Set desc = Description.Create()
desc("micclass").Value = "Webelement"
Set ChildObject=Browser("name:="&BrowserName).Page("name:="&PageName).ChildObjects(desc)
Set Child_Table_Value = nothing
For i=0 to ChildObject.Count-1
innerhtmlvalue = ChildObject(i).GetRoproperty("innerhtml")
htmltag = ChildObject(i).GetRoproperty("micclass")
if(Instr(innerhtmlvalue, "MARGIN-TOP: 2%")<>0) then
if(Instr(innerhtmlvalue, "UniqueText")=0) then
if(Instr(htmltag, "WebElement")<>0) then
Set Child_Table_Value = ChildObject(i)
End If
End If
End IF
Next
Set Table_Value = Child_Table_Value.WebTable("html tag:=Table")
Ok, so assuming you have an HTML structure something like this:
<table width="90%">
<tr>
<td>
<div class="greybox" style="margin-top:2%;">
<table class="datagrid" width="100%">
<tr>
<td>UniqueText</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
...and as per your current solution you can rely on "UniqueText" being present, then you could try the following XPath statement:
(//table[contains(., 'UniqueText')])[last()]
So in QTP/UFT you'd be doing:
Browser("micClass:=Browser").Page("micClass:=Page").WebTable("xpath:=(//table[contains(., 'UniqueText')])[last()]")
Try to use like below.(Try to identify the div block using the "class")
Browser("micClass:=Browser").Page("micClass:=Page").Webelement("class:=greybox").Webtable("class:=datagrid")
Please let me know

com.google.appengine.api.datastore.Text Data type unable to read in JSP Page

I am using hibernate + spring MVC. In My project there is one column in table such as :
#Column(name = "CLM_DSCRIPTION")
private Text desc;
And getter & setter Method for this
public Text getDesc() {
return desc;
}
public void setDesc(Text desc) {
this.desc = desc;
}
now i used this table in jsp where i want to show this column data into table.
M doing as below
<c:forEach items="${tbleObjs}" var="tbl" varStatus="status">
<tr>
<td>${status.index+1}</td>
<td class="labels2">${tbl.desc}</td>
</tr>
</c:forEach>
but it is not geting ${tbl.desc} here.
if i did desc.getValue() then it will work but i don't know, which is the best way to achieve this.
Please suggest me a better way.
i have achieve with following code :
<c:forEach items="${entityObjs}" var="tbl" varStatus="status">
<tr>
<%
Entity entityObj = (Entity)pageContext.getAttribute("tbl");
String disc = project.getDesc().getValue();
pageContext.setAttribute("disc",disc);
%>
<td>${status.index+1}</td>
<td class="labels2">${disc}</td>
</tr>
</c:forEach>

Image in jsp through servlet

I m not able to display image on jsp page plzz help
dis is my jsp page where d image is to be inserted...`
<h4>Welcome<%=rst.getString(1)%></h4>
<table>
<tr><td>Designation:<%=rst.getString(4)%></td><td></td></tr>
<tr><td>Date of Birth:<%=rst.getString(8)%>/<%=rst.getString(7)%>/<%=rst.getString(6)%></td></tr>
<tr><td>Qualification:<%=rst.getString(9)%></td></tr>
<tr><td>Full Address:<%=rst.getString(10)%><%=rst.getString(11)%></td></tr>
<tr><td>Contact No:<%=rst.getString(12)%></td></tr
<tr><td><img src="Image1?imgid=<%=rst.getString(14)%>" width="60" height="60"></img></td></tr>
</table>
and this is my servlet
try
{
String id1=request.getParameter("imgid");
Blob image=null;
byte[]imgData=null;
Class.forName("com.mysql.jdbc.Driver");
Connectioncon=DriverManager.getConnection("jdbc:mysql://localhost:3306/regf","root","password");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select image from teac where UserId='"+id1+"'");
}
if(rs.next()){
image = rs.getBlob(18);
int ln=(int) image.length();
imgData = image.getBytes(1,ln);
response.setContentType("image/jpeg");
OutputStream o = response.getOutputStream();
InputStream readImg = rs.getBinaryStream(18);
int index=readImg.read(imgData, 0,ln);
response.getOutputStream().write(imgData,0,ln);
response.getOutputStream().flush();
}
}
make sure the path "Image1?imgid=<%=rst.getString(14)%>" is correct and follow this format:
<td><img src="" alt="" border=3 height=100 width=100></img></th>.
Print this path on the console or on the page and check image exists there.

Paging with page numbers in JSP file not url

I am using spring paging framework for my form. I am testing the paging framework from here:
http://www.developer.com/java/web/article.php/10935_3830886_3/A-Pagination-Technique-Using-Spring.htm. The url for my form displays the page numbers like this:
1st page: http://...stuff../mySite/paging/paging/0
2nd page: http://...stuff../paging.do?action=list&p=1
3rd page: http://...stuff../paging.do?action=list&p=2
etc
The paging is not consistent and causes a problem in my code (note the difference btwn first pg and 2nd pg urls) and to keep my url clean, I prefer to store the page numbers as a hidden jsp parameter. Is it possible? Do you have any advice? Thanks.
My controller:
#Controller
#RequestMapping(value="/paging")
#ApplicationComponent(component="Paging")
public class PaginationController
{
private final Log logger = LogFactory.getLog(getClass());
#Autowired
private ItemDao itemDao;
#RequestMapping(value="/paging/{p}")
public ModelAndView list(#PathVariable("p") int page) throws Exception
{
ModelAndView mav = new ModelAndView("paging/paging");
System.out.println("In the paging controller");
// get data in a list from jsp
List searchResults = itemDao.getAllItems();
// initialize PagedListHolder with the list,
PagedListHolder pagedListHolder = new PagedListHolder(searchResults);
pagedListHolder.setPage(page);
int pageSize = 5;
pagedListHolder.setPageSize(pageSize);
mav.addObject("pagedListHolder", pagedListHolder);
return mav;
}
}
Here is the JSP:
<jsp:useBean id="pagedListHolder" scope="request" type="org.springframework.beans.support.PagedListHolder"/>
<%-- // create link for pages, "~" will be replaced with the proper page number --%>
<c:url value="/paging.do" var="pagedLink">
<c:param name="action" value="list"/>
<c:param name="p" value="~"/>
</c:url>
<tg:paging pagedListHolder="${pagedListHolder}" pagedLink="${pagedLink}"/>
<%-- // show only current page data --%>
<table width="200px" border="1">
<tr>
<th width="20px">No.</th>
<th>Print Items from pagedListHolder list</th>
</tr>
<c:forEach items="${pagedListHolder.pageList}" var="item">
<tr>
<td>${item.key}</td>
<td style="color:blue;font-weight:bold;text-align:right">${item.data}</td>
</tr>
</c:forEach>

Resources