通过AJAX调用Servlet的doGet()方法交互的JSP页面 [英] Interacting the JSP page with the Servlet doGet() method through AJAX call

查看:2066
本文介绍了通过AJAX调用Servlet的doGet()方法交互的JSP页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做JAVA的code以下发现,通过AJAX调用来调用从JSP页面的servlet的doGet()方法。

下面是我的AJAX调用..       上午派遣NG-点击角的js捕获点击的文本作为查询字符串,以servlet的doGet()方法。

在我的JSP文件,

  $ scope.requestFunc =功能(clickData){

       VAR urlquerystring = clickData;
       jQuery.ajax({
          键入:GET,
          网址:/图表/ testExecution /+ + urlquerystring,?

         数据类型:HTML,
          成功:函数(respnsedata)
          {
            window.location.assign(respnsedata);
          }
       });
  }
 

在我的servlet的doGet()方法,

 保护无效的doGet(HttpServletRequest的REQ,HttpServletResponse的RESP)抛出了ServletException,IOException异常{

        通信System.err.println(在TestExecutionESO的servlet ..);


        串teamnametextfield = req.getParameter(teamnametextfield);
        的System.out.println(Teamname是..+ teamnametextfield);

        尝试 {
            dcmanager = DataCollectorManager.getInstance();
        }赶上(例外五){
            // TODO自动生成的catch块
            e.printStackTrace();
        }

        串selectedteam = req.getQueryString();

        字符串testexeclistofobjectsjson = NULL;

        如果(selectedteam!= NULL)
        {

            串释放= selectedteam.replace(%20,)的ToString();

        testexecutionobjlist = dcmanager.getRallyDcMgr()gettestExecutionobjlist(释放)。
        }

        GSON GSON =新GSON();
        testexeclistofobjectsjson = gson.toJson(testexecutionobjlist);

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

        。req.getSession()的setAttribute(testexeclistofobjectsjson,testexeclistofobjectsjson);

        resp.sendRedirect(TestExecutionESO.jsp);

}
 

我得到的查询字​​符串perfectly..After的处理,我会做的setAttribute()和重定向到下一个JSP页面..   重定向不能正常工作。

下面是我的错误code,     无法加载资源:净:: ERR_TOO_MANY_REDIRECTS ..

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

请帮我解决这个问题.. 如何做的setAttribute()重定向到下一个JSP页面。??

解决方案

  resp.sendRedirect(TestExecutionESO.jsp);
 

这是在code中的罪魁祸首。当你调用的sendRedirect(),它会发出302响应包含位置头,新资源的URI。当浏览器看到这个标题,它会发出新的URI的新要求。

这一切都非常适用于同步请求,但万一AJAX调用,我们使用XMLHtt prequest,不会处理重定向这么好。

我会建议你使用一个RequestDispatcher,而不是转发到JSP这样

  RequestDispatcher的RD = req.getRequestDispatcher(路径至UR-JSP);
rd.forward(REQ,RES);
 

Am doing the JAVA code found below, to call the servlet doGet() method from JSP page through AJAX call.

Here is my AJAX call.. Am sending the clicked text captured by ng-click of Angular js as a querystring to Servlet's doGet() method .

In my JSP file,

  $scope.requestFunc = function (clickData) {

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

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

In my Servlet's doGet() method,

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");

}   

Am getting the querystring perfectly..After the processing, I will do SetAttribute() and redirect to next JSP page.. Redirect is not working..

Here is my error code, Failed to load resource: net::ERR_TOO_MANY_REDIRECTS..

http://10.112.81.95:9000/Charts/testExecution/TestExecutionESO.jsp.... Failed to load resource: net::ERR_TOO_MANY_REDIRECTS

Please help me resolve the problem.. how to redirect to next JSP page by doing the setAttribute() .??

解决方案

resp.sendRedirect("TestExecutionESO.jsp");

This is the culprit in your code. when you call sendRedirect(), it will issue a 302 response containing the location-header, URI of the new resource. When the browser sees this header, it will issue a new request for that new URI.

All of this works well for a synchronous request but in case of AJAX calls, we use an XMLHttpRequest, which will not handle redirects so well.

I'd suggest you to use a RequestDispatcher instead to forward to the JSP like this

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

这篇关于通过AJAX调用Servlet的doGet()方法交互的JSP页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