Wednesday, October 21, 2009

Creating the User Module













Creating the User Module

The user module of the online discussion forum application allows end users to view and post discussions. The user module contains three files: UserPage.jsp, AddDiscussion.jsp, and ViewDiscussion.jsp.




Creating the UserPage.jsp File


The UserPage.jsp file of the application provides links that allow an end user to post a new discussion or view the existing discussion topics.



Listing 7-10 shows the content of the UserPage.jsp file:




Listing 7-10: The UserPage.jsp File







<%@page import="TxtConverter.*"%>
<%
/*Call the constructor of DisplayString by passing the value of query parameter, lang*/
DisplayString ds=new DisplayString((session.getAttribute("lang1")).toString());
%>
<html>
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>User Options</title>
</head>
<body>
<!--Display banner heading using getHeading() method of DisplayString class-->
<center><h1 class="style1"><span class="style1"><font
color="#E24907"><%= ds.getHeading()%></font> </h1></center>
<p>&nbsp;</p>
<!--Display page name using getUserOption() method of DisplayString class-->
<p align="center"><b><font face="Arial" size="5" color="#0000FF"><%=
ds.getUserOption() %> </font></b></p>
<table border="0" width="215" cellspacing="0" cellpadding="0" height="1">
<tr>
<!--Display user ID label using getUser() method of DisplayString class-->
<td width="95" bgcolor="#FFFFFF" height="1"><div align="center"><font
color="#FF0000"><b><%= ds.getUser() %>
</b></font></div></td>
<td width="104" bgcolor="#FFFFFF" height="1">
<div align="left"></div>
<!--Display user ID value from session-->
<div align="center"><font
color="#FF0000"><%=session.getAttribute("userid")%></font></div></td>
</tr>
</table>
<div align="justify">
<table border="0" cellpadding="0" cellspacing="0" width="200" style="position: absolute; left: 653px; top: 128px;
width: 259px" >
<tr>
<!--Display logout hyperlink-->
<td height="18" bgcolor="#FFFFFF"><div align="center"><font
color="#FFFFFF"><b><a href="DoLogout.jsp"><%=
ds.getLogout()%></a></b></font></div></td>
</tr>
</table>
</div>
<p align="center">&nbsp;</p>
<div align="justify">
<table border="0" cellpadding="0" cellspacing="0" width="300" style="position: absolute;
left: 322px; top: 248px; width:
300">
<tr>
<!--Display text for hyperlink to AddDiscussion.jsp using getNewDiscussion() method of
DisplayString class-->
<td bgcolor="#FFFFFF"><div align="center"><i><b><a
href="AddDiscussion.jsp"><%= ds.getNewDiscussion() %>
</a></b></i></div></td>
</tr>
<tr>
<!--Display text for hyperlink to ViewDiscussion.jsp using getViewDiscussion() method of
DisplayString class-->
<td bgcolor="#FFFFFF"><div align="center"><i><b><a
href="ViewDiscussion.jsp"><%= ds.getViewDiscussion()%>
</a></b></i></div></td>
</tr>
</table>
</div>
</body>
</html>















Download this listing.


The above code creates the UserPage.jsp file. This code retrieves the value of the String, lang1, which represents an end user country stored in session, to create an object of the DisplayString class. The code calls the methods of the DisplayString class to provide the user interface. The user interface displays hyperlinks to access the AddDiscussion.jsp and ViewDiscussion.jsp files.



Figure 7-7 shows the user interface of the UserPage JSP in French:






Figure 7-7: User Interface of UserPage JSP in French




Creating the AddDiscussion.jsp File


The AddDiscussion JSP page provides fields that accept end user information about a new discussion. This information is stored in the discussionlist.xml file.



Listing 7-11 shows the content of the AddDiscussion.jsp file:




Listing 7-11: The AddDiscussion.jsp File







