Reputation: 916
I work with servlets
and I need to get the product name when adding it to the cart!
I add products in this way, and then I try to transfer the name when I click on Add to Cart
<c:forEach items="${sessionScope.allProducts}" var="products">
<div class="col-md-4 fashion-grid">
<a href="single.jsp"><img src="images/product/${products.imageName}" width="250" height="350" alt=""/>
<div class="product">
<h3>PRODUCT NAME:</h3>
<h3>${products.name}</h3>
<p>${products.size}</p>
<p>${products.color}</p>
<p>${products.category.name}</p>
<p>${products.manufacturer.name}</p><br></br>
<p><span></span>${products.price}</p>
</div>
</a>
<div class="fashion-view"><span></span>
<div class="clearfix"></div>
<h4><a href="addToCart?name=${products.name}">Add to cart</a></h4>
</div>
But in my servlet i get this exception:
java.sql.SQLException: No value specified for parameter 1
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:1042)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
at dao.impl.ProductDAOImpl.getProductByName(ProductDAOImpl.java:84)
at service.impl.ProductServiceImpl.lambda$getProductByName$2(ProductServiceImpl.java:34)
at db.TransactionManager.doInTransaction(TransactionManager.java:26)
at service.impl.ProductServiceImpl.getProductByName(ProductServiceImpl.java:34)
at servlet.AddToCartServlet.doGet(AddToCartServlet.java:32)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
My add to cart servlet:
@Override
public void init() throws ServletException {
cartService = (CartService) getServletContext().getAttribute(CONTEXT_CART_SERVICE);
productService = (ProductService) getServletContext().getAttribute(CONTEXT_PRODUCT_SERVICE);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
cartService.add(productService.getProductByName(req.getParameter("name")));
req.getSession().setAttribute("totalPrice", cartService.getCartPrice());
req.getSession().setAttribute("cartItems", cartService.getProductForOrder());
LOG.debug("Total price: " + req.getSession().getAttribute("totalPrice"));
}
My ProductServlet:
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ProductFormExecute formExecute = new ProductFormExecute();
ProductsForm form = formExecute.executeFormRequest(req);
String sqlProductQuery = filteredProduct(form);
List<Category> allCategory = categoryService.getAllCategory();
req.getSession().setAttribute(ALL_CATEGORIES, allCategory);
List<CompanyManufacturer> allCompany = manufacturerService.getAllCompanyManufacturer();
req.getSession().setAttribute(ALL_COMPANIES, allCompany);
List<Product> allProduct = productService.getProductByParameter(sqlProductQuery);
req.getSession().setAttribute(ALL_PRODUCTS, allProduct);
if (allProduct.isEmpty()) {
req.getSession().removeAttribute(ALL_PRODUCTS);
req.setAttribute(ERROR_PRODUCTS, ERROR_PRODUCTS_MESSAGE);
}
int countProducts = productService.getAllProduct(productCount(sqlProductQuery)).size();
int productMax = form.getProductMax();
req.getSession().setAttribute(COUNT_PRODUCTS, countProducts);
req.getSession().setAttribute(PRODUCT_ON_PAGE_LIMIT, productMax);
req.getSession().setAttribute(SORTING, form.getSort());
req.getSession().setAttribute(FIND_BY_PRICE_MIN, form.getMinPrice());
req.getSession().setAttribute(FIND_BY_PRICE_MAX, form.getMaxPrice());
LOG.debug("Product max: " + productMax + " " + countProducts);
LOG.debug(productService.getAllProduct(productCount(sqlProductQuery)));
req.getRequestDispatcher(PRODUCT_PAGE).forward(req, resp);
}
My sql query:
> SELECT *, COUNT(*) AS 'count' FROM product INNER JOIN product_category
> on product.product_category_id = product_category.id INNER JOIN
> company_manufacturer_product on
> product.company_manufacturer_product_id=company_manufacturer_product.id
> WHERE pName IN('?') GROUP BY pName ORDER BY pName ;
Why do I get this error and how to correctly remove the data when I click on the link? Thank you in advance)
Upvotes: 0
Views: 45
Reputation: 109547
<a href="addToCart" name="${products.name}">
Should be
<a href="addToCart?name=${products.name}">
as you get the request parameter "name".
However there are other techniques, so look for examples on the web.
(For clarity the items var name should rather be product
than products
.)
Upvotes: 3