当我尝试将数据从servlet传递给jsp时,数据传递成功但是,它看起来像是警告,实际上我想在新的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>
答案 0 :(得分:0)
这不是警报,您的JSP文件告诉浏览器输出内容是样式表。
<META http-equiv="Content-Style-Type" content="text/css">
将"text/css"
更改为"text/html"
,因为您似乎发送的内容是HTML。
虽然在你的评论中你说这是一个AJAX请求,所以你想要发回的东西可能不是HTML,你需要重新考虑使用JSP作为响应,因为AJAX请求通常不使用HTML来处理数据传输。