如何将输入发送到servlet,而不是从动作发送?

时间:2019-05-31 19:03:51

标签: forms post servlets action

我有一种表单,可以将一些输入发布到一个站点,但是我也想将它们发布到我的servlet-甚至可能吗?

我试图用“提交”按钮做某事,我的意思是用函数执行onclick,但是某些事情不能正常工作

            <input type="submit" value="value1" onclick="afterSubmit()"/>
...some inputs...
        </form>
            form=document.getElementById("${initParam['posturl']}";
            function afterSubmit() {
                form.action="http://localhost:8080/url/servlet";
            }
        </script>

还有我的servlet:

public class sendThis extends HttpServlet {

    public void doPost(HttpServletRequest request,
            HttpServletResponse response)   throws ServletException, IOException {

        PrintWriter out = response.getWriter();
        out.println(request.getParameter("item_name_1"));
    }
}```

So when I click on submit button and then go to localhost:8080/url/servlet, I get this error:

HTTP Status 405 – Method Not Allowed

Type Status Report
Message HTTP method GET is not supported by this URL
Description The method received in the request-line is known by the origin server but not supported by the target resource.

1 个答案:

答案 0 :(得分:0)

doGet 方法添加到servlet并处理请求:

protected void doGet(HttpServletRequest request,
            HttpServletResponse response)   throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    out.println(request.getParameter("item_name_1"));
}