how to fetch data from servlet ????



Share on Google+Share on Google+

how to fetch data from servlet ????
3 Answer(s)      4 years and 3 months ago
Posted in : JDBC

how to fetch data from servlet ????

View Answers

May 22, 2013 at 10:36 AM

hi friend,

Please explain what do you want to do exactly. Specify, from where you want to get data using servlet for example, whether from an html form, database, or from else where.

  1. getParameter(String name): A simple method of HttpServletRequest to get the value from an html form.

  2. How to get data from database : To get data from database you would have to first make a connection with database. To know more in detail how to get data from database using servlet you may go through the following links below, may this will be helpful for you


May 22, 2013 at 11:48 AM


<%@page import="java.sql.Statement"%> 
<%@page import="java.sql.DriverManager"%> 
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.ResultSet"%> 
<%@page contentType="text/html" pageEncoding="UTF-8"%> 

<!DOCTYPE html>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

          <h1>Welcome Madhab</h1>
          <form action="NewServlet" method="POST">
              <input type="submit" value="Click!">
          <table border="2">
      <% try {ResultSet rs = (ResultSet) request.getAttribute("data"); 
     while ( {%>             

  <td><input type="text" value="<%=rs.getString("state_id")%>"</td> 
<td><input type="text" value="<%=rs.getString("state_name")%>"</td>
<td><input type="text" value="<%=rs.getString("capital")%>"</td>
<% }} 
catch (Exception e) { 
                  } %>        

May 22, 2013 at 11:49 AM


 try {       
        Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://", "root", "root");

        String query = "select * from state_details"; 
        Statement s = null; 
        s = (Statement) con.createStatement(); 
        ResultSet rs = null;  
        rs = s.executeQuery(query);  
        request.setAttribute("data", rs);  
        RequestDispatcher rd = request.getRequestDispatcher("view.jsp");  
        rd.forward(request, response);  

    } catch (Exception e) {       
    finally {