0

My Folder Structure:

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

Error:

/GISPages/KPI/KPItoGSON 404 (Not Found) 

I am trying this example by BalusC.With this example I want to get new data from database into javascript variable and do some charting. I am not used to servlet :(. What is the problem with sending request to servlet using jQuery? Since I want new data each time button or poll using function onButtonClickOrCertainTime from database is using GSON and Servlet way is better or is it possible with jstl?

Community
  • 1
  • 1
kinkajou
  • 3,664
  • 25
  • 75
  • 128
  • I am moving away from jstl because it is evaluated already into js variables so thought it would not be useful for live system (fetching data without page reload ). I may be wrong please correct me.reading this: http://stackoverflow.com/questions/3342984/jstl-in-jsf2-facelets-makes-sense – kinkajou Aug 01 '12 at 05:40
  • just saying that you can store the gson in simple hidden inputtext refreshed with f:ajax when needed... – Daniel Aug 01 '12 at 11:28
  • @Daniel trying to do with servlet too :) – kinkajou Aug 01 '12 at 13:25

1 Answers1

1

Your servlet is mapped on /KPItoGSON/* and your webapp seems based on the information provided so far to be deployed on the context root. Your servlet is thus listening on http://localhost:8080/KPItoGSON.

Your 404 error indicates that the servlet is been invoked from a HTML page in the /GISPages/KPI folder. You've specified a path-relative servlet URL in $.get() so it's relative to the top folder in the current request URL (the URL as you see in browser's address bar). It is trying to invoke the servlet by URL http://localhost:8080/GISPages/KPI/KPItoGSON which is thus invalid. It should have invoked the servlet by URL http://localhost:8080/KPItoGSON.

Apart from moving the HTML page two folders up, you can fix it by going two folders up in the ajax request URL:

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

or by using a domain-relative URL (starting with a leading slash):

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

By the way, you should remove the processRequest() method from your servlet. Your JSON output is now malformed with a piece of irrelevant HTML.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I have added my folder structure. it's still not working. Is there any links that I can refer to learn fundamentals? – kinkajou Aug 01 '12 at 11:22
  • Oh, the `/GISPages` seems to be not the context path at all, but just another folder of your public webcontent. Your webapp is thus deployed on the domain root. Use the URL of `../../KPItoGSON` or `/KPItoGSON` instead. It's not that hard. It's the same principle as how local disk file system paths work. – BalusC Aug 01 '12 at 11:24
  • with /KPItoGSON http://localhost:7070/KPItoGSON 404 (Not Found) and with ../../KPItoGSON http://localhost:7070/MyTestproject/KPItoGSON 404 (Not Found) – kinkajou Aug 01 '12 at 13:07
  • Do other servlets work or is this your first one and has you never successfully developed a servlet before? Edit: oh, your context path seems to be `/MyTestproject`, this should definitely be in the servlet's URL as well :) But do other servlets work and do you really understand how they are supposed to work? What servlet version is your web.xml declared to? The `@WebServlet` annotation of course work since Servlet 3.0 only. – BalusC Aug 01 '12 at 13:09
  • first time testing with servlet :) . My url pattern is urlPatterns = {"/KPItoGSON/*"} so .. the url pattern would be /MyTestproject/KPItoGSON/*. Are there any complete example that I can work with, see and understand the fundamentals? – kinkajou Aug 01 '12 at 13:21
  • The trailing `/*` is just a wildcard so that it can accept URLs like `/MyTestProject/KPItoGSON/foo`, `/MyTestProject/KPItoGSON/foo/bar/etc`, as long as it starts with `/MyTestProject/KPItoGSON`. As to learning servlets, start at http://stackoverflow.com/tags/servlets/info – BalusC Aug 01 '12 at 13:22