How to stop the servlet?

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 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 and param2 to the validateInput() function to validateInput() input. If the entry is invalid, write a message and complete the process. My question is: how to finish the process? . I know that call return; in doPost() will end this process, but I'm trying to avoid returning any value from validateInput() to doPost() just for the sake of ending the process. I personally think this is more readable, rather than returning true or false in doPost() and calling return; there. Is there any way to do this? Or is it simply impossible?

+4
source share
7 answers

The control always returns to doPost after the validateInput runs, unless you throw an exception (either a non-fixed exception or an IOException in this case).

Therefore, if you do not return the ANY value from validateInput to doPost , even if you commit the response, doPost will still continue to do what it should do. (Of course, if the answer is complete, the browser will not be aware of any further server-side actions).

You will need to return a value, throw an exception (which can doPost can catch) or set a global value that doPost checks (this is just messy).

+4
source
 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 wrong with the input, do this and that } private boolean isValidInput(String param1, String param2){ boolean fail = false; //validate input and return true/false } 
+9
source

A more complicated way would be.

  • validate and setAttributes for the request if an error is detected and the request is redirected to jsp
  • in jsp check the error attribute if any of them displays them

see also

+1
source

What do you call a "process"? This single request? If yes, then sending the message back terminates the servlet. HTTP is a request / response protocol.

If you want to stop the servlet, it is not. The servlet is running in a container that is still ongoing. He is waiting for the next request.

I do not see anything that deserves to be terminated. Validation and rerouting with errors are common.

+1
source

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; } 
+1
source

have you tried one of the following

response.sendRedirect("some view.jsp");

(or)

System.exit(0); // which exits from the program

0
source

Looks like you want to get a link to OutputStream to write HTML back to the client?

You can use response.getOutputStream() , which returns ServletOutputStream .

Once you have written this thread, you need to finish. After that you will need close() and flush() .

Everything that you write to this stream will be sent to the browser. Also remember to call response.setContentType("text/html"); so that the browser knows that it is dealing with HTML.

0
source

Source: https://habr.com/ru/post/1342975/


All Articles