Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jakarta ee 为什么我会得到;java.net.MalformedURLException:无协议:“;当我将表单发布到servlet时?_Jakarta Ee_Servlet 3.0 - Fatal编程技术网

Jakarta ee 为什么我会得到;java.net.MalformedURLException:无协议:“;当我将表单发布到servlet时?

Jakarta ee 为什么我会得到;java.net.MalformedURLException:无协议:“;当我将表单发布到servlet时?,jakarta-ee,servlet-3.0,Jakarta Ee,Servlet 3.0,您好,在我的简单测试webapp项目中,我有一个test.jsp文件: <%@ 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>

您好,在我的简单测试webapp项目中,我有一个test.jsp文件:

<%@ 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>
但是,当我在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();
        }
    }
}
  • 当我在浏览器中打开URL作为GET请求时,我得到了预期的文本响应
  • 当我将表单从浏览器发布到URL时,我得到的是“应用程序/八位字节流”内容
  • 当我将一些数据从RESTClient发布到URL时,我得到了预期的文本响应
  • 我切换到调试模式,并在doPost()方法的第一行设置断点。断点上的点1和点3停止执行。但对于第2点,似乎根本没有调用doPost()方法。

    两件事:

  • 除非您想对servlet的输出进行特殊处理,否则使用编写器要容易得多
  • 您没有通过setContentType宣布输出类型,因此浏览器必须对您抛出的数据的性质进行粗略猜测
  • 简单地说,使用上面的建议,您的方法可以如下所示

    public void doGet(HttpServletRequest request, HttpServletResponse response)
          throws IOException, ServletException
    {
        // read the parameter that got passed
        String inputxml = request.getParameter("inputxml");
        response.setContentType("text/plain"); 
        PrintWriter out = response.getWriter();
        try {
           String s = "Servlet response";
           out.println(s);
           out.println("parameter value = " + inputxml);
        } finally {
           out.close();
        }
    }
    

    注意:servlet可以使用OutputStream或PrintWriter,但不能同时使用两者。

    查看该servlet的代码(或者至少是它的doPost方法,乍一看出错)可能会有所帮助……感谢您的回复。不幸的是,它没有解决我的问题。我更新了我的问题。有点远,但是你能试着用
    来调用它,看看会发生什么吗?我感到羞愧。我没有注意到URL中缺少端口号。应该是。现在它起作用了。谢谢您的时间。@Vojtech这些事情发生了:)祝您的项目好运
    á`Fžű^Čůňţđba`°ÁÍÖKâ×­C´
    Á6đMäϬ?ye(lЧĄŻÂ
    ÉfDĺŇ›Áţל]ęÓhą–ŹôŐ
    @±.GLMC©°ľf`qpP
    
    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();
            }
        }
    }
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
          throws IOException, ServletException
    {
        // read the parameter that got passed
        String inputxml = request.getParameter("inputxml");
        response.setContentType("text/plain"); 
        PrintWriter out = response.getWriter();
        try {
           String s = "Servlet response";
           out.println(s);
           out.println("parameter value = " + inputxml);
        } finally {
           out.close();
        }
    }