CGS – 4854 Summer 2012
This presentation is the property of its rightful owner.
Sponsored Links
1 / 56

CGS – 4854 Summer 2012 PowerPoint PPT Presentation


  • 65 Views
  • Uploaded on
  • Presentation posted in: General

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 .

Download Presentation

CGS – 4854 Summer 2012

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


Cgs 4854 summer 2012

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


Log4j1

Log4j


Log4j2

Log4J


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

<servlet>

<servlet-name>InitLog4j</servlet-name>

<servlet-class>shared.InitLog4j</servlet-class>

<init-param>

<param-name>logPath</param-name>

<param-value>/WEB-INF/logs/error.log</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>


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

}


Cgs 4854 summer 2012

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

<init-param>

<param-name>logPath</param-name>

<param-value>/WEB-INF/logs/error.log</param-value>

<param-name>logName</param-name>

<param-value>error.log</param-value>

<param-name>logLevel</param-name>

<param-value>Level.Debug</param-value>

</init-param>


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!


Cgs 4854 summer 2012

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


Hidden fields big picture

Hidden Fields – Big Picture


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


Cgs 4854 summer 2012

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

}


Cgs 4854 summer 2012

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;


Cgs 4854 summer 2012

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


Cgs 4854 summer 2012

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)


Cgs 4854 summer 2012

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

}


Where to place the files

Where to place the files !


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.


  • Login