how to stop servlet?
Solution 1
Control will always return to doPost
after validateInput
runs unless you throw an exception (either an unchecked exception or IOException
in this case).
So if you do not return ANY value from validateInput
to doPost
, even if you commit the response, doPost
will still go on to do whatever it is supposed to do. (Of course, if the response is commited, the browser will be entirely unaware of any further activity on the server side).
You will need to return a value, throw an exception (which doPost
may catch) or set a global value that doPost
checks (which is just messy).
Solution 2
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{
String param1 = request.getParameter("param1");
String param2 = request.getParameter("param2");
if(!isValidInput(param1, param2)){
PrintWriter out = response.getWriter();
out.write("invalid input");
return;
}
//if nothing's wrong with the input, do this and that
}
private boolean isValidInput(String param1, String param2){
boolean fail = false;
//validate input and return true/false
}
Solution 3
More sophisticated way would be .
- validate and setAttributes to request if error found and forward request to jsp
- on jsp check for the error attribute if any present display them
See Also
Solution 4
What are you calling "the process"? That single request? If yes, then sending a message back DOES end the servlet's responsibility. HTTP is a request/response protocol.
If you mean stopping the servlet, that's not right. The servlet runs in the container, which is still going. It waits for the next request.
I don't see anything that merits stopping. Validation and routing back with errors is common.
Solution 5
Try this:
I changed your validate to return a boolean, so you can run an if statement on it in the doPost method. I also made it return a success instead of failure, just so the check makes more sense once you've returned.
response.sendError will allow you to do a generic error page (See this: http://blogs.atlassian.com/2007/07/responsesenderror_vs_responses/) instead of your output. If you want that you'll want setStatus (again, see the link).
The return should terminate execution and prevent the rest from happening, but it won't shut down the servlet like a system.exit(0) would.
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's 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;
}
Davuth
Updated on July 12, 2022Comments
-
Davuth almost 2 years
I have a servlet that looks like this:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{ String param1 = request.getParameter("param1"); String param2 = request.getParameter("param2"); validateInput(param1, param2, request, response); //if nothing's wrong with the input, do this and that } private void validateInput(String param1, String param2, HttpServletRequest request, HttpServletResponse response) throws IOException{ boolean fail = false; //validate input blah blah blah if (fail){ PrintWriter out = response.getWriter(); out.write("invalid input"); //end process or servlet } }
The idea is that I want to pass
param1
andparam2
to functionvalidateInput()
to validate whether or not the input is valid. If input is invalid, write a message back and then end the process. My question is how to end the process? I'm aware of that callingreturn;
indoPost()
will end the process but I'm trying to avoid returning any value fromvalidateInput()
todoPost()
just for the sake of ending the process. I personally feel that it's more readable this way rather than returningtrue
orfalse
todoPost()
and callreturn;
there. Is there a way to do it? Or is it simply impossible?