我使用Eclipse创建了一个网站,Servlet在该网站上将数据发送到jsp。但是我不知道为什么要更改Servlet中的数据。它仍然将旧数据发送到jsp。即使我尝试了这些选项..
菜单-项目-清洁(如果不使用自动构建,请单击此选项)
菜单-项目-自动构建项目(选中此选项)
这里是示例:
Product.java
public class Product {
private String id;
private String name;
private long price;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getPrice() {
return price;
}
public void setPrice(long price) {
this.price = price;
}
public Product(String id, String name, long price) {
super();
this.id = id;
this.name = name;
this.price = price;
}
}
ProductModel.java
public class ProductModel {
public Product find() {
return new Product("p0x","name x",700); // change to return new Product("www","aaa",1000);
}
public List<Product> findAll()
{List<Product> result= new ArrayList<Product>();
result.add(new Product("p01","name 1",100));// change to result.add(new Product("xxx","yyy",100));
result.add(new Product("p04","name 2",200));
result.add(new Product("p037","name 3",300));
return result;
}
}}
}
ProductController.java
@WebServlet("/urlaccess")
public class ProductController extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ProductController() {
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
// response.getWriter().append("Served at: ").append(request.getContextPath());
PrintWriter out= response.getWriter();
Gson gson= new Gson();
ProductModel productModel= new ProductModel();
String action = request.getParameter("action");
if(action.equalsIgnoreCase("demo1"))
{
out.print(gson.toJson(productModel.find()));
out.flush();
out.close();
}
else if (action.equalsIgnoreCase("demo2"))
{
out.print(gson.toJson(productModel.findAll()));
out.flush();
out.close();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<title>Insert title here</title>
<script type="text/javascript">
$(document).ready(function(){
$('#button2').click(function(){
$.ajax({
type:'GET',
url:'urlaccess?action=demo2',
header:{
Accept: "application/json; charset=utf-8",
"Content-Type":"application/json; charset=utf-8"
},
success:function (da){
//var product= $.parseJSON(result);
var listproducts= $.parseJSON(da);
var s='';
for( var i=0; i<listproducts.length;i++)
{s+='ID:'+ listproducts[i].id+'<br>Name:'+listproducts[i].name+'<br>Price:'+listproducts[i].price+'<br>==========<br>';}
document.getElementById('result2').innerHTML=s;
// alert(product.id);
}
})
});
$('#button1').click(function(){
$.ajax({
type:'GET',
url:'urlaccess?action=demo1',
header:{
Accept: "application/json; charset=utf-8",
"Content-Type":"application/json; charset=utf-8"
},
success:function (data){
var product= $.parseJSON(data);
//var listproducts= $.parseJSON(result);
//var s='';
//for( var i=0; i<listproducts.length;i++)
// {s+='ID:'+ listproducts[i].id+'<br>Name:'+listproducts[i].name+'<br>Price:'+listproducts[i].price+'<br>==========<br>';}
//document.getElementById('result2').innerHTML=s;
alert(product.id);
document.getElementById('result1').innerHTML=product.id;
}
})
});
})
</script>
</head>
<body>
<h1>JSON to JSP</h1>
<fieldset>
<legend>Demo1 </legend>
<input type="button" value="Display Object" id="button1"> <br>
<div id="result1"></div>
</fieldset>
<fieldset>
<legend>Demo2 </legend>
<input type="button" value="Display List Object" id="button2"> <br>
<div id="result2"></div>
</fieldset>
</body>
</html>
当我在ProductModel.java中更改为]时>
public class ProductModel {
public Product find() {
return new Product("www","aaa",1000);
}
public List<Product> findAll()
{List<Product> result= new ArrayList<Product>();
result.add(new Product("xxx","yyy",100));
result.add(new Product("p04","name 2",200));
result.add(new Product("p037","name 3",300));
return result;
}
}}
}
我使用Eclipse创建了一个网站,Servlet在该网站上将数据发送到jsp。但是我不知道为什么要更改Servlet中的数据。它仍然将旧数据发送到jsp。即使我尝试了这些选项..菜单-...