How to Display the Day on JSP
JSP technology gives normally static Web pages a touch of dynamic action. Hypertext markup language tags that surround Java program code give you options for collecting user input so you an display information from database records and create Web pages or sections in Web pages that change “automatically” over time. A good way to start learning how to code on Java Server Pages – the formal name for JSP – is by using it to dynamically display a day.
Insert JSP directly into the body of your Web page in a location of your choosing. If you want to write the code as a reusable module, save it with a .jsp extension and add a reference to its location in the code library instead. Either way, surrounding code with the appropriate opening and closing tags <%...%> identify the code as JSP and ensure your program will respond appropriately:
<body> <center> <h1>The Current Day is:</h1> </left> <% …code goes here %> </body>
JSP uses the java.util package to display the day according to your coding instructions. You can invoke the entire package or be more specific by appending java.util with a class name such “Date” or “Calendar.” If you want to display the current day in day, date or time format, use the “Date” class. If you want to display additional details such as the day of the week, month or year, use the “Calendar” class. Either way, include the Java “import” statement in your opening page directives to tell the program which package and class you are using:
<%@ page import="java.io.,java.util.date, javax.servlet." %>
Display a day using standard JSP format or customize the display by adding date parameters – also called arguments – to your code. Using “<% Date date = new Date() %>” will create a display that includes the day of the week, the current month and day, time in military time format, the time zone and current year:
Sun Dec 11 21:46:49 GMT+04:00 2011
If all you want is the current day of the week, day of the month and the year, add formatting parameters to your code to display the day as: Sun Dec 11 2010
Date date = new Date( ); SimpleDateFormat ft = new SimpleDateFormat ("E dd.MM.yyyy");
Putting It All Together
Add an optional descriptive header if you want to display the day as an announcement. Each time the user refreshes the screen or exits and reenters your site the dynamic nature of JSP will cause the information to update. For a display that appears as a two-line announcement use the code:
<%@ page import="java.io.,java.util." %> <%@ page import="javax.servlet.,java.text." %> <html> <head> <title>Current Day</title> </head> <body> <left> <h1>The Current Day is:</h1> </left> <% Date date = new Date( ); SimpleDateFormat ft = new SimpleDateFormat ("E MM.dd.yyyy"); out.print( "<h2 align=\"left\">" + ft.format(date) + "</h2>"); %> </body> </html>
The results will appear as: The Current Day is: Sun Dec 11 2010
Based in Green Bay, Wisc., Jackie Lohrey has been writing professionally since 2009. In addition to writing web content and training manuals for small business clients and nonprofit organizations, including ERA Realtors and the Bay Area Humane Society, Lohrey also works as a finance data analyst for a global business outsourcing company.