我使用Eclipse创建了一个网站,其中Servlet将数据发送到jsp。但是我不知道为什么要更改 Servlet 中的数据。它仍然将旧数据发送到jsp。尽管我尝试了这些选项..

菜单 - 项目 - 清理(如果不使用自动构建,请单击此选项)

菜单 - 项目 - 自动构建项目(选中此选项)

我还重新启动并清理了 Tomcat 服务器

这里是示例:

产品.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; 
            } 
        } 

产品模型.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; 
        } 
    }} 
    } 

仍然显示这样的结果 请帮我。谢谢

请您参考如下方法:

谢谢@rhenesys。我想分享给每个有同样问题的人。您应该更改为其他浏览器。比如我换成了Chrome浏览器。这对我有用


评论关闭
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!