Saturday 22 January 2011

Simple CRUD Tutorial - 4

In this issue of Simple CRUD Tutorial I will
explain the calling mechanism of DAO calls
made by the Servlets. The servlets are called
by the buttons on the index.jsp just to remind.

index.jsp creates the PersonDAO object when it
first works. It saves this PersonDAO object
into the session Atribute :
...
session.setAttribute("personDAOsess", personDAO);
...

as can be seen below:

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");
...
...

The Servlets get the personDAO object from the session whenever necessary
and use it as seen in the createServlet.java:

./servletPackage/createServlet.java
-----------------------------------
public class createServlet extends HttpServlet {

public void init()
throws ServletException
{
System.out.println("ARSmsg: createServlet began to work");
}

public void doGet( HttpServletRequest req, HttpServletResponse resp )
throws ServletException, IOException
{
resp.setContentType("text/html");
PersonDAOVectorImpl personDAO;
HttpSession session = req.getSession();
personDAO = (PersonDAOVectorImpl) session.getAttribute("personDAOsess");
...
...

The Servlets do some changes on the database vector and then they have to return
back to the index.jsp screen. They also have to set the prev or new values to
the fields of the index.jsp screen. request attributes are used to pass the
field values:
...
...
req.setAttribute("nameReq", firstName);
req.setAttribute("lastReq", lastName);
req.setAttribute("hobbyReq", hobby);
req.setAttribute("idReq", id);

RequestDispatcher view = getServletContext().getRequestDispatcher("/index.jsp");
view.forward(req,resp);
}
}

When index.jsp is loaded it has to get these request values and set the field values
in the index.jsp:

index.jsp (complete)
--------------------

<%--
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>