I Need Help Regarding How to Print ResultSet Which i Am Getting From Servlet in Jsp Here Is The Servlet Code:
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class EditRecord extends HttpServlet {
private static final long serialVersionUID = 1L;
Connection con;
Statement stmt;
ResultSet rs;
DBConnection dbconn;
String sql;
HttpSession session;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPerform(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPerform(request, response);
}
private void doPerform(HttpServletRequest request,HttpServletResponse response)
{
try {
PrintWriter out = response.getWriter();
System.out.println("This is EditRecord Servlet");
session= request.getSession();
String id =(String) session.getAttribute("id");
System.out.println(id);
dbconn = new DBConnection();
con= dbconn.getConnection();
System.out.println(con);
sql ="select * from STUDENT_CRUD where id='"+id+"' ";
System.out.println(sql);
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
System.out.println("Resultset From EditRecord Servlet ="+rs);
if(rs.next())
{
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
System.out.println(rs.getString(3));
System.out.println(rs.getString(4));
System.out.println(rs.getString(5));
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
session.setAttribute("resultset", rs);
RequestDispatcher rd= request.getRequestDispatcher("editrecord.jsp");
rd.forward(request, response);
//response.sendRedirect("editrecord.jsp");
}
catch (Exception e)
{
System.out.println("This is finally block editrecord.jsp");
e.printStackTrace();
}
}
}
Then The Jsp File i write is like:
<%@page import="java.sql.ResultSet"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<form name="editrecord">
<% ResultSet rs =(ResultSet)session.getAttribute("resultset");
out.println("this is getAttribute of resultset"+rs);
%>
<TABLE BORDER="1">
<TR>
<TH>ID</TH>
<TH>FIRSTNAME</TH>
<TH>LASTNAME</TH>
<TH>SUBJECT</TH>
<TH>YEARS</TH>
</TR>
<% while(rs.next()){ %>
<tr>
<!-- <td>ID</td> -->
<td> <input type="text" name="id" value="<%=rs.getString(1) %>"></td>
</tr>
<tr>
<!-- <td>FirstName</td> -->
<td><input type="text" name="firstname" value="<%=rs.getString(2) %>"></td>
</tr>
<tr>
<!-- <td>LastName</td> -->
<td><input type="text" name="lastname" value="<%=rs.getString(3) %>"></td>
</tr>
<tr>
<!-- <td>Subject</td> -->
<td><input type="text" name="subject" value="<%=rs.getString(4) %>"></td>
</tr>
<tr>
<!-- <td>years</td> -->
<td><input type="text" name="years" value="<%=rs.getString(5) %>"></td>
</tr>
<% } %>
</TABLE>
</form>
The result i am getting is :
enter code here
this is getAttribute of resultsetoracle.jdbc.driver.OracleResultSetImpl@5e79fc ID FIRSTNAME LASTNAME SUBJECT YEARS
The ResultSetData i am Getting inside EditServet but not in editrecord.jsp Why? pls help..........
I suggest to avoid Scriplet instead use JSP Standard Tag Library and Expression language that is easy to use and less error prone.
Steps to follow:
sample code:
POJO:
public class StudentDetail{
private String id;
private String fistName;
private String lastName;
private String subject;
private int year;
// getter & setter
}
Servlet:
List<StudentDetail> list = new ArrayList<StudentDetail>();
if (rs.next()) {
StudentDetail studentDetail = new StudentDetail();
studentDetail.setId(rs.getString("ID"));
studentDetail.setFistName(rs.getString("FIRST_NAME"));
studentDetail.setLastName(rs.getString("LAST_NAME"));
studentDetail.setSubject(rs.getString("SUBJECT"));
studentDetail.setYear(rs.getInt("YEAR"));
list.add(studentDetail);
}
request.setAttribute("list",list);
RequestDispatcher rd= request.getRequestDispatcher("editrecord.jsp");
rd.forward(request, response);
JSP:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:if test="${list not empty }">
<table>
<c:forEach items="${list}" var="record">
<tr>
<td>${record.id }</td>
<td>${record.firstName }</td>
<td>${record.lastName }</td>
<td>${record.subject }</td>
<td>${record.year }</td>
</tr>
</c:forEach>
</table>
</c:if>