1 / 18

Bean utilities servlet classes & project

Bean utilities servlet classes & project. Tomcat 6. You can’t deploy under root/WEB-INF/classes in tomcat 6 You can use the manager deployment tool which does require a war file (containing jarred webapp) or build your own webapp using directory structure and stick it into web apps.

oralee
Download Presentation

Bean utilities servlet classes & project

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Bean utilities servlet classes & project

  2. Tomcat 6 You can’t deploy under root/WEB-INF/classes in tomcat 6 You can use the manager deployment tool which does require a war file (containing jarred webapp) or build your own webapp using directory structure and stick it into web apps. Go to homepage of tomcat (localhost:8080) to see how to edit conf/tomcat-users.xml to add a manager.

  3. Tomcat manager

  4. Manager allows you to deploy by uploading war

  5. Motivation for this project • The JSP syntax property=“*” is used to take all incoming parameters and populate beans with the data, doing simple type conversions as needed. • This doesn’t exist in servlets, but a Jakarta package makes it possible to do it. • The text has an example showing one way to do this.

  6. Install and config info requirements: Besides the Tomcat install, and possibly javax.servlet.* classes… Requires three packages from the Apache Commons library: beanutils, collections, and logging. To obtain these packages, see http://jakarta.apache.org/commons/. Also, the site http://www.coreservlets.com/ contains links to all URLs mentioned in the book, including to the specific sections of the Jakarta Commons package.

  7. About these jar files • You’ll have to put them in your class path. • Suggestions: • stick all your jar files in a directory called myjarfiles and then add the path to each to the classpath env variable. • You could probably stick them in some tomcat lib directory. You’ll still need them listed in the classpath.

  8. BeanUtilities (in slide notes also) in beans directory package coreservlets.beans; import java.util.*; import javax.servlet.http.*; import org.apache.commons.beanutils.BeanUtils; /** Some utilities to populate beans, usually based on * incoming request parameters. Requires three packages * from the Apache Commons library: beanutils, collections, * and logging. To obtain these packages, see * http://jakarta.apache.org/commons/. Also, the book's * source code archive (see http://www.coreservlets.com/) * contains links to all URLs mentioned in the book, including * to the specific sections of the Jakarta Commons package. * <P> * Note that this class is in the coreservlets.beans package, * so must be installed in .../coreservlets/beans/. * <P> * Taken from Core Servlets and JavaServer Pages 2nd Edition * from Prentice Hall and Sun Microsystems Press, * http://www.coreservlets.com/. * &copy; 2003 Marty Hall; may be freely used or adapted. */ public class BeanUtilities { /** Examines all of the request parameters to see if * any match a bean property (i.e., a setXxx method) * in the object. If so, the request parameter value * is passed to that method. If the method expects * an int, Integer, double, Double, or any of the other * primitive or wrapper types, parsing and conversion * is done automatically. If the request parameter value * is malformed (cannot be converted into the expected * type), numeric properties are assigned zero and boolean * properties are assigned false: no exception is thrown. */

  9. Bean utilities continued public static void populateBean(Object formBean, HttpServletRequest request) { populateBean(formBean, request.getParameterMap()); } /** Populates a bean based on a Map: Map keys are the * bean property names; Map values are the bean property * values. Type conversion is performed automatically as * described above. */ public static void populateBean(Object bean, Map propertyMap) { try { System.out.println("in populatebean"); BeanUtils.populate(bean, propertyMap); System.out.println("in try populate after populate call"); } catch(Exception e) { System.out.println("in catch exception for populate.."); // Empty catch. The two possible exceptions are // java.lang.IllegalAccessException and // java.lang.reflect.InvocationTargetException. // In both cases, just skip the bean operation. } } }

  10. My “insurance” bean in slide notes, too…also in beans directory package coreservlets.beans; import coreservlets.*;//need filter… remove call to filter to omit this /** Simple bean that represents information needed to * calculate an employee's insurance costs. Has String, * int, and boolean properties. Used to demonstrate * automatically filling in bean properties from request * parameters. * Taken from Core Servlets and JavaServer Pages 2nd Edition * from Prentice Hall and Sun Microsystems Press, * http://www.coreservlets.com/. * &copy; 2003 Marty Hall; may be freely used or adapted. */ public class InsuranceInfo { private String name = "No name specified"; private String employeeID = "No ID specified"; private int numChildren = 0; private boolean isMarried = false; public String getName() { return(name); }

  11. continued /** Just in case user enters special HTML characters, * filter them out before storing the name. */ public void setName(String name) { this.name = ServletUtilities.filter(name); } public String getEmployeeID() { return(employeeID); } /** Just in case user enters special HTML characters, * filter them out before storing the name. */ public void setEmployeeID(String employeeID) { this.employeeID = ServletUtilities.filter(employeeID); } public int getNumChildren() { return(numChildren); } public void setNumChildren(int numChildren) { this.numChildren = numChildren; } /** Bean convention: name getter method "isXxx" instead * of "getXxx" for boolean methods. */ public boolean isMarried() { return(isMarried); } public void setMarried(boolean isMarried) { this.isMarried = isMarried; }}

  12. The servlet in slide notes also package coreservlets; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import coreservlets.beans.*; /** Example of simplified form processing. Illustrates the * use of BeanUtilities.populateBean to automatically fill * in a bean (Java object with methods that follow the * get/set naming convention) from request parameters. * <P> * Taken from Core Servlets and JavaServer Pages 2nd Edition * from Prentice Hall and Sun Microsystems Press, * http://www.coreservlets.com/. * &copy; 2003 Marty Hall; may be freely used or adapted. */ public class SubmitInsuranceInfo extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

  13. Insurance servlet continued InsuranceInfo info = new InsuranceInfo(); BeanUtilities.populateBean(info, request); response.setContentType("text/html"); PrintWriter out = response.getWriter(); String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " + "Transitional//EN\">\n"; String title = "Insurance Info for " + info.getName(); out.println(docType + "<HTML>\n" + "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" + "<BODY BGCOLOR=\"#FDF5E6\">\n" + "<CENTER>\n" + "<H1>" + title + "</H1>\n" + "<UL>\n" + " <LI>Employee ID: " + info.getEmployeeID() + "\n" + " <LI>Number of children: " + info.getNumChildren() + "\n" + " <LI>Married?: " + info.isMarried() + "\n" + "</UL></CENTER></BODY></HTML>"); } }

  14. My html form <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <!-- Front end to InsuranceInfo servlet. Note that the names of the form parameters here *must* match the names of the bean properties. --> <HTML><HEAD><TITLE>Employee Insurance Signup</TITLE></HEAD> <BODY BGCOLOR="#FDF5E6"> <CENTER> <H1>Employee Insurance Signup</H1> <FORM ACTION="http://localhost/servlet/coreservlets.SubmitInsuranceInfo"> Name: <INPUT TYPE="TEXT" NAME="name"><BR> Employee ID: <INPUT TYPE="TEXT" NAME="employeeID"><BR> Number of Children: <INPUT TYPE="TEXT" NAME="numChildren"><BR> <INPUT TYPE="CHECKBOX" NAME="Married" VALUE="true">Married?<BR> <CENTER><INPUT TYPE="SUBMIT"></CENTER> </FORM> </CENTER></BODY></HTML>

  15. The form – note error on married checkbox in next slide

  16. Running servlet

  17. Jar files • You’ll have to decide how to set up your runtime. • Adding jars to the classpath becomes cumbersome. • Adding jars to the lib directory of each webapp may waste storage. • See next slide

  18. webapps myexample WEB-INF classes Lib folder- this is another place to stick jar files lib Web.xml

More Related