这个servlet案例是我写的一个例子,有一个jsp页面负责提交一个简单的订单,运行没问题,但是已提交订单到servlet就提示 (jdStudy为项目名)HTTP Status 404 - /jdStudy/post--------------------------------------------------------------------------------type Status reportmessage /jdStudy/postdescription The requested resource (/jdStudy/post) is not available.
以下是web.xml的配置
<servlet>
<servlet-name>ShoppingCart</servlet-name>
<servlet-class>com.jiandong1.ShoppingCart</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ShoppingCart</servlet-name>
<url-pattern>/ShoppingCart</url-pattern>
</servlet-mapping>
映射的servlet类为:
package com.jiandong1;省略导包内容public class ShoppingCart extends HttpServlet { @Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
} @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//取得Session对象,如果不存在Session对象则为此次会话创建该对象
HttpSession session = req.getSession(true);
Integer itemCount = (Integer)session.getAttribute("itemCount");
//如果session是新的
if (itemCount == null) {
itemCount = new Integer(0);
PrintWriter out = resp.getWriter();
resp.setContentType("text/html");
//接收传来的参数
String[] itemSelected;
String itemName;
itemSelected = req.getParameterValues("item");
if (itemSelected != null) {
for (int i = 0; i < itemSelected.length; i++) {
itemName = itemSelected[i];
itemCount = new Integer(itemCount.intValue() + 1);//+1将1转换为字符串对象
//购买的条目
session.setAttribute("item" + itemCount, itemName);
//总条目数
session.setAttribute("itemCount", itemCount);
}
}
out.print("<html>");
out.print("<title>");
out.print("选项列表");
out.print("</title>");
out.print("<body><h4>Session 列表:</h4><hr><br><br>");
for (int i = 0; i <= itemCount.intValue(); i++) {
out.print((String)session.getAttribute("item" + i) + "<hr>");
}
out.print("</body>");
out.print("</html>");
out.close();
}
} @Override
public void destroy() {
super.destroy();
}}

解决方案 »

  1.   


    页面代码:
    <html>
      <head>
        <title>图书购物</title>
    <meta content="text/html;charset=utf-8">
      </head>
      
      <body bgcolor="#FFFFFF">
        <h1 align="center">图书商店</h1>
        <hr>
        <p><b>请选择下列信息:</b></p>
        <form action="post" action="ShoppingCart">
         <table width="500" border="0" cellspacing="0" cellpadding="0">
         <tr>
         <td width="69">
         <input type="checkbox" name="item" value="java编程思想">
         </td>
         <td width="431">
         列表一:java编程思想
         </td>
         </tr>
         <tr>
         <td width="69">
         <input type="checkbox" name="item" value="MySQl数据库">
         </td>
         <td width="431">
         列表二:MySQl数据库
         </td>
         </tr>
         <tr>
         <td width="69">
         <input type="checkbox" name="item" value="J2EE开发教程">
         </td>
         <td width="431">
         列表三:J2EE开发教程
         </td>
         </tr>
         </table>
         <hr>
         <p>
         <input type="submit" name="jd_submit" value="购买">
         </p>
        </form>
        <p>&nbsp;</p>
      </body>
    </html>
      

  2.   

    改成这个/jdStudy/ShoppingCart
    不是post方法
    而是跟是的url-pattern
      

  3.   

    web.xml贴出来,才知道怎么回事啊。 自己好好检查一下"<servlet-mapping>"下的"<url-pattern>"有没有配置对啊。
      

  4.   

    <form action="post" action="ShoppingCart">怎么两个action阿
     
     
      

  5.   

    <form action="post" action="ShoppingCart">  !!!!!!!!!!!!!!!!!!!!!!!!
      

  6.   

    楼上两位正解....
    <form method="post" action="ShoppingCart">