简体   繁体   中英

JSP page keeps refreshing

okay so as soon as i navigate to this page and it loads I want to redirect it to 'basket.jsp' but for some reason it just keeps refreshing over and over. This must be something to do with me calling the onload function over and over again but i don't know why it is doing this.

My code:

 <% String empty = request.getParameter("emptyBasket"); if (empty!=null) { basket.clearBasket(); } String item = request.getParameter("addItem"); basket.addItem(item); %> <html> <head> <link rel="stylesheet" href="css/style.css" type="text/css"/> </head> <body> <table> <!-- some table stuff unimportant --> </table> <p> Order total = <%= basket.getTotalString()%> <% if ( basket.getTotal() > 0) { %> <form action="order.jsp" method="post"> <input type="text" name="name" size="20"> <input type="submit" value="Place Order" /> </form> <form action="basket.jsp" method="get"> <input type="hidden" name="emptyBasket" value="yes"> <input type="submit" value="Empty Basket" /> </form> <% } %> <script> window.onload = function(){ window.location.href = 'basket.jsp'; } </script> </body> </html> 

if basket.getTotal() > 0 you are creating 2 forms? and then what how are you submitting the data? and to where.. this code doesn't make sense

(its basically like if true create 2 forms and then on your refreshed page you have these 2 forms one submits to orders and one to basket

in general its bad to have more java than html on a view.. but i think this is the main problem atm

form action="basket.jsp" method="get" && window.location.href = 'basket.jsp';

you are submitting the form to that page and then you are refreshing it

Im guessing a few things that you're trying to do but i could be completely off.

Can you explain the logic here or what the flow is?

Also.. i don't think you should be receiving parameters on the html page you need a controller for that.

You should change your structure from everything being in the index.html to..

Project

-src

--controller.java

-web

--index.html

Submit the data in a form from the index.html to the controller.java (controller.java can be a servlet). In controller.java implement doGet/doPost functions and collect the data. You can even do the logic directly in that servlet and use request dispatcher to send the data back to your view.

  1. <meta http-equiv="refresh" content="0; url=your url to another page" /> However using meta for redirect is discouraged by W3C.

  2. <body onload=window.location='your url to another page'> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM