我正在尝试从jsp文件向Servlet发送参数。这是jsp文件。
BookData.jsp
<!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>
<link href="images/First.css" rel="stylesheet" />
</head>
<body>
<div class="header">
<select onchange="javascript:handleSelect(this)">
<option>
<%out.println(session.getAttribute("name")); %></option>
<option value="SignOut">Sign Out</option>
<option value="#" >Cart</option>
</select>
<script type="text/javascript">
function handleSelect(elm)
{
window.location = elm.value+".jsp";
}
</script>
</div>
<hr>
<%
try{
System.out.println("entering BookData.jsp");
final Connection conn=ConnManager.getInstance().getConnection();
String bid=request.getParameter("bid");//retrieving bid
System.out.println("value of bid is "+bid);
String q="select * from asad.book where bid=?";
PreparedStatement pstmt=(PreparedStatement)conn.prepareStatement(q);
pstmt.setString(1,bid);
ResultSet rs =(ResultSet) pstmt.executeQuery();
String title,url,genre,author,cost,description,seller;
while(rs.next()){
title=rs.getString(2);
url=rs.getString(3);
genre=rs.getString(4);
author=rs.getString(5);
cost=rs.getString(6);
description=rs.getString(7);
seller=rs.getString(8);
out.println("<img src="+ url + "style"+"=width:350px;height:350px>"+"</a>");
HttpSession sess=request.getSession();
sess.setAttribute("bookid",bid);
String urltoSend="/Summer/OrderDetails.java";
%>
<h1><%=title %></h1>
<span>
<span>Title : </span><%=title %><br>
<span>Genre : </span><%=genre %><br>
<span>Author : </span><%=author %>
<br><center><a href=<%=urltoSend %> >
BUY NOW</a></center>
<span>Cost : </span><%=cost %><br>
<span>Seller : </span><%=seller %><br>
<span>Description : </span><%=description %><br>
</span>
<%
}
}catch(Exception e){}
%>
</body>
</html>
现在接收参数的是Servlet OrderDetails.java
。
@WebServlet("/OrderDetails")
public class OrderDetails extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public OrderDetails() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
try{
System.out.println("inside try of orderdetails");
final Connection conn=(Connection) ConnManager.getInstance().getConnection();
Statement s=(Statement) conn.createStatement();
HttpSession session=request.getSession();
String username=(String) session.getAttribute("name");
String q="select * from asad.user where username=?";
PreparedStatement pstmt=(PreparedStatement)conn.prepareStatement(q);
pstmt.setString(1,username);
ResultSet rs =(ResultSet) pstmt.executeQuery();
int cost=Integer.parseInt(rs.getString(6));
//String bid=request.getParameter("id");
String bid=(String) session.getAttribute("bookid");
String query="insert into asad.orders(bid,username,cost)"+
" values('"+bid+"','"+username+"','"+cost+"')";
int x=s.executeUpdate(query);
System.out.println("inserted into orders table");//inserted into orders table
}catch(Exception e)
{}
}
}
但我是浏览器会出现requested resource not available
错误。我无法理解为什么?