第一次發佈在這裏,我希望它有一個有效的問題。我一直在構建一個基本的Java Servlet,它接受來自頁面表單的3個名稱/值對,將它們設置爲1.請求屬性2.會話屬性和3. Cookie屬性。然後將Cookie添加到響應中,然後轉發視圖(AccountSettings.jsp)。然後AccountSettings頁面應該使用request.getCookies()將它們轉儲到數組中,然後從數組中讀取值。所有這一切都應該發生在我每次使用這種形式時。在JSP中沒有正確讀取Cookies
我的問題是,Cookie值只糾正我第一次使用的形式,那麼每次我再使用形態,餅乾顯示了頁面加載最後輸入的值。但是,如果我刷新頁面,cookie值將正確顯示。我嘗試手動刪除註銷servlet中的cookie(setMaxAge(0),然後重新添加到響應中),但這隻在索引1處產生了一個常量ArrayOutOfBoundsException,因此我評論了該部分,並保留了Cookie。
我查了一下,在本地主機的Chrome相關的cookie顯示頁面後,值是否設置正確,所以像之前的cookie被實際設置正確繪製的JSP,在我看來。
關於如何解決此問題的任何幫助將不勝感激。這是我的代碼。
的Servlet:
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;
public Login() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
login(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
login(request, response);
}
private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
// get a new or existing session
HttpSession session = request.getSession();
// Instantiate user and populate values
User user = new User();
user.setUser(request.getParameter("user"));
user.setPass(request.getParameter("pass"));
// Get last page
String referringUrl = request.getParameter("referringPage");
session.setAttribute("user", user.getUser());
session.setAttribute("pass", user.getPass());
session.setAttribute("lastPage", referringUrl);
Cookie cookie1 = new Cookie("user", user.getUser());
Cookie cookie2 = new Cookie("pass", user.getPass());
Cookie cookie3 = new Cookie("lastPage", referringUrl);
response.addCookie(cookie1);
response.addCookie(cookie2);
response.addCookie(cookie3);
request.setAttribute("user", user.getUser());
request.setAttribute("pass", user.getPass());
request.setAttribute("lastPage", referringUrl);
try{
if (user.authorize()){
session.setAttribute("name", user.getName());
session.setAttribute("authorized", "1");
}else{
session.setAttribute("authorized", "0");
}
}
catch(Exception e){
e.printStackTrace();
}
RequestDispatcher view = request.getRequestDispatcher("AccountSettings.jsp");
view.forward(request, response);
user.destroy();
}
}
查看:
<div id="content">
<div class="padding">
<%
if (!loggedIn){
out.print(
"Oops! I'm not sure how you got here..."
);
}else{
Cookie[] cookies = request.getCookies();
out.print(
"<h2>Account Settings</h2><br><br>" +
"<table>" +
"<tr>" +
"<th>Source</th>" +
"<th>Username</th>" +
"<th>Password</th>" +
"<th>Last Page Visted</th>" +
"</tr>" +
"<tr>" +
"<th>Request</th>" +
"<td>" + request.getAttribute("user") + "</td>" +
"<td>" + request.getAttribute("pass") + "</td>" +
"<td>" + request.getAttribute("lastPage") + "</td>" +
"</tr>" +
"<tr>" +
"<th>Session</th>" +
"<td>" + session.getAttribute("user") + "</td>" +
"<td>" + session.getAttribute("pass") + "</td>" +
"<td>" + session.getAttribute("lastPage") + "</td>" +
"</tr>" +
"<tr>" +
"<th>Cookies</th>" +
"<td>" + cookies[1].getValue() + "</td>" +
"<td>" + cookies[2].getValue() + "</td>" +
"<td>" + cookies[3].getValue() + "</td>" +
"</tr>" +
"</table>"
);
}
%>
</div>
</div>
謝謝你的澄清,我想這可能是這種情況。就安全性方面而言,我瞭解它是一個非常糟糕的主意,足以說我目前是一名學生,這個例子僅用於演示目的,僅用於家庭作業。 – SRTie4k
當然。希望這也可以由老師解釋。別客氣。 – BalusC