Domanda

I am developing a java servlet. I am using glassfish server 4.

End users are sending me information through URL parametars, something like this:

http://myIP:8080/TestProject/TestServlet?param1=test1&param2=test2&param3=test3

I am getting the values from param1, param2 and param3 and i want to write them in my database. If I get a SQL exception while writing the informations in my db i want to throw "500 Internal Server Error" to let them know that i have some technical problems and to resend their request. I want to know is there a default way to do this, set some status, display text ...?

Here is the code:

@WebServlet(urlPatterns = {"/TestServlet"}, initParams = {
    @WebInitParam(name = "param1", value = ""),
    @WebInitParam(name = "param2", value = ""),
    @WebInitParam(name = "param3", value = "")})
public class TestServlet extends HttpServlet {

    String param1;
    String param2;
    String param3;
    boolean dbOK;

    /**
     * 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 {
        //Get parametars from the request
        param1 = request.getParameter("param1");
        param2 = request.getParameter("param2");
        param3 = request.getParameter("param3");
        //Input in db
        dbOK = Database.saveParams(param1,param2,param3);

        //Print response
        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("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>T-Mobile Interface</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1> dbOK=" + dbOK + "</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);
    }

    /**
     * Handles the HTTP
     * <code>POST</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 doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
È stato utile?

Soluzione

You should be able to do that with response.sendError(int)

EDIT: Was meant to say that the parameter is the error code you want to send, so in your case 500.

Altri suggerimenti

AFAIK ServletException is translated to Error 500 as well. But typically you set status on response and redirect (dispatches) to JSP, which contains your custom error page. You can set request attributes to customize JSP content.

req.setAttribute("MY_ERROR", "Database request failed");
resp.setStatus(HttpServletResponse.SC_ERROR);
ServletContext context = getServletContext();
RequestDispatcher dispatcher = context.getRequestDispatcher("/error.jsp");
dispatcher.forward(req, resp);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top