Search code examples
javaservletspost-redirect-get

POST-REDIRECT-GET vanishing parameter values


I'm implementing Front Controller, which is a servlet and Command pattern design. The problem I have: after successful POST i'm redirecting user to view appending to the request one parameter. and after processing the request through controller I have the name of the parameter, but not the value.

For example if controller should proceed the next path to go to that was returned by Command:

controller?command=viewFaculty&name=Автоматика и приборостроения

The result path would be something like:

controller?command=viewFaculty&name=

My controller:

  public class FrontController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    private static final Logger LOG = Logger.getLogger(FrontController.class);

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        process(request, response, ActionType.FORWARD);
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        process(request, response, ActionType.REDIRECT);
    }

    private void process(HttpServletRequest request,
            HttpServletResponse response, ActionType actionType)
            throws IOException, ServletException {

        LOG.debug("Start processing in Controller");

        // extract command name from the request
        String commandName = request.getParameter("command");
        LOG.trace("Request parameter: 'command' = " + commandName);

        // obtain command object by its name
        Command command = CommandManager.get(commandName);
        LOG.trace("Obtained 'command' = " + command);

        Map<String, String[]> parameterMap = request.getParameterMap();
        for (Iterator<Entry<String, String[]>> iterator = parameterMap
                .entrySet().iterator(); iterator.hasNext();) {
            Entry<String, String[]> entry = iterator.next();
            System.out.println(entry.getKey() + "=" + Arrays.toString(entry.getValue()));
        }

        // execute command and get forward address
        String path = command.execute(request, response, actionType);

        if (path == null) {
            LOG.trace("Redirect to address = " + path);
            LOG.debug("Controller proccessing finished");
            response.sendRedirect(Path.WELCOME_PAGE);
        } else {
            if (actionType == ActionType.FORWARD) {
                LOG.trace("Forward to address = " + path);
                LOG.debug("Controller proccessing finished");
                RequestDispatcher disp = request.getRequestDispatcher(path);
                disp.forward(request, response);
            } else if (actionType == ActionType.REDIRECT) {
                LOG.trace("Redirect to address = " + path);
                LOG.debug("Controller proccessing finished");
                response.sendRedirect(path);
            }
        }
    }
}

Output for parameterMap iterations:

command=[viewFaculty]
name=[]

I've looked at the same problems and answer to them is using response.encodeRedirectUrl(String) - that didn't help, also this method was deprecated. I'm using Tomcat 7, servlets, jsp, log4j and mysql.


Solution

  • As @xehpuk pointed out I have to encode non-ASCII characters, that's is why I've experinced that issue. So in return statement I should do the following:

    return Path.REDIRECT_TO_FACULTY + URLEncoder.encode(facultyName, "UTF-8");