Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sending data from servlet to bean

 
shaf kan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic