读书人

初学者!JSP页面间传值有关问题

发布时间: 2013-09-07 14:12:44 作者: rapoo

菜鸟求助!!JSP页面间传值问题
我有两个JSP的页面,分别为1.jsp和2.jsp。
其中1.jsp中有语句


<body>
<img src="thumbs/9.jpg" width="179" height="100" alt="初学者!JSP页面间传值有关问题" onclick="show_id2('9')"/>
<script type="text/javascript" src="1.js"></script>
</body>


1.js内容如下:

var tu1;
tu1 = 3;

function show_id2(str){
tu1 = str;
}


2.jsp

<body>

Connection conn=null;
PreparedStatement pstmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");//加载JDBC驱动程序
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bbs","root","root");//连接MySQL数据库
}catch (ClassNotFoundException e) {
out.println("找不到驱动类");
} catch (SQLException e) {
out.println("连接MySQL数据库失败");
}
try{
Statement stmt = conn.createStatement();
String queryAll = "insert into user(id,username,password,email,sex) values(null,'candy','123','666','nan');";
int rs = stmt.executeUpdate(queryAll);
//获得所有记录

if(rs != 0)
{
out.println(tu1);
}
else
{
response.sendRedirect("No.jsp");
}
}catch (SQLException e) {
out.println("查询用户信息失败");
e.printStackTrace();
}
%>
</body>


我想在2.jsp中out.println(tu1);处用到tu1的值,怎么办?
[解决办法]
在1.jsp js代码中加入


window.location.href="2.jsp?tu1=str" //str是你想要传的值,tu1是参数名称
在2.jsp中获取这个参数,代码:
String tu1 = request.getparameter("tu1");

读书人网 >Java Web开发

热点推荐