【问题标题】:GSON with servlet带有 servlet 的 GSON
【发布时间】:2012-08-01 05:38:07
【问题描述】:

我的文件夹结构:

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可能?

【问题讨论】:

  • 我正在远离 jstl,因为它已经被评估为 js 变量,所以认为它对实时系统没有用(在没有页面重新加载的情况下获取数据)。我可能错了,请纠正我。阅读:stackoverflow.com/questions/3342984/…
  • 只是说您可以将 gson 存储在简单的隐藏输入文本中,并在需要时使用 f:ajax 刷新...
  • @Daniel 也在尝试使用 servlet :)

标签: jquery jsf-2 jstl servlet-3.0


【解决方案1】:

您的 servlet 映射在 /KPItoGSON/* 上,并且您的 web 应用程序似乎基于迄今为止提供的信息部署在上下文根上。因此,您的 servlet 正在侦听 http://localhost:8080/KPItoGSON

您的 404 错误表明该 servlet 是从 /GISPages/KPI 文件夹中的 HTML 页面调用的。您在 $.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。

【讨论】:

  • 我已经添加了我的文件夹结构。它仍然无法正常工作。有什么链接可以参考学习基础知识吗?
  • 哦,/GISPages 似乎根本不是上下文路径,而只是您公共网络内容的另一个文件夹。因此,您的 webapp 部署在域根目录上。请改用../../KPItoGSON/KPItoGSON 的URL。这并不难。这与本地磁盘文件系统路径的工作原理相同。
  • 使用 /KPItoGSON localhost:7070/KPItoGSON 404(未找到)和使用 ../../KPItoGSON localhost:7070/MyTestproject/KPItoGSON 404(未找到)
  • 其他 servlet 是否可以工作,或者这是您的第一个 servlet,您以前从未成功开发过 servlet?编辑:哦,你的上下文路径似乎是/MyTestproject,这肯定也应该在 servlet 的 URL 中 :) 但是其他 servlet 是否工作,你真的了解它们应该如何工作吗?您的 web.xml 声明为哪个 servlet 版本? @WebServlet 注释当然只能从 Servlet 3.0 开始工作。
  • 第一次使用 servlet 进行测试 :) 。我的 url 模式是 urlPatterns = {"/KPItoGSON/*"} 所以.. url 模式是 /MyTestproject/KPItoGSON/*。有没有我可以使用、查看和理解基本原理的完整示例?
猜你喜欢
  • 2011-10-24
  • 2020-06-05
  • 2013-11-26
  • 2014-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-28
  • 2014-04-26
相关资源
最近更新 更多