The rest of the servlets have the same architectural approach
but differ in functionality. I will expalin the Master-Detail
mechanism and related servlets later.
The complete sources follows:
readNextServlet.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 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();
PersonDAOMySQLDBImpl personDAO;
Person person= new Person();
HttpSession session = request.getSession();
try {
personDAO = (PersonDAOMySQLDBImpl) 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);
}
catch (Error e) {
System.out.println(e.getMessage());
}
finally {
out.close();
}
}
}
readPrevServlet.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 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();
PersonDAOMySQLDBImpl personDAO;
Person person= new Person();
HttpSession session = request.getSession();
try {
personDAO = (PersonDAOMySQLDBImpl) 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();
}
}
}
createServlet.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 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();
PersonDAOMySQLDBImpl personDAO;
HttpSession session = request.getSession();
personDAO = (PersonDAOMySQLDBImpl) session.getAttribute("personDAOsess");
try {
String id = String.valueOf(personDAO.getPersonNextId());
String name = "Pls enter 20 chars";
String last = "Regards. ";
String hobby = "Ali R+";
System.out.println("ProcessData began");
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("createServlet id="+name);
} finally {
out.close();
}
}
}
deleteServlet.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 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();
PersonDAOMySQLDBImpl personDAO;
Person person= new Person();
HttpSession session = request.getSession();
try {
personDAO = (PersonDAOMySQLDBImpl) session.getAttribute("personDAOsess");
personDAO.deleteRec(personDAO.getPersonCurrentId());
System.out.println("ARSSSSSSssCurrentId="+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();
}
}
}
updateServlet.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 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();
PersonDAOMySQLDBImpl 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 = (PersonDAOMySQLDBImpl) 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();
}
}
}