<!--Import the necessary packages-->
<%@page import="javax.xml.parsers.*, java.net.*, java.io.*, java.util.*, org.w3c.dom.*"%>
<%@page
import="javax.xml.transform.Transformer, javax.xml.transform.TransformerFactory,javax.xml.
transform.TransformerException"%>
<%@page
import="javax.xml.transform.TransformerConfigurationException,
javax.xml.transform.dom.DOMSource, javax.xml.transform.stream.StreamResult"%>
<%@page import="javax.xml.transform.OutputKeys"%>
<%@page import="TxtConverter.*"%>
<%!String discussionpath;%>
<%
/*Call the constructor of DisplayString by passing the value of query parameter, lang*/
DisplayString ds=new DisplayString((session.getAttribute("lang1")).toString());
%>
<html>
<%
/*Retrieve category, sub category, and discussion content from request object*/
String tempCategory, tempSubCategory,tempDiscussion,tempuser;
tempCategory=request.getParameter("txtCategory");
tempDiscussion=request.getParameter("txtDiscussion");
tempSubCategory=request.getParameter("txtSubCategory");
if(!(tempCategory == null || tempDiscussion == null ||tempSubCategory == null))
{
try{
/*Create a URL object to load properties file*/
URL url=new URL("http://127.0.0.1:8080/seven/path.properties");
/*Open URL connection*/
URLConnection ucon=url.openConnection();
/*Retrieve the discussionlistpath property*/
Properties prop = new Properties();
prop.load(ucon.getInputStream());
discussionpath = prop.getProperty("discussionlistpath");
}
catch(Exception ex)
{
ex.printStackTrace();
}
/*Obtain a DocumentBuilder object by calling the newInstance()method*/
DocumentBuilderFactory docbfact = DocumentBuilderFactory.newInstance();
/*Call the newDocumentBuilder() method to obtain a DocumentBuilder object*/
DocumentBuilder docb = docbfact.newDocumentBuilder();
/*Parse the discussionlist XML document*/
Document doc = docb.parse(new FileInputStream(discussionpath));
/*Obtain Element object that represents the root element of an XML document*/
Element doce = doc.getDocumentElement();
/*Create a new child element*/
Element newe = doc.createElement("query");
/*Obtain user ID from session*/
tempuser=session.getAttribute("userid").toString();
/*Set category, sub category, discussion, and user ID values as attributes to the query Element*/
newe.setAttribute("Category", tempCategory);
newe.setAttribute("Discussion", tempDiscussion);
newe.setAttribute("user",tempuser);
newe.setAttribute("SubCategory", tempSubCategory);
/*Append query Element to root Element*/
doce.appendChild(newe);
/*Obtain a TransformerFactory object*/
TransformerFactory tFactory = TransformerFactory.newInstance();
/*Create a Transformer object*/
Transformer transformer = tFactory.newTransformer();
/*Set output property of Transformer object*/
transformer.setOutputProperty(OutputKeys.INDENT,"yes");
/*Create a DOMSource that acts a transformation source*/
DOMSource source = new DOMSource(doc);
/*Create a StreamResult object that represents a stream to the XML document and acts as the
/*destination of the transformation*/
StreamResult result = new StreamResult(new PrintStream(new
FileOutputStream(discussionpath)));
/*Perform the transformation to transform the document object tree to the XML document*/
transformer.transform(source, result);
}
%>
<!--Display banner heading using resource bundle-->
<Center><H1 class="style1"><font color="#E24907"><%=
ds.getHeading()%></font></H1></Center>
<br><br>
<p align="center"><font size="5"><b>
<!--Display page heading using resource bundle-->
<%= ds.getPostNewQuery() %></b></font>
</p>
<table border="0" width="215" cellspacing="0" cellpadding="0" height="1">
<tr>
<!--Display user ID label using resource bundle-->
<td width="95" bgcolor="#FFFFFF" height="1"><div align="center"><font
color="#FF0000"><b><%= ds.getUser() %>
</b></font></div></td>
<td width="104" bgcolor="#FFFFFF" height="1">
<!--Display user ID value from session-->
<font color="#FF0000"><b>
<%=session.getAttribute("userid")%></b></font></td>
</tr>
</table>
<div align="justify">
<table border="0" cellpadding="0" cellspacing="0" width="200" style="position: absolute;
left: 707px; top: 129px; width: 208px">
<tr>
<!--Display text of hyperlink to DoLogout.jsp using resource bundle-->
<td height="18" bgcolor="#FFFFFF"><font color="#FFFFFF"><b><a
href="DoLogout.jsp"><%= ds.getLogout() %></a></b></font></td>
</tr>
<tr>
<td height="18" bgcolor="#FFFFFF"><b>
<!--Display text of hyperlink to UserPage.jsp using resource bundle-->
<a href="UserPage.jsp">
<%= ds.getGoMain() %></a></b></td>
</tr>
</table>
</div>
<!--Display HTML form to add new discussion-->
<form action = AddDiscussion.jsp method ="post">
<div align="center">
<table width="544" border="1" cellpadding="0" cellspacing="0" bordercolor="#FF0000">
<tr>
<!--Display label for category field using resource bundle-->
<td width="194" bgcolor="#FFFFFF"><div align="center"><%= ds.getCategory()
%></div></td>
<td width="450" bgcolor="#FFFFFF">
<input name="txtCategory" size="30" >
<select name ="txtCategory1" onChange="{form.txtCategory.value=''+this.value+'';}">
<%
java.util.HashMap hmap = new HashMap();
try{
/*Create a URL object to load properties file*/
URL url=new URL("http://127.0.0.1:8080/seven/path.properties");
/*Open URL connection*/
URLConnection ucon=url.openConnection();
/*Retrieve the discussionlistpath property*/
Properties prop = new Properties();
prop.load(ucon.getInputStream());
discussionpath = prop.getProperty("discussionlistpath");
}
catch(Exception ex)
{
ex.printStackTrace();
}
/*Obtain a DocumentBuilder object by calling the newInstance()method*/
DocumentBuilderFactory docbfact = DocumentBuilderFactory.newInstance();
/*Call the newDocumentBuilder() method to obtain a DocumentBuilder object*/
DocumentBuilder docb = docbfact.newDocumentBuilder();
/*Parse the discussionlist XML document*/
Document doc = docb.parse(new FileInputStream(discussionpath));
/*Obtain query elemens of the XML document as a NodeList object*/
NodeList nl1 = doc.getElementsByTagName("query");
/*Iterate through the NodeList object*/
for(int int_1 = 0; int_1 < nl1.getLength(); int_1 ++)
{
/*Obtain the Node object*/
Node n=nl1.item(int_1);
/*Retrieve attributes of the Node objects*/
NamedNodeMap nnp = n.getAttributes();
Object
obj1=hmap.put((nnp.getNamedItem("Category")).getNodeValue(),(nnp.getNamedItem("Category")).getNodeValue());
if(obj1==null)
{
%>
<!--Populate category drop-down list with values of Category attributes-->
<option
value="<%=(nnp.getNamedItem("Category")).getNodeValue()%>"><%=(nnp.getNamedItem
("Category")).getNodeValue()%></option>
<%
}
}
%>
</select>
</td>
</tr>
<tr>
<!--Display label for sub category field using resource bundle-->
<td width="194" bgcolor="#FFFFFF"><div align="center"><%= ds.getSubCategory()
%></div></td>
<td width="450" bgcolor="#FFFFFF">
<input name="txtSubCategory" size="30" >
<select name ="txtSubCategory1"
onChange="{form.txtSubCategory.value=txtSubCategory1.value;}">
<%
hmap = new HashMap();
/*Obtain a DocumentBuilder object by calling the newInstance()method*/
docbfact = DocumentBuilderFactory.newInstance();
/*Call the newDocumentBuilder() method to obtain a DocumentBuilder object*/
docb = docbfact.newDocumentBuilder();
/*Parse the discussionlist XML document*/
doc = docb.parse(new FileInputStream(discussionpath));
/*Obtain query elemens of the XML document as a NodeList object*/
nl1 = doc.getElementsByTagName("query");
/*Iterate through the NodeList object*/
for(int int_1 = 0; int_1 < nl1.getLength(); int_1 ++)
{
/*Obtain the Node object that represents elements of an XML document*/
Node n=nl1.item(int_1);
/*Retrieve attributes of the Node objects*/
NamedNodeMap nnp = n.getAttributes();
Object
obj1=hmap.put((nnp.getNamedItem("SubCategory")).getNodeValue(),(nnp.getNamedItem
("SubCategory")).getNodeValue());
if(obj1==null)
{
%>
<!--Populate category drop-down list with values of SubCategory attributes-->
<option
value="<%=(nnp.getNamedItem("SubCategory")).getNodeValue()%>"><%=(nnp.getNamedItem
("SubCategory")).getNodeValue()%></option>
<%
}
}
%>
</select>
</td>
</tr>
<tr>
<!--Display label for discussion field using resource bundle-->
<td width="194" bgcolor="#FFFFFF"><div align="center"><%= ds.getDiscussion()
%></div></td>
<td width="338" bgcolor="#FFFFFF">
<textarea name="txtDiscussion" rows="4" cols="50"></textarea>
</td>
</tr>
</table>
<p>
<!--Display label for submit button using resource bundle-->
<input type="submit" value=<%= ds.getSubmitQuery() %>>
</p>
</div>
</form>















