Monday, October 19, 2009

Creating the SearchNewsPage JSP Page













Creating the SearchNewsPage JSP Page

The SearchNewsPage JSP page allows end users to search for news by category, date, or keyword.



Listing 6-8 shows the content of the SearchNewsPage.jsp file:




Listing 6-8: The SearchNewsPage.jsp File







<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Online News</title>
</head>
<body>
<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><font
color="#FFFFFF">Online</font>Online News
</em></strong></font></h2></td>
</tr>
</table>
<p align="center"><b></b></p>
<center>
<table border="1" width="300" cellspacing="0" cellpadding="0" height="1">
<!--Retrieve user ID value from session and display value-->
<tr>
<td width="50%" bgcolor="#FFFFFF" height="1"><div align="center"><font
color="#000080"><b>User Name</b></font></div></td>
<td width="50%" height="1"><div align="center"><font
color="#000080"><b><%=(String)session.getAttribute("username")%></b></
font></div></td>
</tr>
</table>
</center>
<!--Display hyperlink to DoLogout.jsp file-->
<p align="center"><a href="DoLogout.jsp">Logout</a></p>
<!--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" %>
<!--If newslist variable is empty, 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>
<center>
<table border="1" width="47%" height="86">
<!--Display form to search by category and post search values to DoSearchNews.jsp-->
<tr>
<td width="100%" height="30" align="left">
<form method="POST" action="DoSearchNews.jsp">
<!--Display Category drop-down list-->
<p align="center"><b>Category: </b>
<select size="1" name="txtcategory">
<!--Populate drop-down list with news category values-->
<x:forEach var="heading" select="$newslist/newslist/*">
<option value="<x:out select="$heading/category"/>"><x:out
select="$heading/category"/></option>
</x:forEach>
</select><input type="submit" value="Search" name="B1"><input type="reset"
value="Reset" name="B2"></p>
<!--Use hidden field to track query-->
<input type="hidden" name="txtaction" value="bycategory">
</form>
</td>
</tr>
<tr>
<td width="100%" height="19" align="left">
<!--Display form to search by keyword and post search values to DoSearchNews.jsp-->
<form method="POST" action="DoSearchNews.jsp">
<!--Display Keyword field-->
<p align="center"><b>Keyword:</b>
<input type="text" name="txtkeyword" size="22"><input type="submit" value="Search"
name="B1"><input type="reset" value="Reset" name="B2"></p>
<!--Use hidden field to track query-->
<input type="hidden" name="txtaction" value="bykeyword">
</form>
</td>
</tr>
<tr>
<td width="100%" height="19" align="left">
<!--Display form to search by date and post search values to DoSearchNews.jsp-->
<form method="POST" action="DoSearchNews.jsp">
<!--Display Date field-->
<p align="center"><b>Date:</b>
<input type="text" name="txtdate" size="20"><input type="submit" value="Search"
name="B1"><input type="reset" value="Reset" name="B2"></p>
<!--Use hidden field to track query-->
<input type="hidden" name="txtaction" value="bydate">
</form>
</td>
</tr>
</table>
</center>
</body>
</html>















Download this listing.


The above code creates the SearchNewsPage JSP page. The taglib directive includes 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 code displays the Category drop-down list, Keyword text field, and Date text field to allow end users to search for news. The Submit button of the search field submits search information to the DoSearchNews JSP page. The <x:out select="$heading/category"/> tag populates the category drop-down list with a news category retrieved from the newslist XML document.



Figure 6-5 shows the user interface of the SearchNewsPage JSP page:






Figure 6-5: The SearchNewsPage JSP Page










No comments:

Post a Comment