为什么我的servlet配置正确,但是就是提示404找不到我配置的servlet

猴子搬来的救兵Castiel 2011-04-14 10:12:50
这个servlet案例是我写的一个例子,有一个jsp页面负责提交一个简单的订单,运行没问题,但是已提交订单到servlet就提示 (jdStudy为项目名)

HTTP Status 404 - /jdStudy/post

--------------------------------------------------------------------------------

type Status report

message /jdStudy/post

description 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();
}

}

...全文
949 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zaizaihai 2011-04-15
  • 打赏
  • 举报
回复
楼上两位正解....
<form method="post" action="ShoppingCart">
UPC_思念 2011-04-15
  • 打赏
  • 举报
回复
改成这个/jdStudy/ShoppingCart
不是post方法
而是跟是的url-pattern
  • 打赏
  • 举报
回复
<form action="post" action="ShoppingCart"> !!!!!!!!!!!!!!!!!!!!!!!!
wei_wxx 2011-04-15
  • 打赏
  • 举报
回复
<form action="post" action="ShoppingCart">

怎么两个action阿

  • 打赏
  • 举报
回复
[Quote=引用 4 楼 yingzhuo2011 的回复:]
web.xml贴出来,才知道怎么回事啊。

自己好好检查一下"<servlet-mapping>"下的"<url-pattern>"有没有配置对啊。
[/Quote]

开头部分就贴出来了。
yingzhuo2011 2011-04-15
  • 打赏
  • 举报
回复
web.xml贴出来,才知道怎么回事啊。

自己好好检查一下"<servlet-mapping>"下的"<url-pattern>"有没有配置对啊。
zh_666 2011-04-15
  • 打赏
  • 举报
回复
楼上两位正解
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lujun0527 的回复:]

LZ最好把页面的代码也贴出来
[/Quote]

页面代码:
<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> </p>
</body>
</html>
留下买路财 2011-04-14
  • 打赏
  • 举报
回复
LZ最好把页面的代码也贴出来

67,550

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