Download this listing.




The above code creates the AddDiscussion JSP file. The code retrieves the value of the String object, lang1, which is stored in session to create an object of the DisplayString class. The object of the DisplayString class retrieves country-specific data to provide the user interface of the AddDiscussion JSP page. When an end user specifies a topic, sub topic, and content of a discussion, and clicks the Submit button, the code parses the discussionlist.xml file to create an object tree of that file. The code adds a node that corresponds to the discussion in the object tree and transforms the object tree into an XML document. Figure 7-8 shows the user interface of AddDiscussion.jsp in French:






Figure 7-8: User Interface of AddDiscussion.jsp in French




Creating the ViewDiscussion.jsp File


The ViewDiscussion.jsp file displays existing discussions and allows an end user to search for a specific discussion.



Listing 7-12 shows the content of the ViewDiscussion.jsp file:




Listing 7-12: The ViewDiscussion.jsp File







<!--Import the necessary packages-->
<%@page import="javax.xml.parsers.*, java.net.*, java.io.*, java.util.*, org.w3c.dom.*"%>
<%@page
import="javax.xml.transform.Transformer,javax.xml.transform.TransformerFactory, javax.xml.
transform.TransformerException"%>
<%@page
import="javax.xml.transform.TransformerConfigurationException, javax.xml.transform.dom.
DOMSource, javax.xml.transform.stream.StreamResult"%>
<%@page import="javax.xml.transform.OutputKeys"%>
<%@page import="TxtConverter.*"%>
<%!String discussionpath;%>
<%
/*Call the constructor of DisplayString by passing the value of query parameter, lang*/
DisplayString ds=new DisplayString((session.getAttribute("lang1")).toString());
%>
<%
String tempCategory,tempSubCategory;
/*Retrieve values of category and sub category of discussion from request object*/
tempCategory = request.getParameter("txtCategory");
tempSubCategory = request.getParameter("txtSubCategory");
%>
<Center><H1 class="style1"><font color="#E24907"><%=
ds.getHeading()%></font></H1></Center>
<br>
<p align="center"><font size="5"><b>
<%= ds.getQueryList() %> </b></font>
</p>
<table border="0" width="215" cellspacing="0" cellpadding="0" height="1">
<tr>
<td width="95" bgcolor="#FFFFFF" height="1"><div align="center"><font
color="#FF0000"><b><%= ds.getUser() %>
</b></font></div></td>
<td width="104" bgcolor="#FFFFFF" height="1">
<font color="#FF0000"><b>
<%=session.getAttribute("userid")%> </b></font></td>
</tr>
</table>
<div align="justify">
<table border="0" cellpadding="0" cellspacing="0" width="200" style="position: absolute;
left: 713px; top: 115px; width: 207px" >
<tr>
<!---->
<td height="18" bgcolor="#FFFFFF"><font color="#FFFFFF"><b><a
href="DoLogout.jsp"><%= ds.getLogout() %></a></b></font></td>
</tr>
<tr>
<td height="18" bgcolor="#FFFFFF"><b>
<a href="UserPage.jsp">
<%= ds.getGoMain() %></a></b></td>
</tr>
</table>
</div>
<form action = ViewDiscussion.jsp method ="get">
<Center>
<table width="346" border="1" cellpadding="0" cellspacing="0" bordercolor="#FF0000">
<tr>
<td width="179" bgcolor="#FFFFFF"><%= ds.getCategory() %> </td>
<td width="153" bgcolor="#FFFFFF">
<select name="txtCategory" >
<option value=""></option>
<%
HashMap hmap = new HashMap();
try{
/*Create a URL object to load properties file*/
URL url=new URL("http://127.0.0.1:8080/seven/path.properties");
/*Open URL connection*/
URLConnection ucon=url.openConnection();
/*Retrieve the discussionlistpath property*/
Properties prop = new Properties();
prop.load(ucon.getInputStream());
discussionpath = prop.getProperty("discussionlistpath");
}
catch(Exception ex)
{
ex.printStackTrace();
}
/*Obtain a DocumentBuilder object by calling the newInstance()method*/
DocumentBuilderFactory docbfact = DocumentBuilderFactory.newInstance();
/*Call the newDocumentBuilder() method to obtain a DocumentBuilder object*/
DocumentBuilder docb = docbfact.newDocumentBuilder();
/*Parse the discussionlist XML document*/
Document doc = docb.parse(new FileInputStream(discussionpath));
/*Obtain query elemens of the XML document as a NodeList object*/
NodeList nl1 = doc.getElementsByTagName("query");
/*Iterate through the NodeList object*/
for(int int_1 = 0; int_1 < nl1.getLength(); int_1 ++)
{
/*Obtain the Node object*/
Node n=nl1.item(int_1);
/*Retrieve attributes of the Node objects*/
NamedNodeMap nnp = n.getAttributes();
Object obj1 =
hmap.put((nnp.getNamedItem("Category")).getNodeValue(),(nnp.getNamedItem("Category")).
getNodeValue());
if(obj1==null)
{
%>
<!--Populate category drop-down list with values of Category attributes-->
<option><%=(nnp.getNamedItem("Category")).getNodeValue()%></option>
<%
}
}
%>
</select></td>
</tr>
<tr>
<td width="179" height="26" bgcolor="#FFFFFF"><%= ds.getSubCategory() %> </td>
<td width="153" bgcolor="#FFFFFF">
<!--Display sub-category drop-down list-->
<select name="txtSubCategory" >
<option value=""></option>
<%
hmap = new HashMap();
/*Obtain a DocumentBuilder object by calling the newInstance()method*/
docbfact = DocumentBuilderFactory.newInstance();
/*Call the newDocumentBuilder() method to obtain a DocumentBuilder object*/
docb = docbfact.newDocumentBuilder();
/*Parse the discussionlist XML document*/
doc = docb.parse(new FileInputStream(discussionpath));
/*Obtain query elemens of the XML document as a NodeList object*/
nl1 = doc.getElementsByTagName("query");
/*Iterate through the NodeList object*/
for(int int_1 = 0; int_1 < nl1.getLength(); int_1 ++)
{
/*Obtain the Node object that represents elements of an XML document*/
Node n=nl1.item(int_1);
/*Retrieve attributes of the Node objects*/
NamedNodeMap nnp = n.getAttributes();
Object obj1 = hmap.put((nnp.getNamedItem("SubCategory")).getNodeValue(),
(nnp.getNamedItem("SubCategory")).
getNodeValue());
if(obj1==null)
{
%>
<!--Populate category drop-down list with values of SubCategory attributes-->
<option
value="<%=(nnp.getNamedItem("SubCategory")).getNodeValue()%>"><%=
(nnp.getNamedItem("SubCategory")).getNodeValue()%></option>
<%
}
}
%>
</select></td>
</tr>
</table>
<p>
<input type="submit" value= <%= ds.getViewDiscussion() %>">
</p>
</Center>
</form>
<div align="justify">
<table width="300" border="1" cellpadding="0" cellspacing="0" bordercolor="#FF0000"
style="position: absolute; left: 335px; top: 311px; width: 371px;">
<tr bgcolor="#FFFFFF">
<td colspan="3">
<!--Call getExistingDiscussion() method of DisplayString object to display country
specific data-->
<p align="center"><i><font color="#FF0000"><b> <%=
ds.getExistingDiscussion() %> </b></font></i></p>
</td>
</tr>
<tr>
<td width="147" bgcolor="#FFFFFF"><font color="#FF0000"><b>&nbsp;<%=
ds.getCategory() %></b></font></td>
<td width="147" bgcolor="#FFFFFF"><font color="#FF0000"><b><%=
ds.getSubCategory() %></b></font></td>
<td width="147" bgcolor="#FFFFFF"><font color="#FF0000"><b><%=
ds.getDiscussion() %></b></font></td>
</tr>
<%
hmap = new HashMap();
/*Obtain a DocumentBuilder object by calling the newInstance()method*/
docbfact = DocumentBuilderFactory.newInstance();
/*Call the newDocumentBuilder() method to obtain a DocumentBuilder object*/
docb = docbfact.newDocumentBuilder();
/*Parse the discussionlist XML document*/
doc = docb.parse(new FileInputStream(discussionpath));
/*Obtain query elemens of the XML document as a NodeList object*/
nl1 = doc.getElementsByTagName("query");
/*Iterate through the NodeList object*/
for(int int_1 = 0; int_1 < nl1.getLength(); int_1 ++)
{/*If end user does not specifies any search criteria display all discussion data*/
Node n=nl1.item(int_1);
NamedNodeMap nnp = n.getAttributes();
if(tempCategory == null && tempSubCategory == null )
{
%>
<tr>
<td width="147"
bgcolor="#FFFFFF"><%=(nnp.getNamedItem("Category")).getNodeValue()%></td>
<td width="147"
bgcolor="#FFFFFF"><%=(nnp.getNamedItem("SubCategory")).getNodeValue()%></td>
<td width="147"
bgcolor="#FFFFFF"><%=(nnp.getNamedItem("Discussion")).getNodeValue()%></td>
</tr>
<%
}
else
{
if (!(tempCategory.equals("")))
{
if ((nnp.getNamedItem("Category")).getNodeValue().equals(tempCategory))
{
/*If end user specifies a category,match end user category with the node value. For
/* matching categories, display discussion data.*/
%>
<tr>
<td width="147"
bgcolor="#FFFFFF"><%=(nnp.getNamedItem("Category")).getNodeValue()%></td>
<td width="147"
bgcolor="#FFFFFF"><%=(nnp.getNamedItem("SubCategory")).getNodeValue()%></td>
<td width="147"
bgcolor="#FFFFFF"><%=(nnp.getNamedItem("Discussion")).getNodeValue()%></td>
</tr>
<%
}
}
else
{
if (!(tempSubCategory.equals("")))
{
if ((nnp.getNamedItem("SubCategory")).getNodeValue().equals(tempSubCategory))
{
/*If end user specifies both category and sub category,match end user sub
category with the node value. For matching categories, display discussion data.*/
%>
<tr>
<td width="147"
bgcolor="#FFFFFF"><%=(nnp.getNamedItem("Category")).getNodeValue()%></td>
<td width="147"
bgcolor="#FFFFFF"><%=(nnp.getNamedItem("SubCategory")).getNodeValue()%></td>
<td width="147"
bgcolor="#FFFFFF"><%=(nnp.getNamedItem("Discussion")).getNodeValue()%></td>
</tr>
<%
}
}
}
}
}
%>
</table>
</div>















Download this listing.


The above code creates the ViewDiscussion JSP page. The code creates an object of the DisplayString class to display country-specific data. The parse() method of the DocumentBuilder object parses the discussionlist.xml file and retrieves values of the Category and SubCategory attributes. The Topic and Sub Topic drop-down lists display the attribute values. When an end user selects a topic or sub topic to view discussions, the code iterates through the object tree node of the discussionlist XML document to retrieve data of all matching discussions. The code displays the data in the country-specific user interface in a tabular format.



Figure 7-9 shows the user interface provided by the ViewDiscussion JSP page in French:






Figure 7-9: User Interface of the ViewDiscussion JSP Page in French











No comments:

Post a Comment