问题
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?
回答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.
来源:https://stackoverflow.com/questions/11753112/gson-with-servlet