File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes sending data from servlet to bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "sending data from servlet to bean" Watch "sending data from servlet to bean" New topic

sending data from servlet to bean

shaf kan

Joined: Mar 29, 2006
Posts: 2
I am trying to access a database in servlet (known as BeerSelect" and sending it to a bean (known as "Counter") to set its property. Then I tried to display this propert in the jsp but nothing is coming up, its blank. Am i doing something wrong or its not possible this way.

The servlet code is :

public class BeerSelect extends HttpServlet{

public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{

Counter c = new Counter();
String aa = " ";
try {
String testsql="SELECT * FROM Items";
Connection con = DriverManager.getConnection(
"jdbc dbc:testing"," "," ");
Statement statement = con.createStatement();
ResultSet rs = statement.executeQuery(testsql);

RequestDispatcher view = request.getRequestDispatcher("result.jsp");


catch(ClassNotFoundException e) {
System.out.println("Class Not Found : " + e.getMessage());
catch(SQLException exp) {
System.out.println("SQL Exception: " + exp.getMessage());
System.out.println("SQL State: " + exp.getSQLState());
System.out.println("Vendor Error: " + exp.getErrorCode());

The Bean code :
public class Counter{

public Counter(){}

private String name;

public String getName(){
return name;
public void setName(String n){ = n;

The JSP code:

<%@ page import="java.util.*,com.example.*" %>

<h1 align="center"> Beer Recomendations JSP</h1>

<jsp:useBean id="person" class="com.example.Counter" scope="request" />
person is <jsp:getProperty name="person" property="name" />


stu derby
Ranch Hand

Joined: Dec 15, 2005
Posts: 333
I think that if you look at the log where your servlet is writing exceptions, you should see an error message. Your JDBC code is broken.

When a ResultSet is returned to you, it is always positioned before the first row. You always have to call .next() on your ResultSet before you can get any fields from the first row.
majid khan jhung

Joined: Mar 27, 2006
Posts: 2
hi shariff khan(saf kan)
first problem is you need to set ResultSet cursor to next position to access ResultSet..; // added

Counter c = new Counter();// you have created new instance of Counter here
request.setAttribute("person",c); // you saved this instance in request scope.

but never retrieved it in your jsp like

<jsp:useBean id="person" class="com.example.Counter" scope="request" />
person is <jsp:getProperty name="person" property="name" />

server creates new instance of Counter from the above codes.

victory or death....
I agree. Here's the link:
subject: sending data from servlet to bean
It's not a secret anymore!