Search code examples
javastruts

How to return a 401 error response via Struts?


I have a method with a return type ActionForward, and need to return a 401 response in certain situations. How/what can I return from this method to give the desired result?

public ActionForward doExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

    ...

    if (some sort of error situation){
        // need to return a 401 here
    }

    ...
}

Solution

  • response.sendError(HttpServletResponse.SC_UNAUTHORIZED);