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 referencing a servlet on a jsp page

Status
Not open for further replies.

4345487

Programmer
Dec 31, 2002
132
US
Hi,

I have a servlet that creates and returns a Recordset. Does anybody have a little sample of how to reference the servlet is a jsp page to display the values from the recordset. The code is below

Thanks in advance.



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

public class GetDataSample extends HttpServlet
{
/** Creates a new instance of the class */
public GetDataSample(HttpServletRequest request, HttpServletResponse response, JspWriter out)
throws UnavailableException
{
// set up some stuff if you need to here
}

public GetDataSample()
{
//default contstructor
}

public ResultSet getRecordSet() throws UnavailableException
{
String url = "jdbc:eek:dbc:NorthWindJava";
Connection con;
ResultSet rs;
String createString;
createString = "Select FirstName, LastName, Title, Address from employees";
Statement stmt;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(url, "uid", "psw");
stmt = con.createStatement();
rs = stmt.executeQuery(createString);
stmt.close();
con.close();
return rs;
}
catch (Exception e)
{
throw new UnavailableException(e.getMessage());
}
}
}

 
Looks like you're using the wrong tool for the job. Servlets handle requests and respond or forward as necessary. But you're just trying to create a datasource object.

Try making your above object just a plain old Java class, pass in appropriate params to the constructor, put it in your class library (WEB-INF/classes or something) and then call it from some other Servlet or JSP. Does this make sense?

Hope it helps,
petey

News and views of some obscure guy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top