开发者

cookies in java

开发者 https://www.devze.com 2023-02-09 05:16 出处:网络
I am having problem in java to pass cookie from client to server. In java I know a method \"setProperty\" which pass cookie but it takes parameter \"Cookie name\" and \"Cookie value\" separately.But c

I am having problem in java to pass cookie from client to server. In java I know a method "setProperty" which pass cookie but it takes parameter "Cookie name" and "Cookie value" separately.But cookies are sent in one string. Is it ok to pass cookie in this process?? Most of the cookies a开发者_如何学编程re usually contains not only name , values but also domain and expire date . Is it right to skip those domain and expire date and return the cookie only with name and value ?? I want to log in any cookie site like web browsers.


For a java servlet you use javax.servlet.http.Cookie to create cookies. Even if the constructor accept name and value parameters that doesn't mean that those are the only parameters that you can set.

A cookie object has few setters like setDomain or setMaxAge to define your cookie. (see http://download.oracle.com/javaee/5/api/javax/servlet/http/Cookie.html)

Finally you can add those cookies to the response object.

response.addCookie(c1); (see http://www.java-tips.org/java-ee-tips/java-servlet/how-to-use-cookies-in-a-servlet.html)


A cookie is a token which contains sub information in the form key-value pair,

it is generated by the server and is made available to client So that a client sent it back to the server as part of sub-sequence request. It provides a simple mechanism of maintaining user information between request.

Cookies can be two types-
1) persistent Cookies
2) Non persistent cookies

1) Persistent cookies remain value for multiple session, they are stored in a text file by the browser on the client machine.

2) Nonpersistent cookies remain valid only for a single session. They are stored by the browser delete cache, they are discarded when the browser is closed. By default each cookie non- persistent.

Cookies in Java:

Servlet API provide a class name cookies for represents these(cookies) as objects. Cookies object can be created as follows —

Public cookie(String name, String value);

Commonly used methods of cookies class-

getName() - Public String getName();

is used to obtain value of cookie.

setMaxAge() - Public void setMaxAge(int seconds);

is used to set the value for time of cookie, when valid time is associated to a cookie, the cookie become persistent.

addCookie()- public void addCookie(Cookie ch);

http servlet response is used to send a cookie as part of the response.

getCookies() - Public Cookie[] getCookies();

method of http servlet request is used to received cookies which are send by browser as part of request.

Below java Class is used for set cookie in browser-

public class CookieSetExample extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException{
String name= req.getParameter("name");
Cookie ck = new Cookie("user",name);
ck.setMaxAge(600);
response.addCookie(ck);
res.setContentType("text/html");
PrintWriter out = res.getWriter();
}

Below java Class is used for get cookies from browser-

public class CookieGetExample extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException{
String name= "Gust";
Cookie ck[] = getCookies();
if(Optional.ofNullable(ck).ifPresent())
{
name = ck[0].value();
}
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("");
}

Limitations of Cookie-

1) A cookie can be disabled in the browser, i.e. It is not reliable.

2) Persistent doesn’t differentiate between user and the server.

0

精彩评论

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