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 8239169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:03:28+00:00 2026-06-07T20:03:28+00:00

I am crawling through a servlet site and in almost every doPost I encounter

  • 0

I am crawling through a servlet site and in almost every doPost I encounter code like this :

@Override
protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    // set encoding to UTF-8
    if (request.getCharacterEncoding() == null)
        request.setCharacterEncoding("UTF-8");
    // TODO : this may be needed in doGet too ?????????
    response.setCharacterEncoding("UTF-8");

    // check if session exists
    boolean sessionExists = request.isRequestedSessionIdValid();
    HttpSession session = request.getSession();

    if (!sessionExists)
        session.setMaxInactiveInterval(1000);

    // if session does not exist we create it
    ServletContext context = session.getServletContext();
    Integer numSessions = (Integer) context
            .getAttribute("numberOfSessions");
    if (numSessions == null)
        context.setAttribute("numberOfSessions", 1);
    else if (!sessionExists)
        context.setAttribute("numberOfSessions", ++numSessions);
}

Would it be a good idea to create a BaseController class and move this code there – and should I move it in the init() method or in the doPost() – and then call super.doPost ? Also there are lines like session.setAttribute("photo", photo); in some of the servlets. Would it then be a good idea to have a session field in the BaseController – which if I understand things correctly should be volatile.

I am new to all this.

Thanks

  • 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-07T20:03:30+00:00Added an answer on June 7, 2026 at 8:03 pm

    You can move setting the request character encoding to a helper method and call it from doPost. Also check if your code behaved correctly in case that the encoding is set to something else than utf-8.

    On the other hand, the session stuff is a bit weird. If you want to keep track of the number of sessions, remove all that and use javax.servlet.http.HttpSessionListener. It is more elegant and you will have your code in a single place.

    If you need to keep track of variables that belong to a session, keep using the HttpSession class, do not save them in a field in the controller.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While crawling through sources on the web, I've encountered a lot of boiletplate code
How can I configure my site to allow crawling from well known robots like
I'm very new to Python and just crawling my way through it to accomplish
I have been crawling Google for the past hour for a solution to this
I want to know when google is crawling the site, preferably by sending myself
A web-bot crawling your site and using bandwdith resources. Bots are numerous and for
I'm learning RegEx and site crawling, and have the following question which, if answered,
Google has started crawling my site, but from a temporary domain (beta.mydomain instead of
I'm going through crawling wikipedia using website downloader for windows, i was looking through
I'm new to ruby and rails and walking (crawling?) through the tutorial. the second

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.