2014-07-26 4 views
0

학교용 온라인 상점을 만들고 있습니다. 내 제품을 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은 올바른 같습니다

output form

<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'입니다 이 문제를 해결할 수 있습니다.

+0

확인 $ 번에 {pageContext.request.contextPath}, 그것은 당신에게 당신이 찾고있는 경로를 제공해야합니다. 문제의 원인이라고 생각합니다. –

+0

입력 해 주셔서 감사합니다. 경로가 맞는지 확인하고 다른 솔루션을 살펴 보았습니다. JSTL 라이브러리를 사용하여 HTML을 삽입하는 테이블을 작성해야한다는 것을 알게되었습니다. 이전에 내가했던 방식으로 코드를 실행하지 않을 것입니다. 나는 나의 발견에 대한 답을 제공했다. –

답변

0

나는이 모든 잘못에 다가 섰다. 대신 JSTL을 사용하여 JSP 내에서 테이블을 작성해야합니다. 다음 코드로 내가 'getInventory()를'삭제 할 수 있었다 :

첫 번째 단계는 JSP의 상단에 다음 줄을 추가하는 것입니다

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 

당신이 다운로드했는지 확인 JSTL 라이브러리를 만들고 프로젝트에 추가했습니다. 당신은 그것을 here 찾을 수 있습니다.

두 번째 단계는 필요한 변수를 선언하는 것입니다. 이 경우 '제품'의 ArrayList가 필요합니다.

<% 
    ArrayList<Product> products = DAO_Product.getProducts(); 
    //Set an attribute to reference our Arraylist 
    pageContext.setAttribute("products", products); 
%> 

이제 테이블을 만들 준비가되었습니다. 표에는 각 제품에 대한 정보가 표시되며 'cart_servlet'을 호출하는 마지막 열에 버튼이 있습니다. 우리는 'JSTL'에서 '대해 forEach'를 사용하려고 :

<table border="1" cellpadding="5" > 
    <tr> 
     <th>Name</th> 
     <th>Image</th> 
     <th>Price</th> 
     <th>Description</th> 
     <th>Stock</th> 
     <th>Add to Cart?</th> 
    </tr> 
    <c:forEach var="p" items="${products}" > 
     <tr> 
      <td>${p.name}</td> 
      <td><img src="resources/images/${p.getImage()}" alt="Cool Pic"></td> 
      <td>$${p.dollars}.${p.pennies}</td> 
      <td>${p.description}</td> 
      <td>${p.stock}</td> 
      <td> 
       <form action="${pageContext.request.contextPath}/cart_servlet" method="post"> 
        <input type="hidden" name="productID" value="${p.id}"> 
        <input type="submit" name="addToCart" value="Add to Cart"> 
       </form> 
      </td> 
     </tr> 
    </c:forEach> 
</table> 
관련 문제