Saturday 22 January 2011

Simple CRUD Tutorial - 2

This is the second item of the Simple CRUD Tutorial. Please
refer to the intro to get a general view of this application
and the terms of use for the code.

The program begins with the index.jsp screen. This is done
by:

WEB-INF/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>
...
...

</web-app>


index.jsp displays a form to get Name, LastName and Hobby.
It also has 6 buttons to new, save, delete, next, prev and list.

index.jsp
---------

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

The index.jsp needs to use more than one submit. submitFunction(i)
is used for this purpose. You can find the submitFunction(i) at

index.jsp
---------
...
...
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ARS's Simple CRUD</title>
...
...
<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>
...
...

The submitFunction(i) makes an action call depending on the value
of its parameter. It does a servlet call by creating an action
according to ./new, ./save, etc.

The definition of the servlets are done in the web.xml.

WEB-INF/web.xml (complete)
---------------

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

Here, servlet-class indicates the place where the class resides.
url-pattern indicates the pattern that has to be entered to the action
in order to activate this Servlet.
/create
servletPackage.createServlet
/readnext
servletPackage.readNextServlet
/readprev
servletPackage.readPrevServlet
/save
servletPackage.updateServlet
/delete
servletPackage.deleteServlet
/list
servletPackage.listServlet
/selectlist
servletPackage.selectListServlet

All the Servlets reside in ./the servletPackage
(Please have a look at the application Library structure
in the first picture of the intro issue).

I have explained the initiation mechanism and the program control
mechanism in this issue along with the complete web.xml. I will
provide the complete index.jsp in the next issues.