--------------
Each AJAX call triggers a corresponding URL of a servlet that 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>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>
Each servlet is defined in the source Packages.servletPackage
createServlet.java
------------------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package servletPackage;
import java.io.*;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import mainPackage.*;
import daoPackage.*;
import util.Escape;
public class createServlet extends HttpServlet {
public void init()
throws ServletException {
System.out.println("ARSmsg: createServlet began to work");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//response.setContentType("text/json;charset=UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
PersonDAOVectorImpl personDAO;
HttpSession session = request.getSession();
personDAO = (PersonDAOVectorImpl) session.getAttribute("personDAOsess");
try {
String id = String.valueOf(personDAO.getPersonNextId());
String name = "Please enter data here";
String last = "Regards. ";
String hobby = "Ali R+";
System.out.println("ProcessData began");
name = name+ " new,";
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);
} finally {
out.close();
}
}
}
deleteServlet.java
------------------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package servletPackage;
import java.io.*;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import mainPackage.*;
import daoPackage.*;
import util.Escape;
public class deleteServlet
extends HttpServlet
{
/**
* Called once at startup
*/
public void init()
throws ServletException
{
System.out.println("ARSmsg: deleteServlet began to work");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
PersonDAOVectorImpl personDAO;
Person person= new Person();
HttpSession session = request.getSession();
try {
personDAO = (PersonDAOVectorImpl) session.getAttribute("personDAOsess");
personDAO.deleteRec(personDAO.getPersonCurrentId());
person = personDAO.readRec(personDAO.getPersonCurrentId());
String id = String.valueOf(person.getId());
String name = person.getName();
String last = person.getLast();
String hobby = person.getHobby();
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);
} finally {
out.close();
}
}
}
listServlet.java
----------------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package servletPackage;
import java.io.*;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import util.Escape;
public class listServlet
extends HttpServlet
{
/**
* Called once at startup
*/
public void init()
throws ServletException
{
System.out.println("ARSmsg: listServlet began to work");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//response.setContentType("text/json;charset=UTF-8");
response.setContentType("text/html;charset=UTF-8");
//response.setContentType("application/json;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
Enumeration e =request.getParameterNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
System.out.println(key);
}
StringBuffer s =request.getRequestURL();
System.out.println(s.toString());
String id = request.getParameter("id");
String name = request.getParameter("name");
String last = request.getParameter("last");
String hobby = request.getParameter("hobby");
System.out.println("ProcessData began");
name = name+ " list,";
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);
} finally {
out.close();
}
}
}
readNextServlet.java
--------------------
package servletPackage;
import java.io.*;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import mainPackage.*;
import daoPackage.*;
import util.Escape;
public class readNextServlet extends HttpServlet {
public void init()
throws ServletException
{
System.out.println("ARSmsg: readNextServlet began to work");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
PersonDAOVectorImpl personDAO;
Person person= new Person();
HttpSession session = request.getSession();
try {
personDAO = (PersonDAOVectorImpl) session.getAttribute("personDAOsess");
person = personDAO.readNextRec();
String id = String.valueOf(person.getId());
String name = person.getName();
String last = person.getLast();
String hobby = person.getHobby();
System.out.println("next 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);
} finally {
out.close();
}
}
}
readPrevServlet.java
--------------------
package servletPackage;
import java.io.*;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import mainPackage.*;
import daoPackage.*;
import util.Escape;
public class readPrevServlet extends HttpServlet {
public void init()
throws ServletException
{
System.out.println("ARSmsg: readPrevServlet began to work");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
PersonDAOVectorImpl personDAO;
Person person= new Person();
HttpSession session = request.getSession();
try {
personDAO = (PersonDAOVectorImpl) session.getAttribute("personDAOsess");
person = personDAO.readPrevRec();
if (person != null){
session.removeAttribute("personDAOsess");
session.setAttribute("personDAOsess", personDAO);
String id=String.valueOf(person.getId());
String name =person.getName();
String last =person.getLast();
String hobby=person.getHobby();
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 + '"'
+ "}");
}
} finally {
out.close();
}
}
}
updateServlet.java
------------------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package servletPackage;
import java.io.*;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import mainPackage.*;
import daoPackage.*;
import util.Escape;
public class updateServlet extends HttpServlet {
public void init()
throws ServletException
{
System.out.println("Init() worked");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
PersonDAOVectorImpl personDAO;
Person person= new Person();
HttpSession session = request.getSession();
try {
String id = request.getParameter("id");
String name = request.getParameter("name");
String last = request.getParameter("last");
String hobby = request.getParameter("hobby");
personDAO = (PersonDAOVectorImpl) session.getAttribute("personDAOsess");
personDAO.updateRec(Integer.valueOf(id), name, last, hobby);
if (person != null){
session.removeAttribute("personDAOsess");
session.setAttribute("personDAOsess", personDAO);
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);
}
} finally {
out.close();
}
}
}