Thursday, 17 March 2011

Crud with JQuery Master-Detail 3

The AJAX calls address the Servlets in their url parms. The
servlet calls are defined in the web.xml:

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>init</servlet-name>
<servlet-class>servletPackage.initServlet</servlet-class>
</servlet>
<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>init</servlet-name>
<url-pattern>/init</url-pattern>
</servlet-mapping>
<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>

The sources of the servlets are in the source packages
servletPackage library.

initServlet.java is used to initialize the index.jsp's
form fields at the beginning of the application.

At the beginning of the index.jsp:
...
<%
System.out.println("index.jsp JSP script began here.");
PersonDAOMySQLDBImpl personDAO;

if (session.getAttribute("personDAOsess") == null){
System.out.println("index.jsp personDAOsess == null ");
personDAO = new PersonDAOMySQLDBImpl();
session.setAttribute("personDAOsess", personDAO);
}
else {
System.out.println("index.jsp personDAOsess!!!!=null");
personDAO = (PersonDAOMySQLDBImpl) session.getAttribute("personDAOsess");
System.out.println("personcurrentid="+personDAO.getPersonCurrentId());
}
%>
...

initializes the personDAO and puts it into a session attribute if it already does not exist.

Then initServlet.java works. It gets the current id from the personDAO of which constructor
has set it to 1. Then it:
...
person = personDAO.readRec(personDAO.getPersonCurrentId());
...
and retrieves the related person data. It formats this data into JSON format and
returns it with :
out.println(
"{"
+ " \"id\":" + '"' + id + '"' + ","
+ " \"name\":" + '"' + name + '"' + ","
+ " \"last\":" + '"' + last + '"' + ","
+ " \"hobby\":" + '"' + hobby + '"'
+ "}");

The complete source follows:

initServlet.java:
-----------------
package servletPackage;
/**
*
* @author Ali Riza SARAL
*/
import java.io.*;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import mainPackage.*;
import daoPackage.*;

import util.Escape;

public class initServlet extends HttpServlet {

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

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();

PersonDAOMySQLDBImpl personDAO;
Person person= new Person();
HttpSession session = request.getSession();

try {
personDAO = (PersonDAOMySQLDBImpl) session.getAttribute("personDAOsess");
person = personDAO.readRec(personDAO.getPersonCurrentId());


String id = String.valueOf(person.getId());
String name = person.getName();
String last = person.getLast();
String hobby = person.getHobby();
System.out.println("init id="+personDAO.getPersonNextId());

id = Escape.html(id);
name = Escape.html(name);
last = Escape.html(last);
hobby = Escape.html(hobby);


// for JSON output
id = Escape.javaScript(id);
name = Escape.javaScript(name);
last = Escape.javaScript(last);
hobby = Escape.javaScript(hobby);

out.println(
"{"
+ " \"id\":" + '"' + id + '"' + ","
+ " \"name\":" + '"' + name + '"' + ","
+ " \"last\":" + '"' + last + '"' + ","
+ " \"hobby\":" + '"' + hobby + '"'
+ "}");
//System.out.println("ProcessData out id="+name);
}
catch (Error e) {
System.out.println(e.getMessage());
}
finally {
out.close();
}

}

}