Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Servlet Cookies

I am rather new to servlets and currently I'm struggling with adding cookies. I believe this is a result of a modular approach i am taking in that I built a header utility class that simply inserts all the header information into the servlet so I want to take the same approach with adding cookies.

I also have a crediential validator class that excepts the user name and password, validates it, then returns a valid/invalid response. Here i believe lies the problem. In the login form that passes the username and password to the credential validator, I have the form action directing to the credential servlet and the form method as a post.

Doing it this way provides a problem if i want to send a value from the form to another servlet, or does it?

The goal of this project, for school, is to create a simple website strictly with servlets then we get to use JSP to ease the pain.

Is there another approach i should be considering? Is it possible to have these classes that perform a variety of functions on forms when the form action and method are utilized?

Thank you for any help and guidance.

Best E

like image 242
E_IN_CLT Avatar asked Mar 03 '12 04:03

E_IN_CLT


People also ask

How cookies are created in servlet?

To make a cookie, create an object of Cookie class and pass a name and its value. To add cookie in response, use addCookie(Cookie) method of HttpServletResponse interface. To fetch the cookie, getCookies() method of Request Interface is used.

What is cookies in servlet?

A cookie is a small piece of information that is persisted between the multiple client requests. A cookie has a name, a single value, and optional attributes such as a comment, path and domain qualifiers, a maximum age, and a version number.

What is the role of cookies in Java?

Creates a cookie, a small amount of information sent by a servlet to a Web browser, saved by the browser, and later sent back to the server. A cookie's value can uniquely identify a client, so cookies are commonly used for session management.

Which code is used for cookies in servlet?

getCookies() returns an array containing all of the Cookie objects the client sent with this request. Q 23 - Which of the following code is used to get names of the attributes in servlet?


2 Answers

You could send requests to a servlet and then forward requests to another servlet if needed.

In your case, after validation, you can store result in an attribute and then transfer control to another servlet. (if that's what you want to do)

 RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/urlToServlet");
 dispatcher.forward(request, response);

And this is how to deal with cookies.

create and send cookies

Cookie userCookie = new Cookie("name", "value");
userCookie.setMaxAge(60*60*24*365); //Store cookie for 1 year
response.addCookie(userCookie);

read cookie from client

String cookieName = "somecookie";
Cookie[] cookies = request.getCookies();
if (cookies != null) 
{
    for(int i=0; i<cookies.length; i++) 
    {
        Cookie cookie = cookies[i];
        if (cookieName.equals(cookie.getName())) 
        {
            doSomethingWith(cookie.getValue());
        }
    }
}
else
{
    //do something else for firsttime visitors 
}   

Are you using cookies for session tracking? If yes, then use HttpSession. Using HttpSession then there is not need to directly involve with cookies for session tracking.

For example, in a simple login page, this is what you do

HttpSession session = request.getSession();
session.setAttribute("username",username);
In other pages,
if(session.getAttribute("username")==null)
{
//forward to login page.
}
like image 177
John Avatar answered Oct 04 '22 17:10

John


You need to use addCookie in HttpServletResponse. I'd suggest having the java doc to hand so that you can see what is available to a servlet. http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponse.html#addCookie(javax.servlet.http.Cookie)

like image 41
Romski Avatar answered Oct 04 '22 16:10

Romski