GSON与servlet

时间:2012-08-01 05:38:07

标签: jquery jsf-2 jstl servlet-3.0

我的文件夹结构:

enter image description here

的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);
}

JQuery的:

function onButtonClickOrCertainTime(){
     $.get('KPItoGSON', function(responseText) { // 
            alert(responseText);        
        });
}

错误:

/GISPages/KPI/KPItoGSON 404 (Not Found) 

我正在尝试BalusC的this示例。通过这个示例,我想将数据库中的新数据转换为javascript变量并进行一些制图。我不习惯servlet :(。使用jQuery向servlet发送请求有什么问题?因为我希望每次按钮或使用函数 onButtonClickOrCertainTime 从数据库使用GSON和Servlet方式的新数据是更好还是可以使用jstl

1 个答案:

答案 0 :(得分:1)

您的servlet映射在/KPItoGSON/*上,您的webapp似乎基于目前提供的信息部署在上下文根目录中。因此,您的servlet正在监听http://localhost:8080/KPItoGSON

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

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

$.get('../../KPItoGSON', function(responseText) {
    alert(responseText);        
});

或使用域相对URL(以前导斜杠开头):

$.get('/KPItoGSON', function(responseText) {
    alert(responseText);        
});

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