如何将给定链接的详细信息作为参数发送到另一个jsp页面?

时间:2015-03-18 21:47:11

标签: java javascript sql jsp jstl

我尝试将category.jsp页面中的产品链接到自己的product.jsp页面。当我点击类别页面上的给定产品时,浏览器中的链接显示为:http://localhost:8080/ecommerce/product?389,但问题是:我收到HTTP Status 404 - Not Found error。我知道这是因为它没有找到页面,但是在尝试了几个解决方案之后......我似乎无法找到能够产生影响的代码片段......任何帮助都会是非常感谢

Controller Servlet:

package controller;


import cart.ShoppingCart;
import wishlist.Wishlist;
import entity.Category;
import entity.Product;
import java.io.IOException;
import java.util.Collection;
import javax.ejb.EJB;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import session.CategoryFacade;
import session.ProductFacade;

public class ControllerServlet extends HttpServlet {


    @EJB
    private CategoryFacade categoryFacade;
    @EJB
    private ProductFacade productFacade;

@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

    String userPath = request.getServletPath();
    HttpSession session = request.getSession();
    Category selectedCategory;
    Product selectedProduct;
    Collection<Product> categoryProducts;

 // if product page is requested
        if (userPath.equals("/product")) {

            // get productId from request
            String productId = request.getQueryString();

            if (productId != null) {

                // get selected product
                selectedProduct = productFacade.find(Short.parseShort(productId));

                // place selected product in session scope
                session.setAttribute("selectedProduct", selectedProduct);
            }

这是category.jsp中用于调用产品的代码:

<a href="product?${product.id}"><img class="img" src="${initParam.productImagePath}${product.name}.jpg"></a>

完整的Category.jsp:

<table style="text-align: left; width: 100%;" border="0" cellpadding="0" cellspacing="0">

    <c:forEach var="product" items="${categoryProducts}" varStatus="iter">

<td>
<tbody>
<tr>
<td
style="vertical-align: middle; width: 180px; text-align: center; height: 180px;"
class="cell"><a href="product?${product.id}"><img class="img" src="${initParam.productImagePath}${product.name}.jpg"></a><br>
</td>
<td
style="vertical-align: middle; width: 140px; text-align: center;">${product.name}<br>
</td>
<td
style="vertical-align: middle; width: 125px; text-align: center;">$
${product.price}<br>
</td>
<td
style="vertical-align: middle; width: 125px; text-align: center;">

    <form action="addToWishlist" method="post"><br><br> <input
name="productId" value="${product.id}" type="hidden"> 

    <input class="submit" value="<fmt:message key='AddToWishlist'/>" type="submit"> </form>
<br>
</td>
<td
style="vertical-align: middle; width: 123px; text-align: center;">


    <form action="addToCart" method="post"><br><br> 

    <input name="productId"
value="${product.id}" type="hidden"> <input class="submit" value="<fmt:message key='AddToCart'/>"
type="submit"> 


</form>
<br>
</td>
</tr>
</tbody>
  </c:forEach>
</table>

这是Product.jsp(目标网页):

<table style="text-align: left; width: 100%; height: 172px;" border="0"
cellpadding="0" cellspacing="0">



<tbody>
<tr>
<td colspan="1" rowspan="6" style="vertical-align: top;">product_gallery<br>
</td>
<td colspan="1" rowspan="6" style="vertical-align: top;"><img class="img" src="${initParam.productBigImagePath}${product.name}.jpg"><br>
</td>
<td style="vertical-align: top;">${selectedProduct.name}<br>
</td>
<td style="vertical-align: top;"><br>
</td>
</tr>
<tr>
<td style="vertical-align: top;">$ ${selectedProduct.price}</td>
<td style="vertical-align: top;"><br>
</td>
</tr>
<tr>
<td style="vertical-align: top;"><br>
</td>
<td style="vertical-align: top;"><br>
</td>
</tr>
<tr>
<td style="vertical-align: top;"><br>
</td>
<td style="vertical-align: top;"><br>
</td>
</tr>
<tr>
<td colspan="2" rowspan="1" style="vertical-align: top;">${selectedProduct.description}</td>
</tr>
<tr>
<td style="vertical-align: top;"><form action="addToWishlist" method="post"><br><br> <input
name="productId" value="${product.id}" type="hidden"> 

    <input class="submit" value="<fmt:message key='AddToWishlist'/>" type="submit"> </form><br>
</td>
<td style="vertical-align: top;"><form action="addToCart" method="post"><br><br> 

    <input name="productId"
value="${product.id}" type="hidden"> <input class="submit" value="<fmt:message key='AddToCart'/>"
type="submit"> 


</form></td>
</tr>
<tr>
<td style="vertical-align: top;"><br>
</td>
<td style="vertical-align: top;"><br>
</td>

<td colspan="2" rowspan="1" style="vertical-align: top;"><ul><li style="background-color: rgb(198, 255, 201); width:100%; text-align:center; border-radius:2em;"><a href="${value}"><fmt:message key='ContinueShopping'/></a></li>
                            </ul><br>
</td>
</tr>
</tbody>

</table>

1 个答案:

答案 0 :(得分:0)

自己解决了这个问题。我忘了在web.xml中添加参数:

<servlet-mapping>
        <servlet-name>ControllerServlet</servlet-name>

        <url-pattern>/product</url-pattern>


    </servlet-mapping>