这是一个 java 网络应用程序,在我的 .jsp 文件中:
<form method="post" action="<% out.print(response.encodeRedirectURL(request.getContextPath()+"/target.do")); %>"
accept-charset="utf-8">
<label for="name">
<input type="text" name="name" id="name"/>
</label>
<input type="submit">
</form>
并且 target.do 被映射到一个 servlet,其中 doPost 是:
HttpSession session = httpServletRequest.getSession();
session.setAttribute("sessionName",httpServletRequest.getParameter("name"));
httpServletResponse.sendRedirect(getServletContext().getContextPath()+"/target.jsp");
在 target.jsp 中我只显示名称:
Your Http Session name is:
<%
String sessionName = (String) request.getSession().getAttribute("sessionName");
out.print(sessionName);
%>
但是,当 cookie 被禁用时,这将不起作用。我不想使用 .forward 我需要使用 sendRedirect。
应用程序已部署到 Tomcat。
将名称放在请求属性中是一种可能的解决方案,但我实际上正在寻找 URL 编码,但我无法实现它?
编辑:encodeURL 而不是 encodeRedirectURL 也不起作用。
最佳答案
我会回答我自己的问题:
This worked:
httpServletResponse.sendRedirect(httpServletResponse.encodeRedirectURL(getServletContext().getContextPath()+"/target.jsp"));
并且在 .jsp 文件中编码...部分不是必需的..
关于java - 当 cookie 被禁用时,如何使表单工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24193899/