Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

passing ResultSet object from Servlet to Jsp

843841Feb 1 2006 — edited Jun 1 2008
Hi 2 all!

I'm new to servlets/jsp and i'm facing a problem:
I can't pass a resultset object from a servlet to a jsp!

Summary,
An html form sends (post method) inputs (type ="text") to the servlet. The servlet creates the connection to a database and makes the statement to the db. The statement uses the inputs from the html form. The db returns a Resultset. obj. Is there a way representing this Resultset. in a jsp?

Here is my code, not finished..... :)


02.jsp (the form only)

...
...
<form name="form1" method="post" action="/servlet/ServletSearch">
  <table width="43%" border="0.5" align="center">
    <tr> 
      <td width="33%"><div align="center"><strong>ID</strong></div></td>
      <td width="36%"><div align="center"><strong>Surname</strong></div></td>
      <td width="31%"><div align="center"><strong>Name</strong></div></td>
    </tr>
    <tr> 
      <td><div align="center">
          <input type="text" name="pid" size="5">
        </div></td>
      <td><div align="center">
          <input type="text" name="psurname" >
        </div></td>
      <td><div align="center">
          <input type="text" name="pname">
        </div></td>
    </tr>
    <tr> 
      <td><div align="center"><strong>Laboratory Test</strong></div></td>
      <td><div align="center"><strong>Department</strong></div></td>
      <td><div align="center"><strong>Results</strong></div></td>
    </tr>
    <tr> 
      <td><div align="center">
          <input type="text" name="plabtest">
        </div></td>
      <td><div align="center">
          <input type="text" name="pdept">
        </div></td>
      <td><div align="center">
          <input type="text" name="presults">
        </div></td>
    </tr>
    <tr> 
      <td colspan="3"><div align="center"></div>
        <div align="center"> </div>
        <div align="center"></div></td>
    </tr>
  </table>
 <center> 
    <input name="Submit" type="submit" value="Submit">
  </center>
</form>
...
...
...
-------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------

.ServletSearch.java


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

public class ServletSearch extends HttpServlet {
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();

		String id = null;
		String surname = null;
		String name = null;
		String labtest = null;
		String dept = null;
		String results = null;

		Connection connection = null;

		if (request.getParameter("pid") != null)
			id = (String) request.getParameter("pid");

		if (request.getParameter("psurname") != null)
			surname = (String) request.getParameter("psurname");

		if (request.getParameter("pname") != null)
			name = (String) request.getParameter("pname");

		if (request.getParameter("plabtest") != null)
			labtest = (String) request.getParameter("plabtest");

		if (request.getParameter("pdept") != null)
			dept = (String) request.getParameter("pdept");

		if (request.getParameter("presults") != null)
			results = (String) request.getParameter("presults");

		String sqlsearch = "select * from myuser.PATIENT_ADM_CLIN where pat_id='"
				+ id
				+ "' or pat_surname='"
				+ surname
				+ "' or pat_name='"
				+ name
				+ "' or pat_labtest='"
				+ labtest
				+ "' or pat_dept='"
				+ dept + "' or pat_results='" + results + "' ";

		try {
			connection = DBconn.connectToDB();
			Statement search = connection.createStatement();
			ResultSet rs = search.executeQuery(sqlsearch);



/* 
 * I've tried to get the values first  and then loop through the multiple
 * results in the JSP. It doesn't happened of course, it makes no
 * sense...  I can't call the ResultSet from the servlet so simple. 
 * Also, the following Strings can be ommited. Any ideas what could
 * solve my problem? (Ignore the following strings...)
 */



			String paid = rs.getString("PAT_ID");
			String pasurname = rs.getString("PAT_SURNAME");
			String paname = rs.getString("PAT_NAME");
			String palabtest = rs.getString("PAT_LABTEST");
			String padept = rs.getString("PAT_DEPT");
			String paresults = rs.getString("PAT_RESULTS");

		} catch (SQLException e) {
			System.err.println("SQLException: " + e);
		}

		ServletContext sc = this.getServletContext();
		RequestDispatcher rd = sc.getRequestDispatcher("/03_Search.jsp");
		rd.include(request, response);

	}
}
thks and sorry 4 my english...
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 29 2008
Added on Feb 1 2006
13 comments
3,374 views