Try the following:
I changed your check to return a boolean, so you can run the if statement in it in the doPost method. I also made him return success instead of failure, so checking makes more sense after you return.
response.sendError will allow you to create a common error page (see below: http://blogs.atlassian.com/2007/07/responsesenderror_vs_responses/ ) instead of your output. If you want to want setStatus (see Link again).
The return should stop execution and prevent the rest, but it will not disable the servlet, like system.exit (0).
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{ String param1 = request.getParameter("param1"); String param2 = request.getParameter("param2"); if (! validateInput(param1, param2, request, response)){ response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); //Or you can use this instead... //response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); //PrintWriter out = response.getWriter(); //out.write("invalid input"); return; } //if nothing wrong with the input, do this and that } private boolean validateInput(String param1, String param2, HttpServletRequest request, HttpServletResponse response) throws IOException{ boolean success = false; //validate input blah blah blah return success; }
source share