雖然我試圖從servlet向jsp傳遞數據,但數據正在成功傳遞,但它看起來像alert,實際上我想在新的jsp頁面中顯示它。將數據servlet傳遞給jsp
這裏是我的servlet和我想要顯示它的JSP文件,
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("Servlet called!!");
String feature=request.getParameter("id");
PrintWriter out = null;
Connection conn = null;
Statement stmt=null;
ResultSet rs;
try{
conn = JBDC.ConnectionFactory.getConnection();
// Execute SQL query
stmt = conn.createStatement();
String sql;
sql = "SELECT * FROM customers.add_voice where Feature='" +feature+ "'";
rs = stmt.executeQuery(sql);
// Extract data from DB
if(!rs.next()){
//Do nothing
}else{
do{
String price = rs.getString("Price");
response.setContentType("text/html");
request.setAttribute("feature", feature);
request.setAttribute("price", price);
request.getRequestDispatcher("/SelectedFeature.jsp").forward(request, response);
}while(rs.next());
}
// Clean-up
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
}
//在jsp中我取它
<%=(String)request.getAttribute("feature")%>
<%=(String)request.getAttribute("price")%>
//我的JSP文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<META http-equiv="Content-Style-Type" content="text/css">
<script src="javascript/jquery-3.1.0.min.js"></script>
<script src="WebContent/javascript/basic.js"></script>
<script src="javascript/basic.js"></script>
<title>Insert title here</title>
</head>
<body>
<%=(String)request.getAttribute("feature")%>
<%=(String)request.getAttribute("price")%>
</body>
</html>
你的意思是看起來像警報。您正在使用forward方法傳遞一個頁面,並使用getAttribute()方法獲取參數。你需要什麼? – Santhucool
@Santhucool我添加一張圖片請檢查 – cano
還沒有看到任何圖片 – Santhucool