My Folder Structure:
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?