I need to implement onLoad behavior of that jsp page.
JSP page calls a servlet, gets data from db and displays it to the user.
How can I implement onLoad behavior? Meaning data from db is displayed to the
user every time jsp pages is loaded.
The servlet will make all the database calls and the create a Model object to hand to the displaying jsp.
I know this doesn’t answer your question, but this is the best way to do what you’re asking.
Now, to answer your question…
You could create some tag libraries that will make the database call for you and add all the data to your request. I have done this before, but i’m disapointed with the outcome in the long run and I’m going to have to refactor all the work that was done to change it to the solution offered above.
Actually, I have implemented the servlet already with DataBase class which handles all requests to DB and returns ResultSet. Now I was thinking to pass the Result Set to jsp page which will parse it and assign values to appropriate tags. But how is it done onLoad?
Im actually a tad confused what you want here… what do you mean by onload? Your jsp page can make the call to ANY object (why would you need a servlet) and display the results by itself without any special treatment whatsoever similar to this:
<html>
<head>
<title>DB JSP</title>
</head>
<body>
<%
StringBuffer sb = new StringBuffer();
resultset = new MyDBObject().getResults(request);
[loop thru results adding them to the StringBuffer;
%>
<%= sb %>
</body>
</html>
Now thats the down and dirty SLOPPY way to do it but it still does it. Personally I would make an object that return the preformatted output to the JSP in a StringBuffer object and then just add it to the response, something like this:
Again its sloppy but I dont have time to type out all the code for you but I think you get the idea… I DONT like handling ANYTHING like a resultset in a JSP… thats what objects are for! JSPs are (IMO) ONLY for formatting and presenting whatever process results you have to the client.