aspose file tools*
The moose likes Servlets and the fly likes Problem in setting the date in a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem in setting the date in a servlet" Watch "Problem in setting the date in a servlet" New topic
Author

Problem in setting the date in a servlet

Komal Amaresh
Ranch Hand

Joined: Oct 06, 2008
Posts: 67
Hi,

I' am working on a servlet for a leave application. To be able to set the date in the jsp and retrieve the date from a servlet, I' am writing the logic in a core java program. I' am trying to parse the date in a core java program to get and set the date besides computing the number of days between the from and to date. In my application, I'am passing the values in the jsp page in text boxes. I'am using mySQL for creating the database.
As it accepts date in
yyyy-MM-dd
format. I want to pass and retrieve the values in
dd-MM-yyyy
format on the jsp page and the web page, and be able to store the values and in the database in its default format.

I' am writing the getDate(), setdate(), and duration() methods in the core java program. I may well write it in the servlet. But, setting the values by passing the method name reduces complexity and length and more OOPs like, I want to write it in a simple java program.

I' am able to do it by using Bufferedreader and calendar classes on the console. But, as I cant use the console but be able to retrieve the values in a servlet from the jsp.

Please help me as how to write the logic for setting and getting a date for use in a servlet .

One more thing, on the jsp page, I have given three buttons - SUBMIT, RESET, CANCEL. On clicking reset, I want to reset the fields to null to be able to re-enter values. But the web page is going blank. How can I reset the values?
Its the first time I' am trying to integerate a servlet with a different program.

Please help me.

Thanking you,
regards,
Komal
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

So basically you've written the logic for parsing the String (from dd-MM-yyyy format) as date and formatting it to yyyy-MM-dd format?? If all that you want is to convert the program from using BufferedReader to using request parameters, then you should convert your methods to use a parameter and call your methods and pass them the request parameters. So if your class looks like this

You can convert this class to something like this

As far as the Reset problem goes, can you show us the code of the reset button. Did you create it as <input type="reset" /> or something else??


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Komal Amaresh
Ranch Hand

Joined: Oct 06, 2008
Posts: 67
Hi Ankit,

Thanks. That is exactly my need, to be able to use request parameters. But that was not what I did.
My code goes this way:


How am I going to use requestparameters in my code.

And in the jsp, the input type i had used is reset.



Help!!!

Thanking you,
regards,
Komal
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Are you basically asking how to access request parameters in a servlet?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61611
    
  67

You are reading from System.in? What's that have to do with servlets? You'll need to explain better what you are trying to accomplish with that.

In web applications, System.in has no meaning.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Komal Amaresh
Ranch Hand

Joined: Oct 06, 2008
Posts: 67
Hi,

Oh! yes, I had infact written a simple code.
Now I have changed it. On the jsp, I' am retreiving the values from text boxes. Therefore, I' am for now entering the values in mySql date format. I have set the data type to date in the table. and computing the values in the following way in the servlet.
My doPost method is as follows

I have a field Total days, in my jsp, wherein the the difference in dates in nomber of days is shown.
I want the field to display the total days immediately once I enter the from and to days. I have to display the number of days in the jsp as per the requirement, but I' am stuck with this.

I have included a function in the code to do it. But using that I can set the value in the database, and not on the jsp.



Please help me. The above wont work on the client side.

Thanking you

regards,
Komal
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18886
    
    8

Komal Amaresh wrote:I have a field Total days, in my jsp, wherein the the difference in dates in nomber of days is shown.
I want the field to display the total days immediately once I enter the from and to days. I have to display the number of days in the jsp as per the requirement, but I' am stuck with this.
Please help me. The above wont work on the client side.


When you say "immediately", exactly what does that mean? And when you are talking about your JSP, are you talking about the JSP which is running on the server and generating HTML, or are you actually talking about the HTML in the browser? It gets very confusing when people mix the two up.

Also the Java code you posted won't work reliably on the server side either, at least not in places which have daylight saving time. But that's the least of your problems. The main problem seems to be describing the requirements clearly.
Komal Amaresh
Ranch Hand

Joined: Oct 06, 2008
Posts: 67
Hi

I' am speaking of the html which I have saved as a jsp page. The values entered in the jsp if correct are saved into the database.
I have written the lodic for retreiving the values as in the servlet.
I have provided a link in the in the index.jsp which opens the Leaveform.jsp wherein I have to pass all the values. The values passed and validated on the client side, on click are communicated to the server.
Therfore I havs privided two text boxes to enter the from and to dates.
On entering these values the total number of days should be calculated and disolayed in the text box named total days.

The client side entries are urgent . please help me

regards
Komal
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

So you're asking how to compute the difference between two dates in JavaScript?
Aditya Keyal
Ranch Hand

Joined: Dec 01, 2008
Posts: 71
What you basically need is a) A client side way to calculate the difference in no of days and display it either on focus out of the text box or on clicking of a button. For that as David suggests what you actually need is a code to compute a difference in date using Javascript which is readily available in the Javascript forum and on Google. Using a client side scripting mechanism will also eliminate the "immediately" requirement.

You do have one more option which is using AJAX. which will allow you to compute the difference between the dates on the server side. You can opt for that also.

2) As far as the storing of data in a DB goes. I think you have more or less the correct code sample where you can simply parse the date entered in a text box and convert it to a sql date.


- Aditya Webservices Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in setting the date in a servlet