Fetching database field from servlet to jsp page ?

Fetching database field from servlet to jsp page ?

Hello Java Developers.

I am facing problem please help me. I am new in this web development field.

I wanted to pass some of the database field from servlet to jsp...

(i am opening my database in servlet init() method )

how to pass rs.getString(8) field to jsp page . { where 'rs' is Resultset object}

please help

Praful kale

View Answers

May 27, 2013 at 3:33 PM

hi friend,

You can store the value in a Collection and you may access it on the JSP page. ResultSet is tied with the Connection so you can't access it without Connection. To pass the value to the JSP page you may use Request setAttribute method and at the JSP page can retrieve this value using Request getAttribute. Consider the following code may this will be helpful for you.

Demo.java

package net.roseindia;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Demo extends HttpServlet {
    private static final long serialVersionUID = 1L; 

    public void init(ServletConfig config) throws ServletException{
          super.init(config);
          }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html");       
        String driverName = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/record";
        String user = "root";
        String psw = "root";    
        Connection con = null;      
        PreparedStatement ps = null;
        ResultSet rs = null;

        List<String> list = new ArrayList<String>();

        String sql = "select * from country";

        try{
            Class.forName(driverName);
            con = DriverManager.getConnection(url, user, psw);
            ps = con.prepareStatement(sql);
            rs = ps.executeQuery();         
            while(rs.next())
            {
                list.add(rs.getString(1));              
            }           
            request.setAttribute("list", list);         
            ServletContext context = getServletContext();
            RequestDispatcher rd = context.getRequestDispatcher("/rs.jsp");
            rd.forward(request, response);
        }
        catch(Exception sqle)
        {
            sqle.printStackTrace();
        }
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doGet(request, response);
    }

}

Continue...


May 27, 2013 at 3:34 PM

rs.jsp

<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Get ResultSet</title>
</head>
<body>
<%
List list = (ArrayList)request.getAttribute("list");
Iterator<String> itr = list.iterator();
%>
<table>
<%
while(itr.hasNext())
{
    String country = itr.next();
%>
<tr>
<td><%= country %></td>
</tr>
<%
}
%>
</table>
</body>
</html>









Related Tutorials/Questions & Answers:

Ads