I am not able to find what is wrong with my webApp. This is my first project and i'm kinda stuck. All i know is that something is going wrong in ShoppingCart.java whenever i click add to cart in index.html it gives me a error code of series 500 i tried to look into console but didn't understand it fully. It is very simple code please tell me if you see something..
index.html
<table border="1px">
<th>PIZZA NAME</th> <th>PRICE</th> <th>ADD TO CART</th>
<form action="addToCart">
<tr>
<td> Peppereno</td>
<td> 20$ </td>
<td align="center">
<input type="hidden" name="name" value="peppereno"/>
<input type="hidden" name="price" value="20"/>
<input type="submit" value="Add to Cart"/>
</td>
</tr>
</form>
<form action="addToCart">
<tr>
<td> Barbeque</td>
<td> 10$</td>
<td align="center">
<input type="hidden" name="name" value="barbeque">
<input type="hidden" name="price" value="10"/>
<input type="submit" value="Add to Cart" />
</td>
</tr>
</form>
<form action="addToCart">
<tr>
<td> Farmhouse</td>
<td> 35$ </td>
<td align="center">
<input type="hidden" name="name" value="farmhouse"/>
<input type="hidden" name="price" value="35"/>
<input type="submit" value="Add to Cart"/>
</td>
</form>
Cart.java
package com.cart;
import java.util.HashMap;
public class Cart {
HashMap <String,Integer> cartItems;
public Cart()
{
cartItems = new HashMap<>();
}
public HashMap<String,Integer> getItems()
{
retu cartItems;
}
public void addToCart(String item,Integer price)
{
cartItems.put(item,price);
}
public void deleteFromCart(String item)
{
cartItems.remove(item);
}
}
ShoppingCart.java
package com.cartServlet;
import com.cart.*;
@WebServlet("/ShoppingCart")
public class ShoppingCart extends HttpServlet {
private static final long serialVersionUID = 1L;
public ShoppingCart() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
HttpSession session = request.getSession();
Cart shoppingCart = (Cart)session.getAttribute("cart");
if(shoppingCart==null)
{
shoppingCart = new Cart();
session.setAttribute("cart",shoppingCart);
}
String name = request.getParameter("name");
Integer price = Integer.parseInt(request.getParameter("price"));
shoppingCart.addToCart(name, price);
session.setAttribute("cart",shoppingCart);
RequestDispatcher dispatch = request.getRequestDispatcher("response.html");
dispatch.include(request, response);
HashMap<String,Integer> items = shoppingCart.getItems();
PrintWriter out = response.getWriter();
out.println("<table border='1px'>");
for(String key: items.keySet()){
out.println("<tr><td>"+key+" - </td><td>"+"$"+items.get(key)+"</td></tr>");
}
out.println("</table>");
}
}
response.html
<h3>Pizza successfully added to the cart</h3>
<form action="index.html">
Add more pizza to cart<input type ="submit" value="go"/>
</form>
<h2>Cart</h2>
web.xml
<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>com.cartServlet.ShoppingCart</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-patte>/addToCart</url-patte>
</servlet-mapping>
