Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help on how to pass a Resultset to a JSP page

Status
Not open for further replies.

4345487

Programmer
Dec 31, 2002
132
US
Hi,

Any help will be appreciated.

I have a servlet that reads a database and creates a resultset that works OK because I can displau the data in the servlet. Now i want to pass the resultset to a JSP page but I'm having problems.


In the servlet I do this:

Resulset searchResultsList = stmt.executeQuery("select xx);
RequestDispatcher disp;
disp = getServletContext( ).getRequestDispatcher("/T1.jsp");
request.setAttribute("search.results", searchResultsList);
disp.forward(request, response);

In the JSP page I try this:

<%rs = request.getAttribute(&quot;search.results&quot;)%>

if I pass a string it works ok.

Thanks for any help.
 
In the JSP page I try this:

<%rs = request.getAttribute(&quot;search.results&quot;)%>

if I pass a string it works ok.


I couldn't get the problem.

Salih Sipahi
Software Engineer.
City of Istanbul Turkey
s.sipahi@sahinlerholding.com.tr
 
I'm sorry I don't explained correctlty. What was I tring to said was. With the code in the first message if I pass a string from the servlet to the JPS page it works OK but if I pass a ResultSet does not work.
 
Are you closing your statement stmt??

Salih Sipahi
Software Engineer.
City of Istanbul Turkey
s.sipahi@sahinlerholding.com.tr
 
This is the servlet.

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;

public class ChicoServlet extends HttpServlet
{
private void sendLoginForm(HttpServletResponse response, boolean withErrorMessage)
throws ServletException,IOException
{
response.setContentType(&quot;text/html&quot;);
PrintWriter out =response.getWriter();
out.println(&quot;<HTML>&quot;);
out.println(&quot;<HEAD>&quot;);
out.println(&quot;<TITLE>Login</TITLE>&quot;);
out.println(&quot;</HEAD>&quot;);
out.println(&quot;<BODY>&quot;);
/* check if we have an error */
if (withErrorMessage)
{
out.println(&quot;Login failed.Please try again.<BR>&quot;);
}

out.println(&quot;<BR>&quot;);
out.println(&quot;<BR>Please enter your user name and password.&quot;);

out.println(&quot;<BR><FORM METHOD=POST>&quot;);
out.println(&quot;<BR>User Name:<INPUT TYPE=TEXT NAME=userName>&quot;);
out.println(&quot;<BR>Password:<INPUT TYPE=PASSWORD NAME=password>&quot;);
out.println(&quot;<BR><INPUT TYPE=SUBMIT VALUE=Submit>&quot;);
out.println(&quot;</FORM>&quot;);
out.println(&quot;</BODY>&quot;);
out.println(&quot;</HTML>&quot;);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException
{
sendLoginForm(response,false);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException
{
/* get the user name and password */
String userName = request.getParameter(&quot;userName&quot;);
String password = request.getParameter(&quot;password&quot;);
if (userName != null && password != null && userName.equals(&quot;chico&quot;) && password.equals(&quot;desperado&quot;))
{
// Load Driver
try
{
Class.forName(&quot;sun.jdbc.odbc.JdbcOdbcDriver&quot;);
}
catch(ClassNotFoundException cnfe)
{
System.err.println(&quot;Error loading driver: &quot; + cnfe);
}

/* Define the connection url */
String url = &quot;jdbc:eek:dbc:NorthwindJava&quot;;

try
{
/* Stablish the connection */
Connection dbconn = java.sql.DriverManager.getConnection(url, &quot;xxx&quot;, &quot;xxx&quot;);

/* create a statement */
Statement stmt = dbconn.createStatement();

/* Create a result set */
ResultSet searchResultsList = stmt.executeQuery(&quot;Select FirstName, LastName, Title, Address from Employees order by LastName,FirstName&quot;);


RequestDispatcher disp;
disp = getServletContext( ).getRequestDispatcher(&quot;/Chico.jsp&quot;);
request.setAttribute(&quot;search.results&quot;, searchResultsList);
disp.forward(request, response);
}
catch(SQLException cnfe)
{
/* display a error to the user -- can't connect to DB */
System.err.println(&quot;Error connecting to the dtatase: &quot; + cnfe);
}

}
else
{
sendLoginForm(response,true);
}
}
}
 
Lets try the steps one by one.In fact don't prefer to pass your resultset to your jsp.Since you get your resultset in your servlet, create a loop and place the contents of your result set in your class and put these classes in a vector.
Then put this vector in a session.
However in this situation the code has no anormal syntax.Well now the steps.Instead putting your resultset into your request try putting in your session.
instead of
request.setAttribute(&quot;search.results&quot;, searchResultsList);
try this

javax.servlet.http HttpSession session = request.getSession();
session.setAttribute(&quot;search.results&quot;, searchResultsList);

then in your jsp page try to get your resultset from your session (just replace the word request into session).
Lets see if that works.[ponder]

Salih Sipahi
Software Engineer.
City of Istanbul Turkey
s.sipahi@sahinlerholding.com.tr
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top