关于tomcat加载servlet出错的问题,提示:HTTP Status 405 - HTTP method GET is not supported by this URL

digua123 2008-12-15 11:20:48
如题,最近从书上看了一段利用servlet搭建网上购物车的程序,用tomcat加载时就提示HTTP Status 405 - HTTP method GET is not supported by this URL,
从网上发现很多人都朋友到过这个问题,按照很多中解决方案我的问题还是没有结果。

最近要利用这个做一个大作业,时间比较紧,麻烦大家了,帮忙看一下问题处在哪?程序里明明没有使用doGet()方法,为什么还提示那个错误?

源程序如下:
package bits;

import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ShoppingServlet extends HttpServlet{
private void doForward(String strurl,HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
String url=strurl;
ServletContext sc=getServletContext();
RequestDispatcher rd=sc.getRequestDispatcher(url);
rd.forward(req,res);
}
public void doPost(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
req.setCharacterEncoding("GBK");
HttpSession session =req.getSession(false);
if(session==null)
{
res.sendRedirect("/jsp/error.html");
}
Vector buylist=(Vector)session.getAttribute("shopping.shoppingcart");
String action=req.getParameter("command");
if(action.equals("DELETE"))
{
String del=req.getParameter("productvalue");
int d=(new Integer(del)).intValue();
int i=0;
for(i=0;i<buylist.size();i++)
{
item cd=(item)buylist.elementAt(i);
if(cd.getID()==d)
{
break;
}
}
buylist.removeElementAt(i);
session.setAttribute("shopping.shooppingcart",buylist);
doForward("/Cart",req,res);
}
else if(action.equals("ADD"))
{
boolean match=false;
item aCD=getCD(req);
if(buylist==null)
{
buylist=new Vector();
buylist.addElement(aCD);
}
else
{
for(int i=0;i<buylist.size();i++)
{
item cd=(item)buylist.elementAt(i);
if(cd.getID()==aCD.getID())
{
cd.setQuantity(cd.getQuantity()+aCD.getQuantity());
buylist.setElementAt(cd,i);
match=true;
}
}
if(!match)
{
buylist.addElement(aCD);
}
session.setAttribute("shopping.shoppingcart",buylist);
doForward("/Eshopping",req,res);
}
}
else if(action.equals("CHECKOUT"))
{
float total=0;
for(int i=0;i<buylist.size();i++)
{
item anOrder=(item)buylist.elementAt(i);
float price=anOrder.getPrice();
int qty=anOrder.getQuantity();
total+=(price*qty);
}
total+=0.005;
String amount=new Float(total).toString();
int n=amount.indexOf('.');
amount=amount.substring(0,n+3);
req.setAttribute("amount",amount);
doForward("/Checkout.jsp",req,res);
}
}
private item getCD(HttpServletRequest req)
{
String myCD=req.getParameter("productvalue");
StringTokenizer t=new StringTokenizer(myCD,"|");
String ID=t.nextToken();
String name=t.nextToken();
String desc=t.nextToken();
String price=t.nextToken();
item cd=new item();
cd.setName(name);
cd.setID(Integer.parseInt(ID));
cd.setDesc(desc);
cd.setPrice((new Float(price)).floatValue());
cd.setQuantity(1);
return cd;
}
}
...全文
340 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
digua123 2008-12-16
  • 打赏
  • 举报
回复
加入doGet()函数后原来的问题没有了,最初还有问题应该是自己没重启tomcat
谢谢你~
digua123 2008-12-16
  • 打赏
  • 举报
回复
[Quote=引用楼主 digua123 的帖子:]
如题,最近从书上看了一段利用servlet搭建网上购物车的程序,用tomcat加载时就提示HTTP Status 405 - HTTP method GET is not supported by this URL,
从网上发现很多人都朋友到过这个问题,按照很多中解决方案我的问题还是没有结果。

最近要利用这个做一个大作业,时间比较紧,麻烦大家了,帮忙看一下问题处在哪?程序里明明没有使用doGet()方法,为什么还提示那个错误?

源程序如下:
package bits;

import j…
[/Quote]
补充一下:按照varlj说的方法加进doGet()函数后,原来的问题没有了,但出现了新的问题:
exception

java.lang.NullPointerException
bits.ShoppingServlet.doPost(ShoppingServlet.java:29
bits.ShoppingServlet.doGet(ShoppingServlet.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:627)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

不知道怎么修改?是不是和程序里引用得Vector类有关?
digua123 2008-12-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 varlj 的回复:]
加上doGet试试

public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
doPost(req,res);
}

不过……你是不是先把JAVA和JAVASCRIPT分清楚

[/Quote]

加进去了还是一样的提示,还是要谢谢你
不是分不清楚,是发布得时候找不到对应的板块,就发到javascript板块来了
varlj 2008-12-15
  • 打赏
  • 举报
回复
加上doGet试试

public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
doPost(req,res);
}

不过……你是不是先把JAVA和JAVASCRIPT分清楚

87,997

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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