开发者

How can i maintain last cookie value in flex with jsp?

开发者 https://www.devze.com 2023-01-01 09:59 出处:网络
my login form in flex when I login I have created a cookie in jsp like this name setValueCookie.jsp <%@ page language=\"java\" import=\"java.util.* , javax.net.*\" contentType=\"text/html; charset

my login form in flex when I login I have created a cookie in jsp like this name setValueCookie.jsp

<%@ page language="java" import="java.util.* , javax.net.*" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%
    String username = request.getParameter("value");
    System.out.println("Email got in cookieSet = " + username);

    if(username==null) username="";

    Date now = new Date();
    String timestamp = now.toString();
    Cookie cookie = new Cookie("username",username);
    cookie.setMaxAge(365 * 24 * 60 * 60);
    response.addCookie(cookie);

%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>DashBoard-Cookie</开发者_C百科title>
</head>
<body>

</body>
</html>

now using Http service request parameter i am passing username 'Value' to this jsp. and i am reading cookie value from getValueCookie.jsp like this

<%
 String cookieName = "username";
 Cookie cookies [] = request.getCookies ();
 Cookie myCookie = null;
 String result; 
 if (cookies != null)
 {
 for (int i = 0; i < cookies.length; i++) 
 {
    if (cookies [i].getName().equals (cookieName))
    {
    myCookie = cookies[i];
    break;
    }
 }
}

%>
<data>
  <status><%=myCookie.getValue().toString()%></status>
</data>

through the httpservice value i am getting but if i open a new window or any new tab cookie value is not getting how can i solve this? Thanks in advance.


Even though the posted code is pretty cumbersome and nasty (the 2nd example can be done by simply ${cookie.username.value} in EL and scriptlets are no-no, this should be done (in)directly in a HttpServlet or a Filter), the code looks legit.

There springs 2 potential causes to mind:

  1. Cookies are bound to the current request protocol, domain and (by default) the context path. So if you switch by either of them, then the cookie will unaccessible in the other request.

  2. Modifying the response headers (cookies needs to be set in response headers) halfway a JSP file is asking for IllegalStateException: Response already committed exceptions. Check the server logs. If they are there, then just move the scriptlet to top of JSP page or -preferably-, just gently use a HttpServlet or Filter. JSP is the wrong place for this kind of logic.

That said, have you considered using the HttpSession? It provides the same functionality, but then much easier and less problematic since you don't need to hassle with loose cookies. Grab the session by request.getSession() and use session.setAttribute(key, value) to store objects in the session and use session.getAttribute(key) to retrieve objects from session. There the session is for, it is in turn also backed by a cookie, but you don't need to manage it yourself. The server will do it for you transparently.

0

精彩评论

暂无评论...
验证码 换一张
取 消