get 要求と post 要求の両方に応答できるサーブレットを作成しました。問題は、フォームを使用して投稿リクエストを送信すると、応答がないことです。これがコードです。
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 の設定:
詳細 URL を入力するページ:http://localhost:8080/SimpleServletProject/SimpleForm.html
送信を押した後の結果ページ (完全に空) の URL:http://localhost:8080/XmlServletPath