2013-03-20 105 views
1

我已經創建了一個能夠響應get和post請求的servlet。 問題是,當我使用表單發送帖子請求時,我無法回覆。 這是代碼。發佈請求不在我的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

回答

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"> 
+0

這是我第一次嘗試b如果它失敗了。現在我再次嘗試它可以工作。也許我輸入了錯誤的東西。無論如何謝謝你的回答! – skiabox 2013-03-20 18:44:44

+0

不客氣。 – BalusC 2013-03-20 18:48:00

+0

實際上,我正在學習本教程(https://www.youtube.com/watch?v=MnUJl3NYRRc&list=PLE0F6C1917A427E96),並且該人員通過使用簡單/ XmlServletPath作爲操作參數而不是完整路徑來管理相同的結果,但他使用另一個IDE(Eclipse)。 – skiabox 2013-03-20 18:48:30