slide1
Download
Skip this Video
Download Presentation
CGS – 4854 Summer 2012

Loading in 2 Seconds...

play fullscreen
1 / 56

CGS – 4854 Summer 2012 - PowerPoint PPT Presentation


  • 99 Views
  • Uploaded on

CGS – 4854 Summer 2012 . Instructor: Francisco R. Ortega Chapter 3 Part 1. Web Site Construction and Management. Today’s Lecture. Chapter 4 Quiz 4 will be short and given at the start of class (June 14) Covers chapter 3 and Chapter 4. . Mid-Term. Mid-Term June 21 st .

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'CGS – 4854 Summer 2012' - nike


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
slide1
CGS – 4854 Summer 2012

Instructor: Francisco R. Ortega

Chapter 3 Part 1

Web Site Construction and Management

today s lecture
Today’s Lecture
  • Chapter 4
  • Quiz 4 will be short and given at the start of class (June 14)
    • Covers chapter 3 and Chapter 4.
mid term
Mid-Term
  • Mid-Term June 21st.
    • Chapters 1,2,3 and 4.
  • Possible review for mid-term
    • June 14 (after quiz 4) or June 19
  • Extra Credit for Mid-Term
    • Extra credit question may be Java related or Regular Expressions (if covered before the exam)
  • You are allowed to bring one letter size paper to the exam
enhancing the controller
Enhancing the Controller
  • We will be adding some cool features to our controller from Chapter 3
    • Logging
logging
Logging
  • Excellent resource to keep debugging information
  • We will be using log4j
    • http://logging.apache.org/log4j/1.2/
log4j
Log4j
  • Up to 5 levels
    • It will only write the same level or lower levels
      • Fatal errors always are written to the log
    • FATAL, ERROR, WARN, INFO, DEBUG
examples
Examples
  • warn(“optional playerName variable is empty”);
    • Warn, info, debug
initialization servlet
Initialization Servlet
  • It is great to have servlets that set up initial values/classes that you will need
    • They will be called once
    • It cannot be called by a user via the browser
initialization servlet log4j
Initialization Servlet + Log4j

InitLog4j

shared.InitLog4j

logPath

/WEB-INF/logs/error.log

1

question
Question
  • Why don’t we need mapping for the initialization server?
initialization servlet pg 109
Initialization Servlet (pg. 109)

public class InitLog4j extendsHttpServlet {

publicvoidinit()

{ … }

privateFileAppendergetAppender(String filename){ … }

privatevoid initLogger(String name,

FileAppenderappender,

Level level)

{ … }

}

helper methods getappender
Helper Methods: getAppender

PrivateFileAppendergetAppender(StringfileName) {

RollingFileAppenderappender = null;

try{

Appender = newRollingFileAppender(…..)

appender.setMaxBackupIndex(5);

Appender.setMaxFileSize(“1MB”);

}

Catch (IOException ex) {

}

return appender;

}

helper methods initlogger
Helper Methods: initLogger

private void initLogger(String name, FileAppender Name, Level level)

{

Logger logger;

If (name == null)

{

logger = Logger.getRootLogger();

}

else {

logger = Logger.getLogger(name);

}

logger.setLevel(level);

logger.addAppender(appender)

Logger.info(“Starting “ + logger.getName();

}

slide16
Init()

public void init() {

String initPath = getInitParameter(“logPath”);

String logPath = “/WEB-INF/logs/errors.log”

If (initPath != null) logPath = initPath;

FileAppenderappender = getAppender(logPath)

If (appender == null) return;

initLogger(null, appender, Level.ERROR);

initLogger(“org.apache.commons.beanutils”, appender, Level.DEBUG);

}

adding more initialization parameters
Adding more initialization parameters

logPath

/WEB-INF/logs/error.log

logName

error.log

logLevel

Level.Debug

note shared package book
Note: shared Package (book)
  • The helper based classed has been renamed to HelperBaseCh4 (and ch5, ch6) in the shared package.
  • HelperBaseClass has 4 parameters now
    • servlet,request,response,logger
  • Checked moodle or the book site for the shared package
initializing the logger
Initializing the logger
  • We will initialize in the the base class!
slide20
HerlperBaseCh4

public HelperBaseCh4(

HttpServlet servlet,

HttpServletRequest request,

HttpServletResponse response) {

this.servlet = servlet;

this.request = request;

this.response = response;

initLogger();

}

protected void initLogger() {

String logName = "bytesizebook.webdev";

String initName = servlet.getInitParameter("logName");

if (initName != null) logName = initName;

Level logLevel = Level.DEBUG;

String strLevel = servlet.getInitParameter("logLevel");

if (strLevel != null) {

logLevel = Level.toLevel(strLevel);

}

logger = Logger.getLogger(logName);

logger.setLevel(logLevel);

}

}

