Write a SERVLET program which counts how many times a user has visited a web page. If user is visiting the page for the first time, display a welcome message. If the user is revisiting the page, display the number of times visited. (Use Cookie)

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class VisitServlet extends HttpServlet
{
    static int i=1;
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String k=String.valueOf(i);
        Cookie c = new Cookie("visit",k);
        response.addCookie(c);
        int j=Integer.parseInt(c.getValue());
        if(j==1)
        {
            out.println("Welcome");
        }
        else
        {
            out.println("You visited "+i+" times");
        }
                i++;                       
    }
}

Web.xml file(servlet entry)
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
<servlet>
        <servlet-name>VisitServlet</servlet-name>
        <servlet-class>VisitServlet</servlet-class>
    </servlet>
<servlet-mapping>
        <servlet-name>VisitServlet</servlet-name>
        <url-pattern>/servlet/VisitServlet</url-pattern>
    </servlet-mapping>
</web-app>

Comments

  1. Awesome blog. Your articles really impressed for me, because of all information so nice and unique. Java Training in Chennai

    ReplyDelete
    Replies
    1. Write a SERVLET program which counts how many times a user has visited a web page. If user is visiting the page for the first time, display a welcome message. If the user is revisiting the page, display the number of times visited. (Use Cookie)

      python projects for final year students
      Deep Learning Projects for Final Year

      Delete
  2. Useful information.I am actual blessed to read this article.thanks for giving us this advantageous information.I acknowledge this post.and I would like bookmark this post.Thanks
    python training institute in chennai
    python training in Bangalore
    python training in pune

    ReplyDelete
  3. Thank you for allowing me to read it, welcome to the next in a recent article. And thanks for sharing the nice article, keep posting or updating news article.
    Blueprism training in Pune

    Blueprism online training

    Blue Prism Training in Pune

    ReplyDelete
  4. Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.



    Data Science training in Chennai
    Data science training in bangalore
    Data science online training
    Data science training in pune

    ReplyDelete
  5. This is such a great post, and was thinking much the same myself. Another great update.
    java training in omr | oracle training in chennai

    java training in annanagar | java training in chennai

    ReplyDelete
  6. Nice tips. Very innovative... Your post shows all your effort and great experience towards your work Your Information is Great if mastered very well.
    Microsoft Azure online training
    Selenium online training
    Java online training
    Java Script online training
    Share Point online training

    ReplyDelete
  7. All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information, I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates.
    Oracle Training | Online Course | Certification in chennai | Oracle Training | Online Course | Certification in bangalore | Oracle Training | Online Course | Certification in hyderabad | Oracle Training | Online Course | Certification in pune | Oracle Training | Online Course | Certification in coimbatore

    ReplyDelete
  8. I am a beginner and I have some doubts regarding this post. Can you help out?

    ReplyDelete
  9. From where we can get to know about url-pattern in this program

    ReplyDelete
  10. This is a smart blog. I mean it. You have so much knowledge about this issue, and so much passion. You also know how to make people rally behind it, obviously from the responses.
    best data science training in hyderabad

    ReplyDelete
  11. This post is so interactive and informative.keep update more information...
    AWS Training in Velachery
    AWS Training in Chennai

    ReplyDelete
  12. Thanks for sharing this blog with us. Really informative and knowledgeable content to all. Keep up this work in further blogs.
    Data Science Course Training in Hyderabad

    ReplyDelete
  13. Great Blog!!! thanks for sharing this information with us.
    Google Ads Job
    Job in Google Ads

    ReplyDelete
  14. 360DigiTMG offers the best Data Science Courses on market. Enroll now for a bright future.
    best data science courses in chennai

    ReplyDelete

Post a Comment

Popular posts from this blog

Write a java program to create Teacher table(TNo.TName, Sal, Desg) and insert a record in it.

Create a JSP page to accept a number from an user and display it in words: Example: 123 – One Two Three. The output should be in red color.

Write a JSP program to accept the details of Account (ANo, Type, Bal) and store it into database and display it in tabular form. (Use PreparedStatement interface)