Siva Siva - 1 year ago 152
Java Question

unable to retrive data from database and display in JSP using Servlet

I am beginer in JSP and Servlets and if this question is really simple, Please bear with me and please don't close the question as I have googled about the issue and couldn't found exactly what the solution is.

I am passing data from jsp to servlet and inserting in database and from there retriving the same and passing to JSP for display

I was able to pass data from JSP and insert in database successfully but unable to retrive and display in jsp again. below is code.


<!DOCTYPE HTML><%@page language="java"
contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<form name=loginform action=TestServlet method=post>

Input ID <input type=text name="id" size=10></input> <br><br>

<Input type="button" value=Retrive> <br>
<label for = "getdata" ></label> <br>

value is <%= request.getSession().getAttribute("data") %>
<input type=submit value=submit></input>




import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.sql.*;

* Servlet implementation class TestServlet
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

* @see HttpServlet#HttpServlet()
public TestServlet() {
// TODO Auto-generated constructor stub

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub


* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub


// Actual logic goes here.
try {

String getvalue=request.getParameter("id");
PrintWriter out = response.getWriter();
out.println("attempting to read from table");

// Open a connection
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@IP:Port:ServiceName, UN, Pass);
// Execute SQL query
Statement stmt = conn.createStatement();
String sqlinsert, sqlselect;
sqlinsert = "Insert into abc(abc) values("+getvalue+")";
sqlselect="Select abc from abc";

ResultSet rs = stmt.executeQuery(sqlinsert);
ResultSet rs1 = stmt.executeQuery(sqlselect);
int id=0 ;
// Extract data from result set
//Retrieve by column name
id = rs1.getInt("ABC");

//Display values

out.println("ID: " + id + "<br>");

HttpSession session = request.getSession(false);
request.setAttribute("data", "0");
request.getRequestDispatcher("/loginform.jsp").forward(request, response);
// out.println("</body></html>");

// Clean-up environment
}catch(SQLException se){
//Handle errors for JDBC
}catch(Exception e){
//Handle errors for Class.forName



Really unable to understand where is the problem... every time I run the code I am able to see only
in JSP

Answer Source

Simply change request.getSession().getAttribute("data") to request.getAttribute("data") and it will work fine.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download