发布请求在我的servlet中不起作用,但获取请求

时间:2013-03-20 14:15:32

标签: tomcat servlets intellij-idea

我创建了一个能够响应get和post请求的servlet。 问题是,当我使用表单发送帖子请求时,我得不到任何回复。 这是代码。

XmlServlet.java:

package org.skiabox.myservlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


public class XmlServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String fullName = request.getParameter("fullName");
        String userName = request.getParameter("userName");
        out.println("Hello from POST method " + userName + "! We know your full name is " + fullName);

        String prof = request.getParameter("prof");
        out.println("<br/>" + "You are a " + prof);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        String userName = request.getParameter("userName");
        out.println("Hello " + userName);
    }
}

SimpleForm.html:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <form method="post" action="/XmlServletPath">
        User Name:<input name="userName"/>
        Full Name:<input name="fullName"/>
        <br/>
        <br/>
        Profession:
        <input type="radio" name="prof" value="Developer">Developer</input>
        <input type="radio" name="prof" value="Architect">Architect</input>
        <input type="submit"/>
    </form>
</body>
</html>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
           version="3.0">

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <description>A simple servlet</description>
        <display-name>SimpleServlet</display-name>
        <servlet-name>SimpleServlet</servlet-name>
        <servlet-class>org.skiabox.myservlet.SimpleServlet</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>XmlServlet</servlet-name>
        <servlet-class>org.skiabox.myservlet.XmlServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>SimpleServlet</servlet-name>
        <url-pattern>/SimpleServlet</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>XmlServlet</servlet-name>
        <url-pattern>/XmlServletPath</url-pattern>
    </servlet-mapping>
</web-app>

SimpleServletProject.iml:

<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
  <component name="FacetManager">
    <facet type="web" name="Web">
      <configuration>
        <descriptors>
          <deploymentDescriptor name="web.xml" url="file://$MODULE_DIR$/web/WEB-INF/web.xml" />
        </descriptors>
        <webroots>
          <root url="file://$MODULE_DIR$/web" relative="/" />
        </webroots>
      </configuration>
    </facet>
  </component>
  <component name="NewModuleRootManager" inherit-compiler-output="true">
    <exclude-output />
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
    </content>
    <orderEntry type="inheritedJdk" />
    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="library" scope="PROVIDED" name="Tomcat 7.0" level="application_server_libraries" />
  </component>
</module>

Tomcat设置: picture1

picture2

页面输入详细信息网址:http://localhost:8080/SimpleServletProject/SimpleForm.html

按提交后的结果页面(完全为空)网址:http://localhost:8080/XmlServletPath

1 个答案:

答案 0 :(得分:3)

您的servlet正在http://localhost:8080/SimpleServletProject/XmlServletPath上,而不是在http://localhost:8080/XmlServletPath上。您的表单提交错误的网址。

只需相应地修复表单操作URL即可。取代

<form method="post" action="/XmlServletPath">

通过

<form method="post" action="/SimpleServletProject/XmlServletPath">

或者,如果呈现表单的页面无论如何都在同一个URL文件夹中

<form method="post" action="XmlServletPath">

或者,如果您担心上下文路径的动态性

<form method="post" action="${pageContext.request.contextPath}/XmlServletPath">