使用tomcat上的java httpservlet,此URL不支持HTTP方法POST

时间:2014-07-20 23:12:19

标签: java tomcat servlets

我一直在使用HTTP方法通过html表单调用我的httpservlet时,此URL不支持POST。我无法看到我出错的地方。我在Tomcat服务器上运行它。提前谢谢。

servlet应该向浏览器打印一组随机数。

这是我的servlet:

package servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.TreeSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import utilities.RandNumSet;

public class RandomServlet extends HttpServlet {

/**
 * 
 */
private static final long serialVersionUID = 1L;

public RandomServlet() {
    // TODO Auto-generated constructor stub
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    doPost(req, resp);

}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    PrintWriter out = resp.getWriter();
    resp.setContentType("text/html");
    TreeSet<Integer> randNum = RandNumSet.generateRandNumSet();
    Iterator<Integer> iterator = randNum.iterator();
    String numString = "";
    while (iterator.hasNext()){
        numString = numString + iterator.next() + " ";
    }


    out.println("<head>");
    out.println("<title>");
    out.println("Your Random Numbers!");
    out.println("</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1> Your Random Numbers! </h1>");
    out.println("<h1> "+ numString + " </h1>");
    out.println("</body>");
}

}

这是调用html表单片段:

<div id ="form">
    <form action="RandomServlet" method="post">
        <input type="submit" value="Randomize!"/>
    </form>
</div>

以下是web.xml片段

    <servlet>
        <display-name>RandomServlet</display-name>
        <servlet-name>RandomServlet</servlet-name>
        <servlet-class>servlets.RandomServlet</servlet-class>
    </servlet>


    <servlet-mapping>
        <servlet-name>RandomServlet</servlet-name>
        <url-pattern>/RandomServlet</url-pattern>
    </servlet-mapping>

1 个答案:

答案 0 :(得分:1)

我使用以下方式及其作品......

首先:首先检查你所做的目录 -

com.ServletExample 
|-JavaResource 
   |-src
      |-com.servletExample
          |-RandomServlet.java 
   |-WebContent
      |-META-INF
      |-WEB-INF
          |-Lib
          |-web.xml
   |-index.jsp

第二名:index.jsp在这里

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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>
<div id ="form">
    <form action="RandomServlet" method="post">
        <input type="submit" value="Randomize!"/>
    </form>
</div>
</body>
</html>

第三名:RendomServlet.java在这里

package com.servletExample;
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 RandomServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

public RandomServlet() {
super();
}

protected void doGet(HttpServletRequest request,
    HttpServletResponse response) throws ServletException, IOException {

}

protected void doPost(HttpServletRequest request,
    HttpServletResponse response) throws ServletException, IOException {

PrintWriter out = response.getWriter();
response.setContentType("text/html");

out.println("<head>");
out.println("<title>");
out.println("Your Random Numbers!");
out.println("</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1> Your Random Numbers! </h1>");
out.println("<h1> " + "Hello" + " </h1>");
out.println("</body>");
}

}

第四名: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>JSPExample</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
        <display-name>RandomServlet</display-name>
        <servlet-name>RandomServlet</servlet-name>
        <servlet-class>com.servletExample.RandomServlet</servlet-class>
    </servlet>


    <servlet-mapping>
        <servlet-name>RandomServlet</servlet-name>
        <url-pattern>/RandomServlet</url-pattern>
    </servlet-mapping>
</web-app>

我希望这会奏效。