In the last issue of the Simple CRUD tutorial
I will explain the table select screen.
When the list button is clicked on the index.jsp
The listServlet.java is trigged. listServlet.java
simply opens the selectionList.jsp.
listSrvlet.java
---------------
package servletPackage;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpSession;
import javax.servlet.RequestDispatcher;
import mainPackage.*;
import daoPackage.*;
public class listServlet
extends HttpServlet
{
public void init()
throws ServletException
{
System.out.println("ARSmsg: listServlet began to work");
}
public void doGet( HttpServletRequest req, HttpServletResponse resp )
throws ServletException, IOException
{
RequestDispatcher view = getServletContext().getRequestDispatcher("/selectionList.jsp");
view.forward(req,resp);
}
}
selectionList.jsp is a replica of the second
(dinamicSelectionList.jsp). The most important
difference is: the String array is replaced by
our personDAO and its personVect.
...
...
item = items[i];
...
...
is replaced by
...
...
Person person = personDAO.readRec(i);
...
...
The end of the vector is checked with the:
...
...
if (i > personDAO.getPersonVect().size()) break;
...
...
There is also a mechanism to set the colors of odd vs even
rows differently.
The view button is clicked after selecting a row by the user.
...
...
<form name="theList" action="./selectlist" method="get">
...
...
<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.
...
...
Here the hidden field curRow is set by the:
...
...
<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>
...
...
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;
}
The selectListServlet.java gets the curRow from
selectionList.jsp by:
...
...
int currentRow = Integer.valueOf(req.getParameter("curRow"));
...
...
reads the related person's data from personDAO.
It puts this persons data into the request attributes and
calls the index.jsp:
...
...
RequestDispatcher view = getServletContext().getRequestDispatcher("/index.jsp");
view.forward(req,resp);
...
...
index.jsp checks whether the session has a created personDAO,
if yes it reads the request attributes and sets the related fields.
selectListServlet.java
----------------------
package servletPackage;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpSession;
import javax.servlet.RequestDispatcher;
import mainPackage.*;
import daoPackage.*;
import java.util.Enumeration;
import java.util.Iterator;
public class selectListServlet extends HttpServlet {
public void init()
throws ServletException
{
System.out.println("ARSmsg: selectListServlet began to work");
}
public void doGet( HttpServletRequest req, HttpServletResponse resp )
throws ServletException, IOException
{
resp.setContentType("text/html");
PersonDAOVectorImpl personDAO;
Person person= new Person();
HttpSession session = req.getSession();
for(Enumeration e = req.getAttributeNames(); e.hasMoreElements(); ){
String attName = (String)e.nextElement();
System.out.println(attName);
}
int currentRow = Integer.valueOf(req.getParameter("curRow"));
System.out.println("currentRow=================="+currentRow);
try
{
personDAO = (PersonDAOVectorImpl) session.getAttribute("personDAOsess");
person = personDAO.readRec(currentRow);
req.setAttribute("nameReq", person.getName());
req.setAttribute("lastReq", person.getLast());
req.setAttribute("hobbyReq", person.getHobby());
req.setAttribute("idReq", String.valueOf(person.getId()));
RequestDispatcher view = getServletContext().getRequestDispatcher("/index.jsp");
view.forward(req,resp);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
}
}
}