Jquery 带servlet的GSON

Jquery 带servlet的GSON,jquery,jsf-2,jstl,servlet-3.0,Jquery,Jsf 2,Jstl,Servlet 3.0,我的文件夹结构: @WebServlet(name = "KPItoGSON", urlPatterns = {"/KPItoGSON/*"}) public class KPItoGSON extends HttpServlet { /** * Processes requests for both HTTP * <code>GET</code> and * <code>POST</code> m

我的文件夹结构:

@WebServlet(name = "KPItoGSON", urlPatterns = {"/KPItoGSON/*"})

    public class KPItoGSON extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /*
             * TODO output your page here. You may use following sample code.
             */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet KPItoGSON</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet KPItoGSON at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        Gson gson = new Gson();
        HttpServletRequest req = (HttpServletRequest) request;
        KPIListController klc = (KPIListController) req.getSession().getAttribute("kpilist");
        String json = gson.toJson(klc);
        Logger.getLogger(KPItoGSON.class.getName()).warning("The value is"+klc.getKPI().get(1).getUSTER());
        Logger.getLogger(KPItoGSON.class.getName()).info("The json "+json);


      response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(json);
}
function onButtonClickOrCertainTime(){
     $.get('KPItoGSON', function(responseText) { // 
            alert(responseText);        
        });
}

Servlet:

@WebServlet(name = "KPItoGSON", urlPatterns = {"/KPItoGSON/*"})

    public class KPItoGSON extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /*
             * TODO output your page here. You may use following sample code.
             */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet KPItoGSON</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet KPItoGSON at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        Gson gson = new Gson();
        HttpServletRequest req = (HttpServletRequest) request;
        KPIListController klc = (KPIListController) req.getSession().getAttribute("kpilist");
        String json = gson.toJson(klc);
        Logger.getLogger(KPItoGSON.class.getName()).warning("The value is"+klc.getKPI().get(1).getUSTER());
        Logger.getLogger(KPItoGSON.class.getName()).info("The json "+json);


      response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(json);
}
function onButtonClickOrCertainTime(){
     $.get('KPItoGSON', function(responseText) { // 
            alert(responseText);        
        });
}
JQuery:

@WebServlet(name = "KPItoGSON", urlPatterns = {"/KPItoGSON/*"})

    public class KPItoGSON extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /*
             * TODO output your page here. You may use following sample code.
             */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet KPItoGSON</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet KPItoGSON at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        Gson gson = new Gson();
        HttpServletRequest req = (HttpServletRequest) request;
        KPIListController klc = (KPIListController) req.getSession().getAttribute("kpilist");
        String json = gson.toJson(klc);
        Logger.getLogger(KPItoGSON.class.getName()).warning("The value is"+klc.getKPI().get(1).getUSTER());
        Logger.getLogger(KPItoGSON.class.getName()).info("The json "+json);


      response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(json);
}
function onButtonClickOrCertainTime(){
     $.get('KPItoGSON', function(responseText) { // 
            alert(responseText);        
        });
}
错误:

/GISPages/KPI/KPItoGSON 404 (Not Found) 

我正在尝试BalusC的示例。通过这个示例,我想将新数据从数据库中获取到javascript变量中,并绘制一些图表。我不习惯使用servlet:(.使用jQuery向servlet发送请求有什么问题?因为每次使用函数onButtonCickorCertainTime从数据库中选择按钮或轮询时,我都需要新的数据,而servlet的方式使用GSON和servlet的方式更好还是可能?

您的servlet映射到
/KPItoGSON/*
,您的Web应用程序似乎基于到目前为止提供的信息将部署在上下文根目录上。因此,您的servlet正在侦听
http://localhost:8080/KPItoGSON

您的404错误表明servlet是从
/GISPages/KPI
文件夹中的HTML页面调用的。您在
$.get()
中指定了与servlet URL相对的路径,因此它相对于当前请求URL中的顶部文件夹(浏览器地址栏中显示的URL)。它正在尝试通过URL
调用servlethttp://localhost:8080/GISPages/KPI/KPItoGSON
因此无效。它应该通过URL调用servlet
http://localhost:8080/KPItoGSON

除了将HTML页面向上移动两个文件夹外,您还可以通过在ajax请求URL中向上移动两个文件夹来修复它:

$.get('../../KPItoGSON', function(responseText) {
    alert(responseText);        
});
或者使用域相对URL(以斜杠开头):


顺便说一句,您应该从servlet中删除
processRequest()
方法。您的JSON输出现在由于一段不相关的HTML而格式不正确。

我正在远离jstl,因为它已经被评估为js变量,所以认为它对实时系统没有用处(在不重新加载页面的情况下获取数据)。我可能错了,请纠正我。阅读此文:只是说你可以在需要时用f:ajax刷新的简单隐藏输入文本中存储gson…@Daniel也在尝试使用servlet:)我已经添加了我的文件夹结构。它仍然不起作用。是否有任何链接可供我参考以学习基础知识?哦,
/GISPages
似乎根本不是上下文路径,而是您的公共网络内容的另一个文件夹。因此,您的网络应用程序部署在域根上。请使用
。/../KPItoGSON
/KPItoGS的URL改为在
上。这并不难。这与本地磁盘文件系统路径的工作原理相同。使用/KPItoGSON 404(未找到)和.././KPItoGSON 404(未找到)其他servlet可以工作吗?或者这是您的第一个servlet,您以前从未成功开发过servlet吗?编辑:哦,您的上下文路径似乎是
/MyTestproject
,这肯定也应该在servlet的URL中:)但是其他servlet可以工作吗?您真的理解它们应该如何工作吗?您的web.xml声明为哪个servlet版本?自Servlet 3.0以来,
@WebServlet
课程注释仅起作用。首次使用Servlet进行测试:)。我的url模式是urlPatterns={“/KPItoGSON/*”}所以。。url模式应该是/MyTestproject/KPItoGSON/*。是否有任何完整的例子,我可以工作,看到和理解的基本原则?