Friday, November 13, 2009

Creating the MainPage JSP Page













Creating the MainPage JSP Page

The MainPage JSP page renders the home page of the online news application. The MainPage JSP page displays the news headlines and the fields to accept the user ID and password of an end-user.



Listing 6-3 shows the content of the MainPage.jsp file:




Listing 6-3: The MainPage.jsp File







<html>
<head>
<title>Online News</title>
</head>
<body>
<div align="center">
<table width="830" height="71" border="0">
<tr>
<td width="816" height="61" bgcolor="#FFFFFF"><h2 align="center"><font
color="#000080" size="5"><strong><em>Online News
</em></strong></font></h2></td>
</tr>
</table>
<p>&nbsp;</p>
<p><em></em></p>
</div>
<p align="center">&nbsp;</p>
<!--Display form to post logon information to DoLogon.jsp-->
<form action="DoLogon.jsp">
<center>
<table border="1" width="300" cellspacing="0" cellpadding="0" height="81">
<!--Display user ID text field-->
<tr>
<td width="50%" bgcolor="#FFFFFF"><div align="center"><font
color="#000080"><b>User ID</b></font></div></td>
<td width="50%"><input name="txtusername"></td>
</tr>
<!--Display password field-->
<tr>
<td width="50%" bgcolor="#FFFFFF"><div align="center"><font
color="#000080"><b>Password</b></font></div></td>
<td width="50%"><input type="password" name="txtpassword" size="20"></td>
</tr>
<tr>
<td width="50%"><div align="center">
<input type="submit" value="Logon" name="B1">
</div></td>
<td width="50%"><input type="reset" value="Reset" name="B2"></td>
</tr>
</table>
</center>
</form>
<p>&nbsp;</p>
<center>
<table width="48%" border="1" cellpadding="0" cellspacing="0" bordercolor="#0000FF">
<tr>
<td width="100%" bgcolor="#FFFFFF"><div align="center"><font
color="#FF0000"><b>Todays headlines</b></font></div></td>
</tr>
<!--Use taglib directive to include JSTL core and XML tags-->
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"
prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml"
prefix="x"%>
<!--Parse newslist.xml file and store result in newslist variable in application scope-->
<c:if test="${empty applicationScope.newslist}" >
<c:import url="http://localhost:8080/news/xml/newslist.xml" var="xml"/>
<x:parse doc="${xml}" var="newslist" scope="application"/>
</c:if>
<!--Retrieve date element value for each news element and store in tempdate variable in request scope-->
<x:forEach var="heading" select="$newslist/newslist/*">
<c:set var="tempdate" scope="request">
<x:out select="$heading/date"/>
</c:set>
<%
/*Retrieve current date in string*/
java.util.Date currdate = new java.util.Date();
String tempdate=""+currdate.getDate()+"-"+(currdate.getMonth()+1)+"-"+(1900+currdate.getYear());
/*Retrieve date stored in request*/
String tempnewsdate=(String)request.getAttribute("tempdate");
if(tempdate.equals(tempnewsdate))
{
/*Compare current date with date retrieved from object. Display headings for matching dates*/
%>
<tr>
<td width="100%"><div align="center"><font color="#000080"><b>
<x:out select="$heading/heading"/></b></font></div></td>
</tr>
<%
}
%>
</x:forEach>
</table>
</center>
</body>
</html>















Download this listing.


The above code creates the MainPage JSP page. The code displays the User ID and Password fields to accept user information. The taglib directives include the JSTL core and XML tag libraries in the JSP page. The <c:import> tag stores the newslist XML document in an xml variable. The <x:parse> tag parses the XML document and stores the result in a newslist variable. The <x:forEach> tag retrieves the date of publication of news and stores the date value in the tempdate variable. The code uses the Date class to retrieve the current date and stores the date value in the tempnewsdate variable. The code compares the date value stored in the tempdate variable with the value stored in the tempnewsdate variable. For matching values, the code uses the <x:out > tag to display the news headlines.



Figure 6-2 shows the user interface of the MainPage JSP page:






Figure 6-2: The MainPage JSP Page










No comments:

Post a Comment