繁体   English   中英

通过AJAX调用将JSP页面与Servlet doGet()方法进行交互

[英]Interacting the JSP page with the Servlet doGet() method through AJAX call

我正在做下面的Java代码,以通过AJAX调用从JSP页面调用servlet doGet()方法。

这是我的AJAX调用。正在将Angular js的ng-click捕获的单击文本作为查询字符串发送到Servlet的doGet()方法。

在我的JSP文件中

  $scope.requestFunc = function (clickData) {

       var urlquerystring =  clickData; 
       jQuery.ajax({
          type: 'GET',
          url: "/Charts/testExecution/"+"?"+ urlquerystring,

         dataType: 'html',
          success: function(respnsedata)
          {
            window.location.assign(respnsedata);    
          }
       });
  }

在我的Servlet的doGet()方法中,

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.err.println("In TestExecutionESO servlet..");


        String teamnametextfield= req.getParameter("teamnametextfield");
        System.out.println("Teamname is.."+teamnametextfield);

        try {
            dcmanager = DataCollectorManager.getInstance();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String selectedteam= req.getQueryString();

        String testexeclistofobjectsjson = null;

        if(selectedteam!=null)
        {

            String release=selectedteam.replace("%20"," ").toString();

        testexecutionobjlist = dcmanager.getRallyDcMgr().gettestExecutionobjlist(release);
        }

        Gson gson = new Gson();
        testexeclistofobjectsjson = gson.toJson(testexecutionobjlist);

        System.out.println(testexecutionobjlist);
        System.out.println(testexeclistofobjectsjson);

        req.getSession().setAttribute("testexeclistofobjectsjson", testexeclistofobjectsjson);

        resp.sendRedirect("TestExecutionESO.jsp");

}   

正在完美地获取查询字符串。.处理之后,我将执行SetAttribute()并重定向到下一个JSP页面。重定向不起作用。

这是我的错误代码,无法加载资源:net :: ERR_TOO_MANY_REDIRECTS ..

http://10.112.81.95:9000/Charts/testExecution/TestExecutionESO.jsp ....无法加载资源:net :: ERR_TOO_MANY_REDIRECTS

请帮助我解决问题。如何通过执行setAttribute()重定向到下一个JSP页面。

resp.sendRedirect("TestExecutionESO.jsp");

这是代码中的罪魁祸首。 当您调用sendRedirect()时,它将发出302响应,其中包含新资源的位置标头,URI。 当浏览器看到此标头时,它将发出对该新URI的新请求。

所有这些对于同步请求都很好,但是对于AJAX调用,我们使用XMLHttpRequest,它不能很好地处理重定向。

我建议您使用RequestDispatcher代替,这样转发到JSP

RequestDispatcher rd = req.getRequestDispatcher("path-to-ur-jsp");
rd.forward(req,res);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM