我正在尝试在eclipse Helios中运行Tomcat v6.0.35上的简单Web应用程序。当我尝试运行servlet时,我在浏览器上收到以下错误。
状态报告:“此URL不支持HTTP方法GET消息” description:“所请求的资源不允许使用指定的HTTP方法(此URL不支持HTTP方法GET)” 这是我的web.xml代码..
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>servletDemo</display-name>
<servlet-name>DemoServlet</servlet-name>
<servlet-class>com.lara.DemoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DemoServlet</servlet-name>
<url-pattern>/servletDemo.do</url-pattern>
</servlet-mapping>
</web-app>
这是html文件index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="DemoServlet"method="post">
<input type="submit" value="submit">
</form>
</body>
</html>
现在这是DemoServlet.java
package com.lara;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DemoServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
public DemoServlet()
{
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
PrintWriter pw=response.getWriter();
System.out.print("1st appp demo...!!!");
}
}
我的文件夹结构是这样的......
类文件也进入WEB-INF \ classes文件夹,并尝试使用不同的映射样式(如\ servletDemo和index.html等)进入web.xml,但我的servlet没有在浏览器上运行althogh索引文件是在浏览器上运行.. :(
答案 0 :(得分:3)
您的错误是因为您的servlet没有实现doGet
:尝试将doGet
函数添加到类中。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
PrintWriter pw=response.getWriter();
System.out.print("1st appp demo...!!!");
}