Friday, 28 January 2011

CRUD with Serial Persistence - 5

This part of the CRUD with Serial Persistence Tutorial
include the servlets that are used. There are not any
change related to the seralization here. But I have
made some minor changes to fix some bugs.
HEre is the final code:

createServlet.java
------------------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package servletPackage;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import mainPackage.*;
import daoPackage.*;

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

String id=String.valueOf(personDAO.getPersonNextId());
String firstName ="Please enter data here";
String lastName="Regards. ";
String hobby="Ali R+";
//personDAO.createRec(Integer.valueOf(id), firstName, lastName, hobby);
personDAO.createRec(firstName, lastName, hobby);

session.removeAttribute("personDAOsess");
session.setAttribute("personDAOsess", personDAO);
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);
}
}



deleteServlet.java
------------------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package servletPackage;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import javax.servlet.RequestDispatcher;
import java.io.IOException;
import java.io.PrintWriter;
import mainPackage.*;
import daoPackage.*;

public class deleteServlet
extends HttpServlet {

/**
* Called once at startup
*/
public void init()
throws ServletException {
System.out.println("ARSmsg: deleteServlet began to work");
}

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

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

if (person != null) {
session.removeAttribute("personDAOsess");
session.setAttribute("personDAOsess", personDAO);
req.setAttribute("nameReq", person.getName());
req.setAttribute("lastReq", person.getLast());
req.setAttribute("hobbyReq", person.getHobby());
req.setAttribute("idReq", String.valueOf(person.getId()));
}
RequestDispatcher view = getServletContext().getRequestDispatcher("/index.jsp");
view.forward(req, resp);
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
}



readNextServlet.java
--------------------
package servletPackage;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpSession;
import javax.servlet.RequestDispatcher;
import mainPackage.*;
import daoPackage.*;

public class readNextServlet extends HttpServlet {

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

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

try {
personDAO = (PersonDAOVectorImpl) session.getAttribute("personDAOsess");
person = personDAO.readNextRec();

if (person != null) {
session.removeAttribute("personDAOsess");
session.setAttribute("personDAOsess", personDAO);
req.setAttribute("nameReq", person.getName());
req.setAttribute("lastReq", person.getLast());
req.setAttribute("hobbyReq", person.getHobby());
req.setAttribute("idReq", String.valueOf(person.getId()));
System.out.println("id=" + personDAO.getPersonNextId());
}
RequestDispatcher view = getServletContext().getRequestDispatcher("/index.jsp");
view.forward(req, resp);
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}

private String getOutput(String name) {
StringBuffer buffer = new StringBuffer();
buffer.append("<html><body><h2>Hello. Your name is: ");
buffer.append(name);
buffer.append("</h2></body></html>");

return buffer.toString();
}
}



readPrevServlet.java
--------------------
package servletPackage;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpSession;
import javax.servlet.RequestDispatcher;
import mainPackage.*;
import daoPackage.*;

public class readPrevServlet extends HttpServlet {

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

public void doGet( HttpServletRequest req, HttpServletResponse resp )
throws ServletException, IOException
{
resp.setContentType("text/html");
PersonDAOVectorImpl personDAO;
Person person= new Person();
HttpSession session = req.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 firstName =person.getName();
String lastName=person.getLast();
String hobby=person.getHobby();

req.setAttribute("nameReq", firstName);
req.setAttribute("lastReq", lastName);
req.setAttribute("hobbyReq", hobby);
req.setAttribute("idReq", id);
System.out.println("id="+personDAO.getPersonNextId());
}
RequestDispatcher view = getServletContext().getRequestDispatcher("/index.jsp");
view.forward(req,resp);

}
catch (Exception e)
{
e.printStackTrace();
}
finally
{

}
}
}



updateServlet.java
------------------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package servletPackage;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpSession;
import javax.servlet.RequestDispatcher;
import mainPackage.*;
import daoPackage.*;

public class updateServlet extends HttpServlet {

public void init()
throws ServletException
{
System.out.println("Init() worked");
}

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

try
{
String firstName = req.getParameter("name");
String lastName = req.getParameter("last");
String hobby = req.getParameter("hobby");
int id = Integer.valueOf(req.getParameter("id"));
personDAO = (PersonDAOVectorImpl) session.getAttribute("personDAOsess");
personDAO.updateRec(id, firstName, lastName, hobby);

if (person != null){
session.removeAttribute("personDAOsess");
session.setAttribute("personDAOsess", personDAO);

req.setAttribute("nameReq", firstName);
req.setAttribute("lastReq", lastName);
req.setAttribute("hobbyReq", hobby);
req.setAttribute("idReq", String.valueOf(id));
}
RequestDispatcher view = getServletContext().getRequestDispatcher("/index.jsp");
view.forward(req,resp);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{

}
}
}


listServlet.java
----------------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package servletPackage;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpSession;
import javax.servlet.RequestDispatcher;
import mainPackage.*;
import daoPackage.*;

public class listServlet
extends HttpServlet
{
/**
* Called once at startup
*/
public void init()
throws ServletException
{
System.out.println("ARSmsg: listServlet began to work");
}

public void doGet( HttpServletRequest req, HttpServletResponse resp )
throws ServletException, IOException
{

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




selectListServlet.java
----------------------
package servletPackage;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpSession;
import javax.servlet.RequestDispatcher;
import mainPackage.*;
import daoPackage.*;
import java.util.Enumeration;
import java.util.Iterator;

public class selectListServlet extends HttpServlet {

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

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


for(Enumeration e = req.getAttributeNames(); e.hasMoreElements(); ){
String attName = (String)e.nextElement();
System.out.println(attName);
}

int currentRow = Integer.valueOf(req.getParameter("curRow"));
System.out.println("currentRow=================="+currentRow);

try
{
personDAO = (PersonDAOVectorImpl) session.getAttribute("personDAOsess");
person = personDAO.readRec(currentRow);
req.setAttribute("nameReq", person.getName());
req.setAttribute("lastReq", person.getLast());
req.setAttribute("hobbyReq", person.getHobby());
req.setAttribute("idReq", String.valueOf(person.getId()));

RequestDispatcher view = getServletContext().getRequestDispatcher("/index.jsp");
view.forward(req,resp);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{

}
}


}