hidden fields helper
Hidden Fields -- ${helper}
  • Controller Helper
    • Has bean with data entered by user
    • Write itself to the session ( this )
  • Hidden fields
    • Stores data
    • Send it back to application
      • Trigger by button
  • Session State
    • Data is stored for duration of the session.
    • Controller Helper can retrieve it from the session
eliminating hidden fields
Eliminating Hidden Fields
  • You may want a site to let you restart
  • You may want to save a helper from a different application
note using enumeration
Note: Using enumeration

If (state == SessionData.READ) {

…..

}

public enumStreetLight { RED, YELLOW, GREEN};

http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

helper methods
Helper Methods
  • addHelperToSession
    • setAttribute to add helper
    • Retrieve old helper from session
  • copyFromSession
    • Copy specific values from old session
      • In our particular example, it copies the entire bean
slide26
public void addHelperToSession(String name, SessionDatastate) {

if (SessionData.READ== state) {

Object sessionObj =

request.getSession().getAttribute(name);

if (sessionObj != null) {

copyFromSession(sessionObj);

}

}

request.getSession().setAttribute(name, this);

}

slide27
public void copyFromSession(Object sessionHelper) {

if (sessionHelper.getClass() == this.getClass()) {

ControllerHelperoldHelper =

(ControllerHelper)sessionHelper;

data = oldHelper.data;

}

}

packages needed for basehelperch4
Packages needed for BaseHelperCh4

import java.io.IOException;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Level;

import org.apache.log4j.Logger;

slide29
public abstract class HelperBaseCh4 {

protected abstract void copyFromSession(Object helper);

public void addHelperToSession(String name,

SessionData state) {

}

public void addHelperToSession(String name,

booleancheckSession) {

}

}

java question
Java Question
  • Why use abstract classes?
  • Why is it that we can call an abstract method from the base class ?
  • What would happened if we didn’t have an abstract method for our example?
  • Can the implementation of the copyFromSession Method be moved to the base class? Explain.
note copytosession
NOTE: CopyToSession
  • By making the copyToSession an abstract method in the HelperBase, then the addHelperToSession method can be defined in the base class.
addhelpertosession
AddHelperToSession
  • Parameter name should be unique
    • In our book, we keep it the same
    • If you were using multiple controllers
      • Make sure to have a different name for each helper
  • Parameter state
    • Use READ when:
      • Need to read old helper.
    • Use IGNORE when:
      • Start of a new request
slide33
HelperBase

HttpServletRequest request

HttpServletResponse response

Logger logger

Abstract copyFromSession()

addHelperToSession()

ControllerHelper

RequestData data

doGet()

getData()

copyFromSession()

copyfromsession
copyFromSession

if (sessionHelper.getClass() == this.getClass() ){

ControllerHelper helper = (ControllerHelper)sessionHelper;

data = helper.data;

}

It’s important to know that the helper is the same type of controller for the application

controllerhelper doget
ControllerHelper.doGet()

public voiddoGet()

throws ServletException, IOException

{

addHelperToSession(“helper”,SessionData.READ);

}

location jsp
Location JSP
  • If not relative
    • Must specify path
location of jsp old way
Location of JSP – OLD WAY

if (request.getParameter("processButton") != null) {

address = "/ch3/dataBean/Process.jsp";

}

else if (request.getParameter("confirmButton") != null) {

address = "/ch3/dataBean/Confirm.jsp";

}

else

{

address = "/ch3/dataBean/Edit.jsp";

}

location of jsp
Location of JSP

protected String jspLocation(String page) {

return "/ch3/dataBean/" + page;

}

location of jsp new way
Location of JSP - New Way

if (request.getParameter("processButton") != null) {

address = "Process.jsp";

}

else if (request.getParameter("confirmButton") != null)

{

address = "Confirm.jsp";

}

Else

{

address = "Edit.jsp";

}

