Question: Fetching database field from servlet to jsp page ?

Ads

 
 
 

Share on Google+Share on Google+

praful kale
Fetching database field from servlet to jsp page ?
2 Answer(s)      3 years and 11 months ago
Posted in : JSP-Servlet

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

Ads
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

<[email protected] 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>

Ads









Related Tutorials/Questions & Answers:
Tutorials   
Java Spring Hibernate Struts Training fat image from db fat image from db fat image from db fat image from db unable to get value returned from javascript variable in page How to change image size before adding to mysql using jsp Design a registration.jsp page with input for user registration details Cart Quantity jsp servlet coding issue servlet,jsp login registration forms without database in ecalipse ide using hashmap to store the registraion details ? servlet,jsp login registration forms without database in ecalipse ide using hashmap to store the registraion details ? servlet,jsp login registration forms without database in ecalipse ide using hashmap to store the registraion details ? servlet,jsp login registration forms without database in ecalipse ide using hashmap to store the registraion details ? servlet,jsp login registration forms without database in ecalipse ide using hashmap to store the registraion details ? servlet,jsp login registration forms without database in ecalipse ide using hashmap to store the registraion details ? servlet,jsp login registration forms without database in ecalipse ide using hashmap to store the registraion details ? image pdf marking pdf marking Multiple image upload using JSP Read code from excel sheet and upload into database using JSP Student java about search engine Convert BLOB to PDF and save it to a location how to copy files from remote to local machine using SFTP SSL in java? Like Team viewer Socket and Servert -Jsp how to copy files from remote location to local using java? How To Store Multilple Images In MySQL database dynamically with image uploader and then retrieve them in image gallery using java how to create SOAP based web service in java? how to fetch data from mysql database table and draw a bar chart on that data using in jsp how to create users in LDAP using java? how to authenticate user using TIVOLI api in java? how to create users using IBM Tivoi API in jsp/servelet? Java/J2ee Please explain this code as many packages and classes are unknown to me .! how to download the uploaded folder files using jsp Login Query image is display from path of mysql database please reply the code to store dynamic fields in database? Reloading issue in jsp How to get Button Id ? calling one jap page from another jsp page login login Problem to get connection from DAO class to JDBC Page Loading Time Fix table's column's name row(1st row of the table) so that it does not move up when the table is scrolled up to view more rows below Sending hidden values

Ads

 
Advertisement null

Ads