将值从java类导入jsp

时间:2013-12-13 06:37:35

标签: java eclipse jsp

我是jsp的新手。我正在尝试从我的java类访问我的jsp页面中的值。我经历了很多页面,并实现了很多东西,我把它弄得一团糟。 这是我的dao类方法

    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;

}

这些代码工作正常,我在控制台中获取值..

以下代码是我的jsp页面。

    <%@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>

我试图在表格中显示“name”,“street_name”,“city”,“gender”,“reg_date”的值,然后失败了...... 这是我的控制台中的结果

    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

帮助..提前感谢..

4 个答案:

答案 0 :(得分:1)

在转发到jsp页面之前,在servlet中的'request'中设置此属性。

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");

将这些值设置为

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

然后转发

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

使用

获取jsp中的值
<%=xxx%>//xxx is the reference as **name**, **city**..

答案 1 :(得分:0)

尝试使用以下代码,

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()%>';

答案 2 :(得分:0)

您必须将您的属性设置为请求对象,然后将servlet类转发到JSP,您可以从Request对象获取getAttribute,因为它是JSP中的隐式对象。

但是当调用getAttribute时,必须将其强制转换为适当的Object类型。

并确保您没有使用Redirect机制,否则您的Request对象将为null,因为它是JSP的新对象。仅在Forward Mechanism中,请求对象被复制到JSP上的新请求对象。

您也可以使用Net上提供的示例 Pradeep Kr Kaushal 示例。

我写这篇文章是为了让您了解有关Request对象的转发和重定向机制效果。

答案 3 :(得分:0)

你没有在任何地方叫你dao class显示方法。

在你的dao类中,更改方法如此(java start中的方法名称按惯例用小写字母开头):

public DisplayDo display() throws Exception { ...

在方法结束时,在从ResultSet填充它们之后,初始化DisplayDo对象并设置userDo,addressDo和regDo对象:

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

在你的jsp中,为你的dao类添加导入(假设它被称为DisplayDao):

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

将此添加到您的jsp以创建新的DisplayDao并访问其display()方法:

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

然后您可以输出如下值:

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