address = jspLocation(address);

visible and hidden jsp
Visible and Hidden JSP
  • Visible
    • /homework3/jsp/
  • Hidden
    • /WEB-INF/homework3/jsp/
  • WEB-INF is hidden from the browser not the servlet
jsp location more
JSP Location - MORE
  • If jsp is at the same location of the class
    • “/WEB-INF/classes/mypackage/myclass/” + page
  • Where to keep the JSPs pages?
    • Keep it hidden to yield to the controller.
jsp location more1
JSP LOCATION - MORE
  • If you are single developer
    • You may want to keep the jsp in the same location that you have your controller
    • Starting chapter 4, the book will follow this method.
  • If you are working in a team or want to keep things organized
    • Keep JSP in separate folders
controller logic more
Controller Logic - MORE
  • Controllers need to perform several task
    • Save/Load Data
    • Validate Data
    • Set address of next page
  • To keep things more organized
    • Use a method for each button
    • Use annotations to specify the button
decoding button names
Decoding Button Names

@ButtonMethod(buttonName= “confirmButton“)

public String confirmButton() {

fillBeanFromRequest(data);

String address;

if (isValid(data) ){

address = “Confirm.jsp”;

}

else

{

address = “Edit.jsp”;

}

return jspLocation(address);

}

buttons annotations
Buttons & Annotations
  • Use isDefault=True parameter for the default button
    • @ButtonMethod(buttonName=“editButton”, isDefault=true)
    • Even if no button is clicked, this will be the default action
  • Make sure the annotation reflects the name of the method and button
executing the button
Executing the button
  • Call executeButtonMethod()
  • Call executeButtonMethods(Class clazz, booleansearchForDefaults)
  • Call InvokeButtonMethod(Method buttonMethod)
slide47
protected String executeButtonMethod(Class clazz,

booleansearchForDefault)

throws IllegalAccessException, InvocationTargetException {

String result = "";

Method[] methods = clazz.getDeclaredMethods();

for (Method method : methods) {

ButtonMethod annotation =

method.getAnnotation(ButtonMethod.class);

if (annotation != null) {

if (searchForDefault && annotation.isDefault()) {

methodDefault = method;

}

if (request.getParameter(annotation.buttonName())

!= null) {

result = invokeButtonMethod(method);

break;

}

}

}

in class participation
In-Class participation
  • Due June 14: please write pseudo-code and comments explaining how the button invocation works.
  • This includes the following methods from HelperBaseCh4
    • Call executeButtonMethod()
    • Call executeButtonMethods(Class clazz, booleansearchForDefaults)
    • Call InvokeButtonMethod(Method buttonMethod)
  • It will be only be accepted late during the day of June 14. It must be handed to me before the end of my office hours.
filling a bean
Filling a bean
  • Can we automate the mutators methods for the bean?
    • void setPlayerName(string playerName)
  • We can use a java extension to do this
  • Remember to follow the name conventions
    • playerName will be?
      • setPlayerName(….)
      • getPlayerName()
what do you need
What do you need?
  • commons-collection
    • Version used in book: 3.2.1
  • commons-logging
    • Version used in book: 1.1.1
  • commons-beanutils
    • Version used in book: 1.8.3
  • Download from the book site (jar) or
    • htttp://jakarta.apache.org/common/
fillbeanfromrequest
fillBeanFromRequest

public void fillBeanFromRequest(Object data) {

try {

populate(data, request.getParameterMap());

} catch (IllegalAccessExceptioniae) {

logger.error("Populate - Illegal Access.", iae);

} catch (InvocationTargetExceptionite) {

logger.error("Populate - Invoke Target.", ite);

}

}

using fillbeanfromrequest
Using fillBeanFromRequest

@ButtonMethod(buttonName=“confirmButton”)

Public String confirmMethod() {

fillBeanFromRequest(data);

return jspLocation(“Confirm.jsp”)

}

controllerhelper doget1
ControllerHelper – doGet()

protected void doGet()

throws ServletException, IOException {

addHelperToSession("helper", SessionData.READ);

String address = executeButtonMethod();

RequestDispatcherdisp =

request.getRequestDispatcher(address);

disp.forward(request, response);

}

notes
Notes
  • Review the code in pages 130 and 131 of your book
office hours
Office Hours
  • Remember that I switch my office hours for Tuesdays.
ad