Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8610241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:04:59+00:00 2026-06-12T04:04:59+00:00

This is my servlet that creates the session and stores an accessCount to that

  • 0

This is my servlet that creates the session and stores an accessCount to that current session

@WebServlet("/ShowSession.do")
public class ShowSession extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        HttpSession session = request.getSession();

        String header;
        Integer accessCount  = (Integer)request.getAttribute("accessCount");

        if(accessCount == null){
            accessCount = new Integer(0);
            header = "Welcome New Comer";
        }else{
            header = "Welcome Back!";
            accessCount = new Integer(accessCount.intValue() + 1 );
        }

        //Integer is immutable data structure. so we cannot
        //modify the old one in-place,Instead, you have to
        //allocate a new one and redo setAttribute
        session.setAttribute("accessCount", accessCount);
        session.setAttribute("heading", header);
        RequestDispatcher view = getServletContext().getRequestDispatcher("/showsession.jsp");
        view.forward(request, response);
    }
}

And this is the View that prints out the contents of that session

<body>
    <%
        HttpSession clientSession = request.getSession();
        String heading = (String) clientSession.getAttribute("heading");
        Integer accessCount = (Integer) clientSession
                .getAttribute("accessCount");
    %>
    <h1>
        <center>
            <b><%=heading%></b>
        </center>
    </h1>
    <table>
        <tr>
            <th>Info type</th>
            <th>Value</th>
        <tr>
        <tr>
            <td>ID</td>
            <td><%=clientSession.getId()%></td>
        </tr>
        <tr>
            <td>Creation Time</td>
            <td><%=new Date(clientSession.getCreationTime())%></td>
        </tr>
        <tr>
            <td>Time of last Access</td>
            <td><%=new Date(clientSession.getLastAccessedTime())%></td>
        </tr>
        <tr>
            <td>Number OF Access</td>
            <td><%=accessCount%></td>
        </tr>
    </table>
</body>

THe problem is that even though I already access for a lot of times it still returns me an accessCount == 0,

I am accessing at localhost:8080/someFolderName/ShowSession.do

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:05:00+00:00Added an answer on June 12, 2026 at 4:05 am

    You’re getting your accessCount initial value from the request. You should check it from session.

    //Bad!
    Integer accessCount  = (Integer)request.getAttribute("accessCount");
    //Good (maybe)
    Integer accessCount  = (Integer)session.getAttribute("accessCount");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I create this filter : public class LoginFilter implements Filter { @Override public void
I am maintaining this servlet that has a HttpServletResponse response that replies back to
I have this java servlet that grabs information from a form, I need to
Related to this question , is the idea of a default servlet that serves
I notice that there is getWebApplicationContext in org.springframework.web.servlet.mvc.AbstractController . This means that spring programmers
I have a Java EE 6 servlet that creates an instance of FooBarModelImpl and
OK, here is the problem I have. I have a servlet that dynamically creates
I've this servlet filter servlet called everytime a file from Restricted folder is called.
I have tested this servlet and it works well, except in Google Chrome it
I have this servlet program and an external .jar file (written by someone else)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.