2013-12-13 74 views
1

我是jsp的新手。我試圖從我的java類訪問我的jsp頁面的值。我通過了很多頁面並實施了很多東西,而我所做的一切都變成了一團糟。 這是我的DAO類方法從java類導入值到jsp

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> 

蔭試圖顯示「姓名」,「慨」,「城市」,「性別」,表中的「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 

做幫助..在此先感謝..

回答

1

設置該屬性在轉發到JSP頁面之前,你的servlet「請求」。

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. 

而且使用

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

嘗試用下面的代碼獲得JSP的價值,

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

您好..我想你的代碼但是我得到一個java.lang.NullPointerException它無法獲取控制檯中顯示的值..它仍然返回null – Alvin

0

您必須將您的屬性設置爲請求對象,並將servlet類轉發到JSP,因爲它是JSP中的隱式對象,因此您可以從Request對象獲取屬性。

但是,當調用getAttribute時,必須將其轉換爲適當的Object類型。

並確保您沒有使用重定向機制,否則您的請求對象將爲空,因爲它是一個新的JSP對象。只有在Forward Mechanism中,請求對象纔會被複制到JSP上的新請求對象。

您可以使用Net上的示例以及 Pradeep Kr Kaushal示例。

我已經寫了這個,只是讓你通知有關Request對象的Forward和Redirect機制效果。

0

你不叫你dao class顯示方法在任何地方。

在你的DAO類,(在Java中開始與約定小寫字母方法名),改變方法,像這樣:

public DisplayDo display() throws Exception { ... 

在方法的結束,從結果填充它們之後,初始化一個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>