Wednesday, October 21, 2009

Creating the Files to Search for Articles













Creating the Files to Search for Articles

All end users can search for articles by specifying the category and keyword criteria. The files of the application that allow an end user to search for articles are:




  • FrmSearchTag.java: Implements the <techdoc:formsearcharticle/>custom tag that displays the form to search for articles.




  • DoSearchArticle.jsp: Parses the articlelist.xml file to retrieve articles and uses the <techdoc:showarticle/>custom tag to display articles.




  • ShowArticleTag.java: Implements the <techdoc:showarticle/> custom tag to display search results.






Creating the FrmSearchTag Tag Handler


The FrmSearchTag tag handler implements the <techdoc:formsearcharticle /> custom tag. This tag handler displays the fields in which an end user can specify the search criteria.



Listing 5-12 shows the content of the FrmSearchTag.java file:




Listing 5-12: The FrmSearchTag.java File







package technicaldocs;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
public class FrmSearchTag extends TagSupport
{
public int doStartTag()
{
try {
/*Retrieve JSPWriter object to print out response*/
JspWriter out = pageContext.getOut();
/*Create HTML form that submits information to DoSearchArticle.jsp*/
out.println("<form method=\"get\" action=\"DoSearchArticle.jsp\">");
out.println("<table border=\"1\" width=\"46%\" cellpadding=\"0\"
cellspacing=\"0\">");
out.println("<tr>");
out.println("<td width=\"100%\" colspan=\"2\" bgcolor=\"#FF00FF\">");
out.println("<p align=\"center\"><b>SEARCH
ARTICLE</b></td>");
out.println("</tr>");
/*Display drop-down list to select category*/
out.println("<tr>");
out.println("<td width=\"45%\" bgcolor=\"#808080\"><b><font
size=\"3\"
color=\"#00FFFF\">Category</font></b></td>");
out.println(" <td width=\"55%\" bgcolor=\"#808080\"><select size=\"1\"
name=\"category\">");
out.println("<option value=\"corejava\">CORE JAVA</option>");
out.println("<option value=\"j2ee\">J2EE</option>");
out.println("<option value=\"vc++\">VC++</option>");
out.println("</select></td>");
out.println("</tr>");
out.println("<tr>");
/*Display field to specify keyword*/
out.println("<td width=\"45%\" bgcolor=\"#808080\"><b><font
size=\"3\"
color=\"#00FFFF\">Keyword</font></b></td>");
out.println("<td width=\"55%\" bgcolor=\"#808080\"><input type=\"text\"
name=\"keyword\" size=\"15\"></td>");
out.println("</tr>");
out.println("<tr>");
/*Display Submit and Reset button*/
out.println("<td width=\"45%\"><input type=\"submit\" value=\"Submit\"
name=\"B1\"></td>");
out.println("<td width=\"55%\"><input type=\"reset\" value=\"Reset\"
name=\"B2\"></td>");
out.println("</tr>");
out.println("</table>");
out.println("</form>");
}
catch(IOException ioe)
{
System.out.println("Error in HeaderTag: " + ioe);
}
return(SKIP_BODY);
}
}















Download this listing.


The above code creates the FrmSearchTag tag handler. This code displays the Category drop-down list and the Keyword text field. The code uses the Submit button to submit the search information to the DoSearchArticle.jsp file.





Creating the DoSearchArticle JSP Page


The DoSearchArticle JSP page accesses the articlelist.xml file to retrieve information about articles, based on the search criteria specified by the end user. The JSP page uses the <techdoc:showarticle/> custom tag to display the retrieved information.



Listing 5-13 shows the content of the DoSearchArticle.jsp file:




Listing 5-13: The DoSearchArticle.jsp File







<!--Import the necessary packages-->
<%@page import="javax.xml.parsers.*, java.util.*, java.net.*, java.io.*, org.w3c.dom.*"%>
<!--Use taglib directive to include technicaldocs-taglib.tld TLD-->
<%@ taglib uri="WEB-INF/tlds/technicaldocs-taglib.tld" prefix="techdoc" %>
<html>
<head>
<title>Article Search Result Page</title>
</head>
<body>
<%!String articlepath;%>
<%
String temprole=(String)session.getAttribute("role");
if("administrator".equals(temprole) || "user".equals(temprole))
{
%>
<%String tempsubheading="View Article";%>
<!--use header custom tag to display banner-->
<techdoc:header subheading="<%=tempsubheading%>"/>
<%
/*Retrieve category and keyword for article to retrieve*/
String tempcategory =request.getParameter("category");
String tempkeyword=request.getParameter("keyword");
/*Check whether category and keyword are specified*/
if(!(tempcategory==null || tempcategory ==null ))
{
try
{
/*Create a URL object to load the properties file*/
URL url=new URL("http://127.0.0.1:8080/fifth/path.properties");
/*Open URL connection*/
URLConnection ucon=url.openConnection();
/*Retrieve the articlelistpath property*/
Properties prop = new Properties();
prop.load(ucon.getInputStream());
articlepath = prop.getProperty("articlelistpath");
}
catch(Exception ex)
{
ex.printStackTrace();
}
/*Create a DocumentBuilderFactory object*/
DocumentBuilderFactory docbfact = DocumentBuilderFactory.newInstance();
/*Call newDocumentBuilder to create a DocumentBuilder object*/
DocumentBuilder docb = docbfact.newDocumentBuilder();
/*Parse the XML document*/
Document doc = docb.parse(new FileInputStream(articlepath));
%>
<div align="justify">
<table border="1" cellpadding="0" cellspacing="0" width="100%" >
<tr>
<td colspan="2" bgcolor="#008080">
<p align="center"><i><font color="#FFFFFF"><b>Article Search
Result</b></font></i></p>
</td>
</tr>
<%
/*Obtain a NodeList object that represents the elements of the XML file with name user*/
NodeList nl1 = doc.getElementsByTagName("article");
/*Iterate through the NodeList object*/
for(int int_1 = 0; int_1 < nl1.getLength(); int_1 ++)
{
Node n=nl1.item(int_1); NamedNodeMap nnp = n.getAttributes();
if((nnp.getNamedItem("category").getNodeValue()).equals(tempcategory))
{
/*Retrieve NamedNodeMap corresponding to article ID attribute*/
String temparticleid=(nnp.getNamedItem("articleid")).getNodeValue();
NodeList nl2 = n.getChildNodes();
Node n2=nl2.item(1);
NamedNodeMap nnp1 = n2.getAttributes();
/*Retrieve article content*/
String temparticle=(nnp1.getNamedItem("text").getNodeValue()).toUpperCase();
if(temparticle.indexOf(tempkeyword.toUpperCase(),0)!=-1)
{%>
<tr>
<td>
<!--use showarticle custom tag to display banner-->
<techdoc:showarticle category="<%=tempcategory%>"
articleid="<%=temparticleid%>" article="<%=temparticle%>"/>
</td>
</tr>
<%
}
}
}
%>
</table>
</div>
<%
}
else
{%>
Please enter something in the Keyword field.
<%
}%>
<p align="center">&nbsp;</p>
<%
if(temprole.equals("administrator"))
{
%>
<p align="center"><a href="AdminPage.jsp">Back to administration
page</a></p>
<%
}
%>
<%
if(temprole.equals("user"))
{
%>
<p align="center"><a href="UserPage.jsp">Back to user page</a></p>
<%
}
}
%>
<techdoc:footer/>
</body>
</html>















Download this listing.


The above code uses the request object to retrieve the category and keyword an end user specifies to search for articles. The parse() method of the DocumentBuilder object parses the articlelist.xml file. The parse() method returns a Document object that represents an object tree of the articlelist document. The code iterates through the nodes of the object tree to retrieve article information that matches the specified search criteria. If an article matches the search criteria, the code uses the <techdoc:showarticle/> custom tag to display that article. The code passes the category, article ID, and article content as attributes of the <techdoc:showarticle/> custom tag.





Creating the ShowArticleTag Tag Handler


The ShowArticleTag tag handler implements the <techdoc:showarticle/> custom tag to display article information. The tag handler obtains information about articles that are passed as tag attributes. This information is displayed in tabular format. The tag handler also displays the Update, Delete, and Rate buttons to update, delete, and rate articles. Listing 5-14 shows the content of the ShowArticleTag.java file:




Listing 5-14: The ShowArticleTag.java File







package technicaldocs;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import javax.servlet.http.*;
import java.io.*;
public class ShowArticleTag extends TagSupport
{
/*Declare tag attributes*/
private String category="";
private String articleid="";
private String article="";
/*Declare setXXX() methods for attributes*/
public void setCategory(String String_1)
{
category=String_1;
}
public void setarticleid(String String_1)
{
articleid=String_1;
}
public void setArticle(String String_1)
{
article=String_1;
}
/*Declare getXXX() methods for attributes*/
public String getCategory()
{
return category;
}
public String getArticleid()
{
return articleid;
}
public String getArticle()
{
return article;
}
public int doStartTag()
{
try
{
/*Obtain JspWriter object*/
JspWriter out = pageContext.getOut();
/*Retrieve HttpSession object*/
HttpSession session = pageContext.getSession();
/*Retrieve role from session*/
String temprole = (String)session.getAttribute("role");
/*Create form and submit to ModifyDeleteRate.jsp*/
out.println("<form method=\"get\" action=\"ModifyDeleteRate.jsp\">");
out.println("<input type=\"hidden\" name=\"task\" />");
out.println("<table border=\"1\" width=\"81%\" cellpadding=\"0\"
cellspacing=\"0\">");
out.println("<tr>");
out.println("<td width=\"28%\" bgcolor=\"#808080\"
colspan=\"2\"><b><font size=\"3\"
color=\"#00FFFF\">Category</font></b></td>");
out.println("<input type=\"hidden\" name=\"category\"
value=\""+getCategory()+"\"/>");
out.println("<td width=\"72%\" bgcolor=\"#808080\"
colspan=\"2\">"+getCategory()+"</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td width=\"28%\" bgcolor=\"#808080\"
colspan=\"2\"><b><font size=\"3\" color=\"#00FFFF\">Article");
out.println("ID</font></b></td>");
out.println("<input type=\"hidden\" name=\"articleid\"
value=\""+getArticleid()+"\"/>");
out.println("<td width=\"72%\" bgcolor=\"#808080\"
colspan=\"2\">"+getArticleid()+"</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td width=\"28%\" bgcolor=\"#808080\"
colspan=\"2\"><b><font size=\"3\"
color=\"#00FFFF\">Article</font></b></td>");
out.println("<td width=\"72%\" bgcolor=\"#808080\" colspan=\"2\"><textarea
rows=\"2\" name=\"article\"
cols=\"36\">"+getArticle()+"</textarea></td>");
out.println("</tr>");
out.println("<tr>");
if(temprole.equals("administrator"))
{
out.println("<td width=\"61%\"><input type=\"button\" value=\"Modify\"
name=\"Modify\"
onClick=\"{this.form.task.value='modify';this.form.submit();}\"></td>");
out.println("<td width=\"62%\" colspan=\"2\"><input type=\"button\"
value=\"Delete\" name=\"delete\"
onClick=\"{this.form.task.value='delete';this.form.submit();}\"></td>");
out.println("<td width=\"1%\">&nbsp;</td>");
}
else if(temprole.equals("user"))
{
out.println("<td width=\"61%\">Rating (1 - 10)<input type=\"text\"
name=\"rating\" /></td>");
out.println("<td width=\"61%\"><input type=\"button\" value=\"Rate\"
name=\"Rate\"
onClick=\"{this.form.task.value='rate';this.form.submit();}\"></td>");
}
out.println("</tr>");
out.println("</table>");
out.println("</form>");
}
catch(IOException ioe)
{
System.out.println("Error in HeaderTag: " + ioe);
return(SKIP_BODY);
}
return(SKIP_BODY);
}
}















Download this listing.


The above code creates the ShowArticleTag tag handler. The category, articleid, and article private fields represent the attributes of the <techdoc:showarticle/> custom tag. The values of the category, articleid, and article private fields can be retrieved and set using accessor and mutator methods. The code also contains the doStartTag() method, which calls the accessor methods to retrieve values that are passed as attributes to the <techdoc:showarticle/> custom tag. The code uses the attribute values to retrieve and print article information. Next, the code obtains the value of an end user role stored in the session object. For end users with administrator role, the code displays the Update and Delete buttons for each article to update or delete an article. When the administrator clicks the Update or Delete button, a request parameter task is sent along with the request to the ModifyDeleteRate JSP page. For end users with user role, the code displays the Rate button.




Figure 5-5 shows the View Articles page of the online article processing application that displays articles to end users with administrator role:






Figure 5-5: The View Articles Page for Administrator Role


Figure 5-6 shows the View Articles page of the online article processing application that displays articles to end users with user role:






Figure 5-6: The View Articles Page for User Role











No comments:

Post a Comment