• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

date check format with jsp and Action class- what's wrong?

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to do following:
get the date format by:
SimpleDateFormat sdf = (SimpleDateFormat)SimpleDateFormat.getDateInstance(DateFormat.SHORT, request.getLocale());
String myFormat = sdf.toLocalizedPattern();
System.out.println("myFormat inside Action is:"+ myFormat);

it gives me the format suppose:M/d/yy

I do this in jsp.
Now I need to get the format got on jsp into my action class. How do i access it in my Action class?

If I say:
request.setAttribute("myDateFormat_fromReq",myDateFormat_fromReq);
on my jsp and say:
tring myDateFormat_fromReq1 = (String)request.getAttribute("myDateFormat_fromReq");
in my Action class, it gives me null for this value got from the request.

What's wrong? any suggestions?
 
Ranch Hand
Posts: 354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i believe you need to pass the param into your action via either as a session object, a part of a form object or a link parameter. i may be wrong here, but here is my understanding of the request object. on a jsp, when you say request.setAttribute(), i believe the request object being set with that value is the CURRENT request (the request upon which you entered the jsp). the next request object (to eventually be passed into your action) is created UPON the submission of the page and associated form/parameters are constructed according.

unless the locale changes from page to page or jsp to jsp, it may be appropriate to simply set it into the session scope from your jsp and access the session object from your action class.
 
Liz Brown
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
thanks for the suggestion.
I tried following to pass frm jsp:
session.setAttribute("myDateFormat_fromSes",myDateFormat_fromSes);
System.out.println("myDateFormat_fromSes set in session on jsp:"+ myDateFormat_fromSes);

How do I access it in Action class?
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Liz,

JSPs are generally processed *after* Action classes, so setting a request attribute in a JSP will never be seen in the Action. Similarly, by setting it in the session, the Action will see the value set by the previous request.

So as Alan says, the Action class is probably a better place to set it. (And the request scope should be fine).

Hope that helps.
 
alan do
Ranch Hand
Posts: 354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can set it from your jsp or in the action class, as long as you're placing it in the session scope; however, scriptlets in jsp is rather undesirable. you can set it using the action class upon which the user first access that the date format is required. (actually, it's even better when you first initialize the user session, e.g home action). to access it in your action, do the following:

HttpSession session = request.getSession();
//assuming that you the object in which the format was stored is SDF
String dateFormat = (String) session.getAttribute("myDateFormat_fromSes");
 
Liz Brown
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks !
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic