[go: up one dir, main page]

0% found this document useful (0 votes)
17 views1 page

Slip 6 B

This Java code defines a servlet class that counts the number of visits to a web page using cookies. It checks if a cookie already exists, increments the count if so, or creates a new cookie and sets the count to 1 if not.

Uploaded by

SHUBHAM JAGTAP
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views1 page

Slip 6 B

This Java code defines a servlet class that counts the number of visits to a web page using cookies. It checks if a cookie already exists, increments the count if so, or creates a new cookie and sets the count to 1 if not.

Uploaded by

SHUBHAM JAGTAP
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import java.io.

*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Slip6B extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
try{
response.setContentType("text/html");
PrintWriter pw=response.getWriter();
Cookie ck2;
pw.println("<h1><font color='orange'>welcome...</font></h1>");
Cookie[] ck1=request.getCookies();
if(ck1==null)
{
pw.println("<h1><font color='green'>welcome...</font></h1>");
pw.println("<br>Visited cnt is 1");
ck2=new Cookie("cnt","1");
response.addCookie(ck2);
}
else
{
int cnt=Integer.parseInt(ck1[0].getValue());
cnt++;
pw.println("<br>Visited cnt is"+cnt);
ck2=new Cookie("cnt",Integer.toString(cnt));
response.addCookie(ck2);
}
}catch(IOException e){} catch (NumberFormatException e) {
}
}
}

You might also like