This section of CRUD with Serial Persistence includes
the code for the rest of the package. This section
only makes calls to the DAO so basicly it has not
changed. But I have corrected some minor discrepancies.
So please be careful, they are the last version of
Simple CRuD Tutorial.
web.xml
-------
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Simple CRUD of ARS</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>new</servlet-name>
<servlet-class>servletPackage.createServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>readnext</servlet-name>
<servlet-class>servletPackage.readNextServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>readprev</servlet-name>
<servlet-class>servletPackage.readPrevServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>save</servlet-name>
<servlet-class>servletPackage.updateServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>delete</servlet-name>
<servlet-class>servletPackage.deleteServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>list</servlet-name>
<servlet-class>servletPackage.listServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>selectlist</servlet-name>
<servlet-class>servletPackage.selectListServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>new</servlet-name>
<url-pattern>/new</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>readnext</servlet-name>
<url-pattern>/readnext</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>readprev</servlet-name>
<url-pattern>/readprev</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>save</servlet-name>
<url-pattern>/save</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>delete</servlet-name>
<url-pattern>/delete</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>list</servlet-name>
<url-pattern>/list</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>selectlist</servlet-name>
<url-pattern>/selectlist</url-pattern>
</servlet-mapping>
</web-app>
index.jsp
---------
<%--
Document : index
Created on : 15.Oca.2011, 18:23:21
Author : Ali Riza SARAL
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="mainPackage.*" %>
<%@ page import="daoPackage.*" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ARS's Simple CRUD</title>
<%
System.out.println("index.jsp JSP script began here.");
PersonDAOVectorImpl personDAO;
String nameVal="", lastVal="", hobbyVal="", idVal="";
if (session.getAttribute("personDAOsess") == null){
System.out.println("index.jsp personDAOsess == null ");
personDAO = new PersonDAOVectorImpl();
session.setAttribute("personDAOsess", personDAO);
Person person = personDAO.readRec(1);
nameVal= person.getName();
lastVal= person.getLast();
hobbyVal= person.getHobby();
idVal= String.valueOf(person.getId());
}
else {
System.out.println("index.jsp personDAOsess!!!!=null");
personDAO = (PersonDAOVectorImpl) session.getAttribute("personDAOsess");
nameVal = (String) request.getAttribute("nameReq");
lastVal = (String) request.getAttribute("lastReq");
hobbyVal = (String) request.getAttribute("hobbyReq");
idVal = (String) request.getAttribute("idReq");
}
%>
<SCRIPT>
function submitFunction(i) {
if (i==1) document.theDetail.action= "./new";
if (i==2) document.theDetail.action= "./save";
if (i==3) document.theDetail.action= "./delete";
if (i==4) document.theDetail.action= "./readnext";
if (i==5) document.theDetail.action= "./readprev";
if (i==6) document.theDetail.action= "./list";
document.theDetail.submit()
}
function InitScr() {
// alert ("Merhaba from Ali R+ with best wishes! ");
}
</SCRIPT>
</head>
<body style="background-color:yellow" onLoad="InitScr()">
<h1>The Simplest CRUD Application with JAVA</h1>
<br />
<form name="theDetail" action="" method="get">
<input type="hidden" name="id" value="<%= idVal%>"/><br />
First name: <input type="text" id ="nameId" name="name" value="<%= nameVal%>"/><br />
Last name : <input type="text" name="last" value="<%= lastVal%>"/><br />
Hobby : <input type="text" name="hobby" value="<%= hobbyVal%>"/><br />
<br />
<INPUT TYPE="button" VALUE="New" onClick="submitFunction(1)">
<INPUT TYPE="button" VALUE="Save" onClick="submitFunction(2)">
<INPUT TYPE="button" VALUE="Delete" onClick="submitFunction(3)">
<INPUT TYPE="button" VALUE="Next" onClick="submitFunction(4)">
<INPUT TYPE="button" VALUE="Prev" onClick="submitFunction(5)">
<INPUT TYPE="button" VALUE="List" onClick="submitFunction(6)">
</form>
</body>
</html>
tableARS.css
------------
table
{
position:fixed;
top:50px;
left:50px;
}
table, td, th
{
border:1px groove #ccccff ;
border-collapse:collapse;
}
th
{
background-color:#E3F6CE;
color: #ff9999;
}
td
{
background-color: whitesmoke;
color: black;
}
selectionList.jsp
-----------------
<%--
Document : list2
Created on : 19.Oca.2011, 20:15:28
Author : Ali Riza SARAL
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="mainPackage.*" %>
<%@ page import="daoPackage.*" %>
<html>
<head>
<style type="text/css">
span.title{
background-color: white;
color: #2470B3;
font-size: larger;
padding-top:5px;
padding-bottom:2px;
}
</style>
<link rel="stylesheet" type="text/css" href="selectionList.css" />
<%
PersonDAOVectorImpl personDAO;
personDAO = (PersonDAOVectorImpl) session.getAttribute("personDAOsess");
int id = personDAO.getPersonCurrentId();
System.out.println("ARSSSSsssss selectionList.jsp: id="+id);
%>
<script type='text/javascript'>
var currentRow=-1;
function SelectRow(newRow)
{
for(var j=1;j<5;++j)
{
var cell=document.getElementById('cell_'+newRow+','+j);
cell.style.background='#A3D0F7';
if(currentRow!=-1)
{
var cell=document.getElementById('cell_'+currentRow+','+j);
c = currentRow%2 == 0 ? 1 : 0;
if (c == 1) cell.style.background='e7e7e7';
else cell.style.background='whitesmoke';
}
}
currentRow=newRow;
document.getElementById('curRow').value=(currentRow-1)
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Ali R+ Select Table Row</title>
</head>
<body>
<form name="theList" action="./selectlist" method="get">
<%
try {
Person person = personDAO.readRec(1);
// throws an exception if empty.
%>
<TABLE WIDTH="40%">
<TBODY>
<tr align="center">
<span class="title">Some of Ali R+'s Friends </span>
</tr>
<tr>
<th>Person Id</th>
<th>First Name</th>
<th>Last Name</th>
<th>Hobby</th>
</tr>
<%
for (int i = 1, c = 0;;) { //for all the elements of the data
System.out.println(person.getName());
if (c == 0) {
%>
<TR bgcolor="e7e7e7">
<%
} else {
%>
<TR bgcolor="whitesmoke">
<%
}
%>
<TD onclick='SelectRow(<%=(i+1)%>)' id='cell_<%=i+1%>,1'>
<%=person.getId()%>
</TD>
<TD onclick='SelectRow(<%=(i+1)%>)' id='cell_<%=i+1%>,2'>
<%=person.getName()%>
</TD>
<TD onclick='SelectRow(<%=(i+1)%>)' id='cell_<%=i+1%>,3'>
<%=person.getLast()%>
</TD>
<TD onclick='SelectRow(<%=(i+1)%>)' id='cell_<%=i+1%>,4'>
<%=person.getHobby()%>
</TD>
</TR>
<%
c = c == 0 ? 1 : 0;
i++;
if (i > personDAO.getPersonVect().size()) break;
try {
person = personDAO.readRec(i);
} catch (java.lang.ArrayIndexOutOfBoundsException _e0) {
break;
}
} //for all the elements of the data
%>
</TBODY>
</TABLE>
<%
} catch (java.lang.ArrayIndexOutOfBoundsException _e0) {
%>
<FONT>There is no data. </FONT>
<%
}
%>
<input type="hidden" id ="curRow" name="curRow" value="0"/><br />
<input type="submit"
value="View"
position:fixed
top:5px left:5px >
Please select a row by clicking on it and then click on the View button.
</form>
</body>
</html>
SelectionList.css
-----------------
table
{
position:fixed;
top:100px;
left:50px;
}
table, td, th
{
border:1px groove #ccccff ;
border-collapse:collapse;
}
th
{
background-color:#E3F6CE;
color: #ff9999;
}
td
{
color: black;
}
I have not included the source of static anf dinamic table select examples here.
If you are interested that means you need to study the 'Simple CRUD Tutorial' first.