1

I used Eclipse to create a website where Servlet send data to jsp. However I don't know why I change data in Servlet. It's still send old data to jsp. Even though I tried these options ..

Menu - Project - clean (Click this option if Build Automatically is not used)

Menu - Project - Project Build Automatically (Check this option)

I also restarted and clean Tomcat server enter image description here

Here is example:

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>

When I change inside ProductModel.java to

     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;
        }
    }}
    }

It's still show result like this enter image description here Please help me. Thank you

ELIP
  • 311
  • 6
  • 19
  • There is a lot to digest here, and you are not really telling us what is happening. In the JS, are the `alert`s appearing? *It's still send old data to jsp.* - what does this even mean? What is `old` and what is `new`? – Scary Wombat Jan 17 '20 at 06:52
  • @Scary Wombat I added more information as you comment. Please help – ELIP Jan 17 '20 at 07:15
  • OK, understood. If you try clicking on the `Add and Remove` menu option it should allow you to deploy directly to the locally installed Tomcat – Scary Wombat Jan 17 '20 at 07:21
  • This may also help https://stackoverflow.com/questions/15380125/cannot-add-a-project-to-a-tomcat-server-in-eclipse – Scary Wombat Jan 17 '20 at 07:22
  • @ScaryWombat , I'm sorry. I can't get your point. My project was added to server tomcat And I run it on one server only. – ELIP Jan 17 '20 at 07:54
  • Basically remove from tomcat server and add again – Scary Wombat Jan 17 '20 at 07:58
  • @ScaryWombat .Sure, I did it even I run it on new server. However If restart my computer It's solved. That's so strange – ELIP Jan 17 '20 at 08:04
  • 1
    Did you try to use a different browser or the incognito mode or CTRL+F5 (Windows) for a clean cache? – rhenesys Jan 18 '20 at 10:19

1 Answers1

0

Thank you @rhenesys. I want to share for every one have the same issue. You should change to another browser. For example I changed to Chrome browser. It worked for me enter image description here

ELIP
  • 311
  • 6
  • 19