Using the same technique, replace the contents of
BookServlet.java with the following:
package com.microfocus.book;
import java.io.PrintWriter;
import java.io.StringWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.microfocus.cobol.runtimeservices.IRunUnit;
import com.microfocus.cobol.runtimeservices.servlet.ServletRunUnitManager;
public class BookServlet extends HttpServlet
{
private static final long serialVersionUID = -3563065100184185678L;
public static final String STOCK_NO_ATTRIBUTE = "stockno";
public static final String TITLE_ATTRIBUTE = "title";
public static final String AUTHOR_ATTRIBUTE = "author";
public static final String TYPE_ATTRIBUTE = "type";
public static final String ISBN_ATTRIBUTE = "isbn";
public static final String PRICE_ATTRIBUTE = "price";
public static final String ONHAND_ATTRIBUTE = "onhand";
public static final String SOLD_ATTRIBUTE = "sold";
public static final String STATUS_ATTRIBUTE = "status";
public static final String RUN_UNIT_ID_ATTRIBUTE = "rununitid";
public static final String SUBMIT_PARAMETER = "submit";
public static final String READ_PARAMETER = "Read";
public static final String ADD_PARAMETER = "Add";
public static final String DELETE_PARAMETER = "Delete";
public static final String NEXT_PARAMETER = "Next";
public static final String END_PARAMETER = "End Session";
public static final String ERROR_VALUE = "ERROR";
public static final String DEFAULT_VALUE = "DEFAULT";
public static final String VIEW_URL = "/BookJsp.jsp";
protected void doProcessing(HttpServletRequest req, HttpServletResponse res, boolean isGet)
{
String subValue = req.getParameter(SUBMIT_PARAMETER);
if(subValue == null)
{
subValue = DEFAULT_VALUE;
}
setRunUnitId(req);
if(subValue.equals(READ_PARAMETER))
{
performRead(req, res);
}
else if(subValue.equals(ADD_PARAMETER))
{
performAdd(req, res);
}
else if(subValue.equals(DELETE_PARAMETER))
{
performDelete(req, res);
}
else if(subValue.equals(NEXT_PARAMETER))
{
performNext(req, res);
}
else if(subValue.equals(END_PARAMETER))
{
performEndSession(req, res);
}
else
{
outputBlankBook(req, res);
}
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(VIEW_URL);
try
{
dispatcher.forward(req, res);
}
catch(Exception e)
{
throw new RuntimeException(e);
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res)
{
doProcessing(req, res, true);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res)
{
doProcessing(req, res, false);
}
private void performRead(HttpServletRequest req, HttpServletResponse res)
{
BookInterface bookInterface = getBookInterface(req.getSession());
String bookId = getStockNo(req, res);
if(bookId == null)
{
bookId = ERROR_VALUE;
}
try
{
BookBean book = bookInterface.readBook(bookId);
outputBook(req, res, book);
}
catch(JavaBookException e)
{
outputBookException(req, res, e);
}
catch (Exception e)
{
outputException(req, res, e);
}
}
private void performAdd(HttpServletRequest req, HttpServletResponse res)
{
BookInterface bookInterface = getBookInterface(req.getSession());
try
{
BookBean book = getBook(req, res);
book = bookInterface.addBook(book);
outputBook(req, res, book);
}
catch(JavaBookException e)
{
outputBookException(req, res, e);
}
catch (Exception e)
{
outputException(req, res, e);
}
}
private void performDelete(HttpServletRequest req, HttpServletResponse res)
{
BookInterface bookInterface = getBookInterface(req.getSession());
String bookId = getStockNo(req, res);
if(bookId == null)
{
bookId = ERROR_VALUE;
}
try
{
BookBean book = bookInterface.deleteBook(bookId);
outputBook(req, res, book);
}
catch(JavaBookException e)
{
outputBookException(req, res, e);
}
catch (Exception e)
{
outputException(req, res, e);
}
}
private void performNext(HttpServletRequest req, HttpServletResponse res)
{
BookInterface bookInterface = getBookInterface(req.getSession());
String bookId = getStockNo(req, res);
if(bookId == null)
{
bookId = ERROR_VALUE;
}
try
{
BookBean book = bookInterface.nextBook(bookId);
outputBook(req, res, book);
}
catch(JavaBookException e)
{
outputBookException(req, res, e);
}
catch (Exception e)
{
outputException(req, res, e);
}
}
private void performEndSession(HttpServletRequest req, HttpServletResponse res)
{
HttpSession session = req.getSession();
session.invalidate();
outputError(req, res, "Session invalidated");
}
private void outputBlankBook(HttpServletRequest req, HttpServletResponse res)
{
outputBook(req, res, BookBean.blankBook());
}
private void outputBookException(HttpServletRequest req, HttpServletResponse res, JavaBookException jbe)
{
outputError(req, res, jbe.getMessage());
}
private void outputException(HttpServletRequest req, HttpServletResponse res, Exception e)
{
String msg = e.getClass().getName() + " [" + e.getMessage() + "]";
StringWriter strWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(strWriter);
e.printStackTrace(printWriter);
req.setAttribute(STATUS_ATTRIBUTE, strWriter.toString());
outputError(req, res, msg);
}
private void outputBook(HttpServletRequest req, HttpServletResponse res, BookBean book)
{
if(book != null)
{
req.setAttribute("book", book);
}
else
{
req.setAttribute("book", BookBean.msgBook("ERROR! book is null in output book"));
}
}
private void outputError(HttpServletRequest req, HttpServletResponse res, String msg)
{
outputBook(req, res, BookBean.msgBook(msg));
}
private BookBean getBook(HttpServletRequest req, HttpServletResponse res)
{
return new BookBean
(
getStockNo(req, res),
getIsbn(req, res),
getTitle(req, res),
getAuthor(req, res),
getType(req, res),
getPrice(req, res),
getOnhand(req, res),
getSold(req, res),
""
);
}
private String getAttribute(HttpServletRequest req, HttpServletResponse res, String attribute)
{
String stockNoStr;
stockNoStr = req.getParameter(attribute);
if(stockNoStr == null)
{
stockNoStr = ERROR_VALUE;
}
return stockNoStr;
}
private String getStockNo(HttpServletRequest req, HttpServletResponse res)
{
return getAttribute(req, res, STOCK_NO_ATTRIBUTE);
}
private String getTitle(HttpServletRequest req, HttpServletResponse res)
{
return getAttribute(req, res, TITLE_ATTRIBUTE);
}
private String getAuthor(HttpServletRequest req, HttpServletResponse res)
{
return getAttribute(req, res, AUTHOR_ATTRIBUTE);
}
private String getType(HttpServletRequest req, HttpServletResponse res)
{
return getAttribute(req, res, TYPE_ATTRIBUTE);
}
private String getIsbn(HttpServletRequest req, HttpServletResponse res)
{
return getAttribute(req, res, ISBN_ATTRIBUTE);
}
private String getPrice(HttpServletRequest req, HttpServletResponse res)
{
return getAttribute(req, res, PRICE_ATTRIBUTE);
}
private String getOnhand(HttpServletRequest req, HttpServletResponse res)
{
return getAttribute(req, res, ONHAND_ATTRIBUTE);
}
private String getSold(HttpServletRequest req, HttpServletResponse res)
{
return getAttribute(req, res, SOLD_ATTRIBUTE);
}
private IRunUnit getRunUnit(HttpSession session)
{
return ServletRunUnitManager.getManager().GetSessionRunUnit(session);
}
private void setRunUnitId(HttpServletRequest req)
{
HttpSession session = req.getSession();
IRunUnit runUnit = getRunUnit(session);
String ruid = "" + runUnit.getRunUnitID();
req.setAttribute(RUN_UNIT_ID_ATTRIBUTE, ruid);
}
private BookInterface getBookInterface(HttpSession session)
{
BookInterface output = new BookInterface(session);
return output;
}
}