Alvin
Alvin

Reputation: 416

importing values from java class to jsp

I am a novice in jsp. i am trying to access values from my java class into my jsp page. i went thru a lot of pages and implemented a lot of stuff and i thing i made it all into a mess. this is my dao class method

    public DisplayDO Display(DisplayDO disDo)throws Exception
{
    System.out.println("inside dao display");
    Connection conn = null;
    try{
            conn = DbConnection.getConn();
        }
    catch(Exception e)
    {
        e.printStackTrace();
    }
        Statement statement = null;
    try{
        statement = conn.createStatement();
        }
    catch(SQLException e)
    {
        e.printStackTrace();
    }
    System.out.println("dbconnection established");
    int userid=disDo.getUserid();

    try
    {
        System.out.println("inside dao try");
        System.out.println("userid = "+userid);
        String str= "Select address_id from user where user_id =910";
        //+userid;
        PreparedStatement ps= conn.prepareStatement(str) ;
        ResultSet rs=ps.executeQuery(str);
        int address1=0;
        while (rs.next())
        {
            address1= rs.getInt(("address_id"));
        }

        int addressid = 0;
        addressid = address1;
        System.out.println("values of   addressid= "+   addressid +"address1= "+ address1); 
        System.out.println("query execution successfull");
        str="select name,street_name,city,gender,reg_date from user,address,registeration where user.address_id="+addressid+" && address.address_id= "+addressid+" && registeration.user_id="+userid+" ;";
        rs=ps.executeQuery(str);
        UserDO user = new UserDO();
        RegDO regDO = new RegDO();
        AddressDO addressDO = new AddressDO();
        while(rs.next())
        {
            user.setName(rs.getString("name"));
            System.out.println("name= "+rs.getString("name"));
            addressDO.setStreetname(rs.getString("street_name"));
            System.out.println("street_name= "+rs.getString("street_name"));
            addressDO.setStreetname(rs.getString("city"));
            System.out.println("city = "+rs.getString("city"));
            regDO.setGender(rs.getString("gender"));
            System.out.println("gender = "+rs.getString("gender"));
            regDO.setReg_date(rs.getString("reg_date"));
            System.out.println("reg_date = "+rs.getString("reg_date"));
            System.out.println("date using getter"+regDO.getReg_date()); 
        }
    }
    catch(Exception e)
    {
        e.setStackTrace(null);
    }
    return disDo;

}

THis code is working perfectly and i am getting the values in the console..

the following code is my jsp page.

    <%@page import="java.lang.String" %>
    <%@page import="java.io.*" %>
    <%@page import="com.quinoid.e_tender.databean.RegDO"%>
    <%@page import="com.quinoid.e_tender.databean.AddressDO"%>
    <%@page import="com.quinoid.e_tender.databean.UserDO"%>
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <!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>Insert title here</title>
      </head>
      <body>
       <h1>Your Values</h1> 

    <%
 System.out.print("inside display_1");
UserDO user=(UserDO)request.getAttribute("name");
out.println(user);
AddressDO add=(AddressDO)request.getAttribute("add");
RegDO reg=(RegDO)request.getAttribute("reg");
String name,street_name,city,gender,reg_date;
name=(String)request.getAttribute("user");

 /*street_name=add.getStreetname();
city=add.getCity();
gender=reg.getGender();
reg_date=reg.getReg_date();
 */%>
<!-- <script type="text/javascript">

    /*document.getElementById("name").innerHTML =(String)request.getAttribute("name");
document.getElementById("street_name").innerHTML = add.getStreetname();
    document.getElementById("city").innerHTML = add.getCity();
    document.getElementById("gender").innerHTML = reg.getGender();
    document.getElementById("reg_date").innerHTML = reg.Reg_date(); */

    </script> -->
     <form name="innerHTML" method ="post">
 <table border="1">
 <tr>   
    <th>    Name   </th>
    <th>Street Name</th>
    <th>    City   </th>
    <th>   Gender  </th>
    <th>Registration date</th>
  </tr>
  <tr>
          <td id=name>        </td>
          <td id=street_name> </td>
          <td id=city>        </td>
          <td id=gender>      </td>
          <td id=reg_dae>     </td>
  </tr>
 </table>
    </form> 
    </body>
    </html>

Iam trying to display the values of "name","street_name","city","gender","reg_date" in the table and failed miserably.. This is the result in my console

    the servlet is in user display
    In display method
    userid= 910
    inside dao display
    dbconnection established
    inside dao try
    userid = 910
    values of   addressid= 118address1= 118
    query execution successfull
    name= anjana
    street_name= nagar
    city = tvm
    gender = F
    reg_date = 1990-08-15
    date using getter1990-08-15
    exiting display method
    inside display_1
    inside display_1

do help.. thanks in advance..

Upvotes: 2

Views: 2936

Answers (4)

Pieter
Pieter

Reputation: 903

You are not calling you dao class Display method anywhere.

In your dao class, change the method like so (method names in java start with a lowercase letter by convention):

public DisplayDo display() throws Exception { ...

At the end of the method, after populating them from the ResultSet, initialize a DisplayDo object and set the userDo, addressDo and regDo objects:

    DisplayDO displayDo = new DisplayDO();
    displayDo.setRegDo(regDO);
    displayDo.setAddressDi(addressDO);
    displayDo.setUserDo(userDO);

In your jsp, add the import for your dao class (assuming it is called DisplayDao):

<%@page import="com.quinoid.e_tender.dao.DisplayDao"%>

Add this to your jsp to create a new DisplayDao and access its display() method:

<% DisplayDao displayDao = new DisplayDao();
   DisplayDo displayDo = displayDao.display();
   UserDO userDo = displayDo.getUserDo();
   ...
%>

You can then output values like this:

<td id="name"><%= userDo.getName() %></td>

Upvotes: 0

emphywork
emphywork

Reputation: 448

You must set your attributes to the request object and than forward the servlet class to JSP, where you can getAttribute from the Request object as it is an implicit object in the JSP.

But when getAttribute is called you must cast it to appropriate Object type.

And Make sure you are not using Redirect mechanism, otherwise your Request object will be null because it is a new object for JSP. Only in Forward Mechanism the request object is copied to the new request object on JSP.

You can use the examples available on Net and as well Pradeep Kr Kaushal example.

I have written this to just let you inform about Forward and Redirect mechanism effects on Request object.

Upvotes: 0

Noman ali abbasi
Noman ali abbasi

Reputation: 539

try with following code,

document.getElementById("street_name").innerHTML = '<%=add.getStreetname()%>';
document.getElementById("city").innerHTML = '<%=add.getCity()%>';
document.getElementById("gender").innerHTML =  '<%=reg.getGender()%>';
document.getElementById("reg_date").innerHTML =  '<%=reg.Reg_date()%>';

Upvotes: 0

Pradeep Kr Kaushal
Pradeep Kr Kaushal

Reputation: 1546

Set this attribute in 'request' in your servlet before forwarding to the jsp page.

UserDO user=(UserDO)request.getAttribute("name");
out.println(user);
AddressDO add=(AddressDO)request.getAttribute("add");
RegDO reg=(RegDO)request.getAttribute("reg");
String name,street_name,city,gender,reg_date;
name=(String)request.getAttribute("user");

set these values as

request.setAttribute("name",userDo); //UserDo instance.
request.setAttribute("add",addressDo);//AddressDO  instance. and so on...

And then forward it

 request.getRequestDispatcher("xxxx").forward(request,response);//xxxx is jsp page you are forwarding.

And get the value in jsp using

<%=xxx%>//xxx is the reference as **name**, **city**..

Upvotes: 1

Related Questions