File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC 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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "sending data from servlet to bean" Watch "sending data from servlet to bean" New topic
Author

sending data from servlet to bean

shaf kan
Greenhorn

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";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(
"jdbc dbc:testing"," "," ");
Statement statement = con.createStatement();
ResultSet rs = statement.executeQuery(testsql);

c.setName(rs.getString("place"));
request.setAttribute("person",c);
RequestDispatcher view = request.getRequestDispatcher("result.jsp");
view.forward(request,response);

rs.close();
statement.close();
con.close();

}//try
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){
this.name = n;
}
}

The JSP code:

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

<html>
<body>
<h1 align="center"> Beer Recomendations JSP</h1>
<p>

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

</body>
</html>
</body>
</html>

Thanx
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
Greenhorn

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..
rs.next(); // added
c.setName(rs.getString("place"));
request.setAttribute("person",c);


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

but never retrieved it in your jsp like
<%
request.getAttribute("person");
%>

btw
<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....
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: sending data from servlet to bean
 
Similar Threads
pls help me to put this into a servlet
bean problem
Problem running JSP with Resin
jsp:usebean | NoClassDefFoundError
Starting out on Servlets - from Head First Servlets and JSP