良好的一天,在我的簡單測試webapp項目我有這個test.jsp的文件:當我將表單發佈到servlet時,爲什麼會得到「java.net.MalformedURLException:no protocol:」?
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Test page</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/testCaptureParts" method="post">
<textarea id="inputxml" name="inputxml" rows="20" cols="80"></textarea>
<input type="submit" value="submit" />
</form>
</body>
</html>
它生成此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=UTF-8">
<title>Test page</title>
</head>
<body>
<form action="/TestApp/testCaptureParts" method="post">
<textarea id="inputxml" name="inputxml" rows="20" cols="80"></textarea>
<input type="submit" value="submit" />
</form>
</body>
</html>
的/ TestApp/testCaptureParts是實現的doPost(一個servlet ) 方法。
當我提交表單,我甚至之前的servlet的doPost方法得到以下異常()方法被調用:
I 30, 2014 11:05:36 ODP. org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [TestCapturePartsServlet] in context with path
[/TestApp] threw exception
java.net.MalformedURLException: no protocol: captureParts
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at cz.test.servlet.TestCapturePartsServlet.doPost(TestCapturePartsServlet.java:47)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
我試圖硬編碼的網址爲:
http://127.0.0.1/TestApp/testCaptureParts
但隨後當我在Mozilla中提交表單時,會打開「SaveAs」對話框,並顯示「您打開testCaptureParts,它是應用程序/八位字節流,您想要做什麼?」。 如果我將它提交給Internet Explorer,它將打開一個空白頁面,其中包含一些不可讀的字符。
á`Fžű^Čůňţđba`°ÁÍÖKâ×C´
Á6đMäϬ?ye(lЧĄŻÂ
ÉfDĺŇ›Áţל]ęÓhą–ŹôŐ
@±.GLMC©°ľf`qpP
請指教,我該如何將表單發佈到servlet以作進一步處理?先謝謝你。
編輯 - 這是servlet類:
public class TestCapturePartsServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException {
doPost(httpRequest, httpResponse);
}
@Override
protected void doPost(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException {
String inputxml = httpRequest.getParameter("inputxml");
httpResponse.setContentType("text/plain");
PrintWriter out = httpResponse.getWriter();
try {
String s = "Servlet response.";
out.println(s);
out.println("Parameter value = " + inputxml);
} finally {
out.close();
}
}
}
- 當我在瀏覽器作爲一個GET請求我收到預期的文本響應打開URL。
- 當我從瀏覽器張貼表單到URL我得到的「應用程序/八位字節流」的東西。
- 當我從RESTClient發佈一些數據到URL時,我得到了預期的文本響應。
我切換到調試模式,並在doPost()方法的第一行設置了斷點。斷點上的點1和3停止執行。但是對於第2點來說,它看起來並沒有調用doPost()方法。
看到這個servlet的代碼(或者至少它的doPost方法在第一眼看起來出問題了)可能會有幫助... – fvu
我剛加入它。 – Vojtech