Sunday, June 21, 2015

Persons Profile System in JSP and XML

A simple persons profle system that I wrote in JSP and XML that will retrieve the name and age of the person that is being stored in XML file. The code is very short and easy to understand perfect for beginners in Java Servers Pages or JSP programming.

In this program I am using Java EE IDE provided freely by Eclipse.org and Apache Tomcat as my application server. I hope you will find my work useful in learning web programming using Java Server Pages or JSP.

If you have some questions please send me an email at jakerpomperada@gmail.comand jakerpomperada@yahoo.com.

People here in the Philippines can reach me at my mobile number 09173084360.

Thank you very much and Happy Programming.



Sample Program Output


Program Listing


persons.jsp

<%@page import="org.w3c.dom.*, javax.xml.parsers.*" %>
<%
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse("http://localhost:8080/helloworld/person.xml");
%>
<%!
public boolean isTextNode(Node n){
return n.getNodeName().equals("#text");
}
%>

<html>
<title> Persons Profile System in JSP and XML </title>
<head>
    
</head>
<style>
label {
      display: block;
  float: left;
  width : 250px;    
font-size:20px;
}
input, select {
                width: 200px;
                border: 2px solid #000;
                padding: 0;
                margin: 0;
                height: 30px;
                -moz-box-sizing: border-box;
                -webkit-box-sizing: border-box;
                box-sizing: border-box;
            }
            input {
                text-indent: 4px;
            }
</style>

<!--  Code for translating thai language in the web browser -->
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 
  <body bgcolor="lightgreen">
  <div class="container">
 <h3>Persons Profile System in JSP and XML </h3>
</div>
<br><br>
<form method="post" action="persons.jsp">
<label> Select  </label>
<select id="select" class="select" name="filter" style="text-align:center;">
<option value="1"  ${param.filter == '1' ? 'selected' : ''}> 1st Choice</option>
<option value="2"  ${param.filter == '2' ? 'selected' : ''}> 2nd Choice</option>
</select> <br><br>
<input type="submit" id="submit" name="submit" value="OK" title="Click here to select your choice.">
</form> <br>
     

   <%
   
   
   String option = request.getParameter("filter");  
      
    
   if("1".equals(option)){
 
%>  



 <table border='2'>
 <tr>
  <th>NAME</th>
  <th>AGE</th>
  </tr>  
 <%   Element  element = doc.getDocumentElement(); 
NodeList personNodes = element.getChildNodes();     
for (int i=0; i<personNodes.getLength(); i++){
Node emp = personNodes.item(i);
if (isTextNode(emp))
continue;
NodeList NameDOBCity = emp.getChildNodes(); 
%>
<tr>
     <%
for (int j=0; j<NameDOBCity.getLength(); j++ ){
Node node = NameDOBCity.item(j);
if ( isTextNode(node)) 
continue;
%>
<td>     <%= node.getFirstChild().getNodeValue() %></td>
<%
%>
</tr>
<%
}
%>
</table>
<% } %>
</body>   
 
</html>



person.xml


<people>
  <person>
    <name>Joe Tan </name>
    <age>30</age>
  </person>
  <person>
    <name>Ana Maria Chua</name>
    <age>29</age>
  </person>
  <person>
    <name>Ferdinand Lim</name>
    <age>45</age>
  </person>
  <person>
    <name>Raul Smith</name>
    <age>61</age>
  </person>
<person>
    <name>Leslie Paul Adams</name>
    <age>53</age>
  </person>
<person>
    <name>John Ching</name>
    <age>78</age>
  </person>

</people>

No comments:

Post a Comment