我是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
做幫助..在此先感謝..
您好..我想你的代碼但是我得到一個java.lang.NullPointerException它無法獲取控制檯中顯示的值..它仍然返回null – Alvin