进来有分----------jsp+javabean的问题!!! (100分)

L

lql0459

Unregistered / Unconfirmed
GUEST, unregistred user!
问题描述:
我的思路是用test.jsp呈现产品信息,conn.java作数据库连接与查询,
用set.jsp显示相关产品信息。现在的运行结果是:我不管点击cpu还是mainboard
都只得到cpu的结果(以最后一次传递propery为准),
1.我不知得到正确结果需怎样改正我的test.jsp?
2.有没有比这更好的解决方案?我看到过重定向url时可以传递参数,不知我能不能
将不同的sql串传递给set.jsp,那样就不用在test.jsp中写scripts了!我试用了
这种方法,但是我不知道怎样在set.jsp中设置传递的参数?与conn.java有关否?
源程序:(如下)
数据连接bean:(conn.java)//////////////////////////////////////////////////////
/*
function: javaBean for database connection
author: robornet(robornet@yahoo.com.cn)
date: 2002-03-22
*/
package robornet;
import java.sql.*;
public class conn{
String sDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr="jdbc:eek:dbc:siteconn";
Connection conns=null;
ResultSet rs=null;
private String sql=new String("");;
public conn(){
try{
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e){
System.err.println("conn():"+e.getMessage());
}
}
public String getsql(){
return sql;
}
public void setsql(String robor){
sql=robor;
}
public ResultSet executeQuery(String sql){
rs=null;
try{
conns=DriverManager.getConnection(sConnStr);
Statement stmt=conns.createStatement();
rs=stmt.executeQuery(sql);
}
catch (SQLException ex){
System.err.println("aq.executeQuery:"+ex.getMessage());
}
return rs;
}
}
产品显示test.jsp://///////////////////////////////////////////////////
<html>
<head>
<title>产品信息</title>
<meta http-equiv="Content-Type" content="text/html;
charset=gb2312">
</head>
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*" %>
<jsp:useBean id="showtextcpu" scope="page" class="robornet.conn" />
<jsp:setProperty name="showtext" property="sql" value="SELECT * FROM quoteofday where sort='主板'" />
</jsp:useBean>
<a href="set.jsp" target="_blank">CPU</a>
<jsp:useBean id="showtextmb" scope="page" class="robornet.conn" />
<jsp:setProperty name="showtext" property="sql" value="SELECT * FROM quoteofday where sort='cpu'" />
</jsp:useBean>
<p><a href="set.jsp" target="_blank">Mainboard</a> </p>
</body>
</html>
查询结果set.jsp////////////////////////////////////////////////////////////
<html>
<head>
<title>查询结果</title>
<meta http-equiv="Content-Type" content="text/html;
charset=gb2312">
</head>
<%@ page import="java.sql.*" %>
<jsp:useBean id="showtext" scope="application" class="robornet.conn" />
<body bgcolor="#FFFFFF" text="#000000">
<%
ResultSet RS=showtext.executeQuery(showtext.getsql());
String name,intro,price,tt;
out.print("<center>");
out.print("<table wdith='100%' border='1' class='unnamed1'");
while (RS.next()){
out.print("<tr>");
out.print("<td wdith='40%'>");
name=RS.getString("name");
out.print(name);
out.print("</td wdith='60%'>");
out.print("<td>");
intro=RS.getString("intro");
if (intro!=null) out.print(intro);
out.print("</td>");
out.print("<td wdith='10%'>");
price=RS.getString("price");
out.print(price);
out.print("</td>");
out.print("</tr>");
}
out.print("</table>");
out.print("</center>");
RS.close();
%>
</body>
</html>
 
不能回答问题的朋友,帮忙UP一下,动手10分!
不够再加!
 
怎么就只有一个人!
 
up:10分!
 
您可以用 scope="session" 试试:
<jsp:useBean id="showtextcpu" scope="session" class="robornet.conn" />
<jsp:useBean id="showtextmb" scope="session" class="robornet.conn" />
 
孙兄:
page、request、session、application我都试了,可还是不行! 谢谢你们的回答!
不过我自己现在改变了一种方案,在test.jsp中传递一个参数给set.jsp,然后在set中
根据参数来查询数据!
大富翁的确是一个比较好的论坛!再次感谢大家的回答!!!
 

只要是你的想法,无论正确与否,写后必给分!
不能回答的,UP一下同样有分!
现作出第一步决定:将下面这个问题的分数移至本论题!
http://www.delphibbs.com/delphibbs/dispq.asp?lid=977817
 
个人观点:觉得你对JSP指令的含义没有弄清楚,看你的test.jsp,你是不是认为
点cup就执行它后面的setProperty,点mainboard就传他后面的setProperty?
如果是这样的话,就错了。JSP指令在服务器端被执行,所以在页面载入到客户端时
已经执行完了。
我用Tomcat测试了一下,以下是服务器分析你的test.jsp生成的java文件的部分内容
JspRuntimeLibrary.introspecthelper(pageContext.findAttribute("showtext"), "sql","SELECT * FROM quoteofday where sort='涓绘澘'",null,null, false);
JspRuntimeLibrary.introspecthelper(pageContext.findAttribute("showtext"), "sql","SELECT * FROM quoteofday where sort='cpu'",null,null, false);
所以在下一个页面,你getProperty当然都是SELECT * FROM quoteofday where sort='cpu'。
另外,你的test.jsp的指令有些写错了,你看看发送到客户端浏览器的HTML文件内容
(不过可以不同的服务器有不同的结果,我用的tomcat)
 
来了呵呵分分。
 
shisam,谢谢你的指点!
按照第二种方案已解!再次感谢大家!
为节约空间,我就不贴sourcecode!
http://www.delphibbs.com/delphibbs/dispq.asp?lid=977817
这个问题已有答案!请上面的几位赶快到那领分!
 
多人接受答案了。
 
顶部