gpt4 book ai didi

java - 从java类导入值到jsp

转载 作者:行者123 更新时间:2023-11-30 11:24:49 25 4
gpt4 key购买 nike

我是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

帮忙..提前致谢..

最佳答案

在转发到 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.

并在jsp中使用获取值

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

关于java - 从java类导入值到jsp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20560533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com