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

  • Home
  • SEARCH
  • 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 5955561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:07:54+00:00 2026-05-22T18:07:54+00:00

I have a Servlet where i use Map<Integer, String> for month, day and year

  • 0

I have a Servlet where i use Map<Integer, String> for month, day and year to get displayed in Select tag with help of JSTL tag.

Like for example in a Servlet Code:

// Storing month in key-value pair
Map<Integer, String> months = new LinkedHashMap<Integer,String>();
months.put(1, new String("Jan"));
months.put(2, new String("Feb"));
months.put(3, new String("Mar"));
months.put(4, new String("Apr"));
...

// Putting month in request scope to be accessed in JSP
request.setAttribute("months", months);

But, I want to write Map data structure code in other Helper class and call it in Servlet so that my Servlet becomes less tedious

Question:

  1. How to delegate to helper class/method?
  2. How to call it in request scope in Servlet?
  • 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-05-22T18:07:54+00:00Added an answer on May 22, 2026 at 6:07 pm

    Simply:

    Map<Integer, String> months = new Helper().months();
    

    Depending on your needs you can implement it also as a static method. Using DI using normal method is more approriate (most of the time).

    There’s no need to hard core the months in the method. SimpleDateFormat can handle this. (It could be a bit nicer using joda time instead of the GregorianCalendar.)

    public static Map<Integer, String> months() {
        Map<Integer, String> months = new HashMap<Integer, String>();
        for(int i=0;i<12;i++){
            GregorianCalendar cal = new GregorianCalendar(2000, i, 1);
            String month = new SimpleDateFormat("MMM", Locale.ENGLISH).format(cal.getTime());
            months.put(i + 1, month);
        }
        return months;
    }
    

    Request scope: Your servlet is called for every request. As long as you’re adding the month as an attribute to the request everything should be fine. There’s actually no need to create the map for every request. Months don’t change that much. You should store the map in the application context at start up.

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

Sidebar

Related Questions

I made a servlet which creates a Map Object: Map<String, Integer> data = new
I have a servlet running in an Oracle OCCAS server. Currently I map some
I have seen some applications that use a servlet to serve the images. What
When you use JSF, you'll have the controller servlet javax.faces.webapp.FacesServlet that will be mapped
I have a Java servlet website, but does not use any frameworks like Spring
I have a servlet that is used for many different actions, used in the
I have a servlet that I would like to run within ColdFusion MX 7.
I have a servlet S which handles callbacks from a 3rd party site. The
In tomcat 6 i have a servlet running openbluedragon, everything compiles and servers up
I have a Java servlet which generates XML, translates it with an XSLT stylesheet,

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.