Java 后重定向获取消失参数值
我正在实现前端控制器,这是一个servlet和命令模式设计。我遇到的问题是:成功发布后,我将重定向用户以查看附加到请求的一个参数。在通过控制器处理请求之后,我有参数的名称,但没有值 例如,如果控制器应继续执行命令返回的下一个路径:Java 后重定向获取消失参数值,java,servlets,post-redirect-get,Java,Servlets,Post Redirect Get,我正在实现前端控制器,这是一个servlet和命令模式设计。我遇到的问题是:成功发布后,我将重定向用户以查看附加到请求的一个参数。在通过控制器处理请求之后,我有参数的名称,但没有值 例如,如果控制器应继续执行命令返回的下一个路径: controller?command=viewFaculty&name=Автоматика и приборостроения 结果路径类似于: controller?command=viewFaculty&name= 我的控制器: publ
controller?command=viewFaculty&name=Автоматика и приборостроения
结果路径类似于:
controller?command=viewFaculty&name=
我的控制器:
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);
}
}
}
}
我研究过同样的问题,答案是使用response.encodeRedirectUrlString——这没有帮助,而且这个方法也被弃用了。我使用的是Tomcat 7、servlet、jsp、log4j和mysql。正如@xehpuk所指出的,我必须对非ASCII字符进行编码,这就是我体验到这个问题的原因。因此,在返回声明中,我应该执行以下操作:
return Path.REDIRECT_TO_FACULTY + URLEncoder.encode(facultyName, "UTF-8");
Ааааабббббббббббб1073。您必须使用URL编码。@xehpuk yeap,就是这样
return Path.REDIRECT_TO_FACULTY + URLEncoder.encode(facultyName, "UTF-8");