【问题标题】:Calling servlet doPost method in the javascript在 javascript 中调用 servlet doPost 方法
【发布时间】:2013-05-26 19:37:42
【问题描述】:

我想使用 javascript 代码调用 Servlet doPost() 方法,但我收到 http 405(此 URL 不支持 HTTP 方法 GET)异常。

这是我的 javascript 代码:

  url="RedirectServlet?&FD="+FD+"&TD="+TD+"&actionid="+status+"&usercode="+usercode+"&action=reports"+"";

RedirectServlet.java:

 protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
        {
 if(action.equals("reports")){
        System.out.println("inside reports");

        //Getting values from Reports_arb.jsp
        String Fromdate=request.getParameter("FD");
        String Todate=request.getParameter("TD");
        String status=request.getParameter("actionid");
        String usercode=request.getParameter("usercode");

        //placing given values in a session 

        request.setAttribute("FD", Fromdate);
        request.setAttribute("TD", Todate);
        request.setAttribute("actionid", status);
        request.setAttribute("usercode", usercode);


        //Redirecting to showReport_arb.jsp
        //response.sendRedirect("showReport_arb.jsp");

        request.getRequestDispatcher("showReport_arb.jsp").include(request, response);

    }  
  }  

【问题讨论】:

  • 您似乎正在发出 GET 请求,但服务器不接受该请求。它似乎只接受 POST,因此得名
  • 默认情况下,您的请求是 GET 且 servlet 不接受 GET。我认为您应该在请求标头中更改为 POST。
  • @Ian 感谢您的即时回复。我们如何从 javascript 向 servlet 发出 post 请求
  • 请看,How to use Servlets and Ajax?,我敢肯定,从今往后你再也不用摸不着头脑了。

标签: javascript servlets


【解决方案1】:

看到你URL,你就是sendingdata along with the URL。其中servletget请求。

所以URL 正在尝试访问doGet,但是在servlet 中没有实现doGet 会导致问题。

编辑

使用它来访问您的 servlet doPost

<form ...   method="post">...</form>

【讨论】:

  • 我想在重定向 jsp 中隐藏 url 数据(在 showreports_arb.jsp 上方),这就是我在我的 servlet 中使用 doPost() 方法的原因
猜你喜欢
  • 2016-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-18
  • 2014-09-27
  • 2012-06-12
  • 2012-02-27
  • 2011-02-01
相关资源
最近更新 更多