Cookie Info Simulation

Enter the information for login..

User Login


Submit


Download Code
package ram;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet1 extends HttpServlet 
{
  public void doGet(HttpServletRequest request, 
    HttpServletResponse response) {
    try{
        response.setContentType("text/html");
        PrintWriter pwriter = response.getWriter();
String name = request.getParameter("userName");
String password = request.getParameter("userPassword");
pwriter.print("Hello:"+name);
pwriter.print("Password is:"+password);
//Creating two cookies
Cookie c1=new Cookie("userName",name);
Cookie c2=new Cookie("userPassword",password);
//Adding the cookies to response header
response.addCookie(c1);
response.addCookie(c2);
pwriter.print("View Cookies Details"); pwriter.close(); }catch(Exception exp){ System.out.println(exp); } } }

User Info Output

Hover for Source code.

View Site Information

Username

Hello
Source Code

JAVA CODE: Cookie c[]=request.getCookies(); pwriter.print("Name: "+c[0].getValue());

Password

Password is:
Source Code

JAVA CODE : pwriter.print("Password: "+c[1].getValue());

Take Quiz


1. How Many types of cookies Are in servlets.?
a. 1         b. 2
c. 3         d. 4

2. Non-persistent cookie are ?
a. valid for single session         b. valid for Multiple session
c. Not valid for session            d. None of the Above

3. Persistent cookie are ?
a. valid for single session         b. valid for Multiple session
c. Not valid for session            d. None of the Above

4. Cookies were originally designed for:
a. Client-side programming         b. Both Client-side & Server-side programming
c. Web programming                 d. Server-side programming

5.Which attribute is used to extend the lifetime of a cookie?
a. higher-age         b. increase-age

c. max-age            d. lifetime