【问题标题】:servlet not running on tomcat v6.0.35 and eclipse heliosservlet 未在 tomcat v6.0.35 和 eclipse helios 上运行
【发布时间】:2023-03-08 08:13:01
【问题描述】:

我正在尝试在 Eclipse Helios 中的 Tomcat v6.0.35 上运行一个简单的 Web 应用程序。当我尝试运行 servlet 时,我在浏览器上收到以下错误。

状态报告:“此 URL 不支持消息 HTTP 方法 GET” 描述:“请求的资源不允许指定的 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 文件夹,并且还尝试进入具有不同映射样式的 web.xml,如 \servletDemo 和 index.html 等,但是我的 servlet 没有在浏览器上运行,尽管索引文件是在浏览器上运行..:(

【问题讨论】:

  • 将鼠标放在您已放在问题上的[servlet] 标签上方,等到黑色信息框出现,然后单击其中的信息链接。跨度>

标签: servlets


【解决方案1】:

您的错误是因为您的 servlet 没有实现 doGet:尝试将 doGet 函数添加到类中。

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{
    PrintWriter pw=response.getWriter();
    System.out.print("1st appp demo...!!!");
}

【讨论】:

  • 我应该用doGet替换doPost方法还是在servlet中添加doGet方法??
  • @KailashGaur 您需要将 doPost 替换为 doGet ,您可以将它们都保留在您的 servlet 类中
  • 谢谢你们。它正在工作,但为什么它不能使用 doPost 方法?
  • doPost 仅响应 method 属性设置为 POST 的 POST 请求(例如表单提交)。您需要验证您的方法属性是否设置正确:乍一看,您的 index.html 中的 method 之前似乎需要一个空格,您可能还有其他问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-20
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 2015-06-21
  • 2013-08-20
  • 1970-01-01
相关资源
最近更新 更多