학교용 온라인 상점을 만들고 있습니다. 내 제품을 SQL 데이터베이스에 저장하고 있습니다. 나는 cart_servlet에 자신의 ID를 보낼 옆에 버튼이있는 DB에 모든 제품을 표시하려고합니다. 제품을 표시하기 위해 나는 이렇게 같은 테이블을 생성 해요 : 내가 상상으로동적으로 생성 된 양식에서 서블릿 호출
는public static String getInventory(){
String result = "<table>";
for (Product p : DAO_Product.getProducts()) {
result = result + "<tr>"
+ "<td>" + p.getName() + "</td>"
+ "<td><img src=\"resources/images/" + p.getImage() + "\" alt=\"Duke waving his hand\"></td>"
+ "<td>" + p.getDollars() + "</td>"
+ "<td>" + p.getPennies() + "</td>"
+ "<td>" + p.getStock() + "</td>"
+ "<td>" + p.getDescription() + "</td>"
//creates a form consisting of one button and a hidden value
//clicking the button should submit the corresponding hidden value
+ "<td><form action=\"${pageContext.request.contextPath}/cart_servlet\" method=\"post\">"
+ "<input type=\"hidden\" name=\"product\" value=\"" + p.getId() + "\" />"
+ "<input type=\"submit\" name=\"submit\" value=\"Add to Cart\" />"
+ "</form>"
//End Form
+ "</tr>";
}
}
이 올바른 테이블을 생성하고 생성하는 HTML은 올바른 같습니다
<table><tr><td>Caverna</td>
<td><img src="resources/images/caverna.jpg" alt="sampleImage"></td>
<td>112</td><td>12</td><td>34</td>
<td> you are the bearded leader of a small dwarf family which lives in a little cave in the mountains. Together, you</td>
<td><form action ="${pageContext.request.contextPath}/cart_servlet" method="post">
<input type="hidden" name="product" value="4" />
<input type="submit" name="submit" value="Add to Cart" />
</form>
을 그러나 'HTTP 상태 404 - 찾을 수 없음'설명 : '요청한 리소스를 사용할 수 없습니다'라고 표시된 버튼을 클릭하면
@WebServlet("/cart_servlet")
public class CartServlet extends HttpServlet{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String result;
Status status = new Status();
req.setAttribute("status", status);
//RequestDispatcher view = req.getRequestDispatcher("browseProducts.jsp");
if (req.getParameter("submit") != null) {
}
//view.forward(req, resp);
}
}
내 'CartServlet'에서 모든 주석 봤는데, 아마 캔트 어떻게 든 컨텍스트를 가져 오거나 클래스를 찾을 확신하지만 방법에 대한 확신입니다 : 내가 요청하고 서블릿 내 'CartServlet'입니다 이 문제를 해결할 수 있습니다.
확인 $ 번에 {pageContext.request.contextPath}, 그것은 당신에게 당신이 찾고있는 경로를 제공해야합니다. 문제의 원인이라고 생각합니다. –
입력 해 주셔서 감사합니다. 경로가 맞는지 확인하고 다른 솔루션을 살펴 보았습니다. JSTL 라이브러리를 사용하여 HTML을 삽입하는 테이블을 작성해야한다는 것을 알게되었습니다. 이전에 내가했던 방식으로 코드를 실행하지 않을 것입니다. 나는 나의 발견에 대한 답을 제공했다. –