1 / 86

CGS – 4854 Summer 2012

CGS – 4854 Summer 2012 . Instructor: Francisco R. Ortega Chapter 5. Web Site Construction and Management. Today’s Lecture. Brief review of Log4J Chapter 5 Remember – Mid-Term next class. Mid-Term. Mid-Term June 21 st . Chapters 1,2,3 and 4. Possible review for mid-term

lisbet
Télécharger la présentation

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. 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. CGS – 4854 Summer 2012 Instructor: Francisco R. Ortega Chapter 5 Web Site Construction and Management

  2. Today’s Lecture • Brief review of Log4J • Chapter 5 • Remember – Mid-Term next class

  3. 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 : Regular Expressions (if covered before the exam) • You are allowed to bring two letter size paper to the exam

  4. ASCII Table (Part 1)

  5. Regular Expressions • Match strings of text (wiki) • Sequence of regular expressions is known as a pattern • Regular expressions contain • Wildcards • Special characters • Escape sequences

  6. Regular Expressions 101 • Characters match themselves except: [\^$.|?*+() • \ suppresses the meaning of special characters • [] starts a character class. We match one from the class. • - specifies a range of characters • ^ negates a character class • . matches any single character except line break • | matches either the left, or the right (or)

  7. Character Classes • [xyz] : will match x or y or z • [a-z] : will match lowercase letters • [a-zA-Z] : will match all letters • [a-Z] :will not match any letters (why?) • [A-z] : will match all letters but additional symbols. Why? • [^abc] : Any character except for a,b or c.

  8. Predefined classes

  9. Escape Sequence • \. : would match a period • [.] : would match a period • \ does not lose special meaning inside square brackets [\d]

  10. Alternation • yes|no • yes|no|maybe • It will match either yes,no or maybae. • But only one of them.

  11. Grouping and Capturing • (pattern) • Capturing pattern. • Can retrieve values from \1 thru \9 • Example • Text: abyes3 • [a-z] ([a-z]) (yes|no) \d • \1 is equal to b • \2 is equal to yes • (?:pattern) • Only used for grouping

  12. Ignoring case • (?i)yes|no • [yY] [eE] [sS] | [Nn] [Oo]

  13. Repetition

  14. Regex in java • You will need to use two backslashes • Regex: \d • Java regex: “\\d”

  15. Hibernate (wiki) • Mapping from Java classes to database tables • Mapping Java data types to SQL data types • Data query and retrieval facilities. • Generates the SQL calls • Required Validation

  16. Hibernate Required Validation • Uses annotations • @Pattern(regexp = “…” , message = “Some Message”) • There is a default message • Annotations • @Pattern • @NotNull (do not use with primitives) • Annotations to be used with Numbers • @Min ( value = 20) • @Max (value = 100) • @Range(min= 0 , max =100) • Annotations for Collections will be covered in Ch. 6

  17. Required Validation @Pattern(regex=".*\\S.*", message="cannot be empty") @NotNull public String getHobby() { return hobby; } If more than one annotation, then is assume that logical operator is “AND”

  18. Question • .*\S.* • In java “.*\\S.*

  19. JSP Hobby ${helper.errors.hobby} <input type="text" name="hobby" value="${helper.data.hobby}"> <br> Aversion ${helper.errors.aversion} <input type="text" name="aversion" value="${helper.data.aversion}">

  20. Min/Max

  21. Creating Error Messages • Class that performs validation is: • Validator • Only one instance is needed • Created from Validator Factory • Only one instance is needed

  22. Creating Error Messages • Protected static final ValidatorFactoryvalidatorFactory = Validation.buildDefaultValidatorFactory(); • Protected static final Validator validator= validatorFactory.getValidator(); • Array of validation is created by calling validator’s validate method. • Set<ConstraintViolation<Object>> violations = validator.validate(data);

  23. Implementing Validation • Using the array provided by hibernate • Linear Search • Using a Map (HashMap) • Insert O(1) • Get/Set O(1) • Contain Key Average O(1 + n/k) worst case is O(n) • Where have we seen something similar to the map structure in this class? • Request.getSession().setAttribute(“helper”,this);

  24. Changes to Helper • errorMap • setErrors • getErrors() • clearErrors() All of this can be found in HelperBaseCh5 in the shared folder.

  25. HelperBaseCh5 HelperBase HttpServletRequest request HttpServletResponse response Logger logger Map errorMap Abstract copyFromSession() addHelperToSession() executeButtonMethod() fillBeanFromRequest() setErrors() getErrors() isValid()

  26. java.util.Map Example Map<String, MyBean> myMap = new HashMap<String,MyBean(); MyBean bean = new MyBean(); myMap.put(“theBean”, bean); MyBeananotherBean = myMap.get(“theBean”);

  27. errorMap • java.util.Map<String,String> errorMap = new HashMap<String, String> (); • setErrors(Object data)

  28. isValid Public booleanisValid(Object data) { setErrors(data); Return errorMap.isEmpty(); }

  29. getErrors + clearErrors() Public map getErrors() { return errorMap; } Public map clearErrors() { if (errorMap != null) errorMap.clear(); }

  30. Setting the Error Message @ButtonMethod(buttonName=“confirmButton”) Public String confirmMethod() { fillBeanFromRequest(data); String address; if (isValid(data)) { address = jspLocation(“Confirm.jsp”); } else { address = jspLocation(“Edit.jsp”); } return address; }

  31. Retrieving the Error Messages Hobby ${helper.errors.hobby} <br> <input type="text" name="hobby" value="${helper.data.hobby}">

  32. The Big Picture!

  33. Summary • Bean • Validation annotations • Helper Base (HelperBaseCh5) • Error map and interfaces for the error • Controller Helper • Modify the logic for the buttons/actions • JSP • ${helper.errors.someField}

  34. Post versus Get? • Post does not encode data in URL • Post has two additional headers • Content-Type • Content-Length • A blank line follows content-length and then data is encoded in the request.

  35. Post Request

  36. Using Post • Change the form method <form method = “Post” action=“Controller”> . . . </form>

  37. POST GET Request is created when Types url into browser Follows Hypertext link Click in button of form • Request is only generated when button is clicked • It allows to know if is the first time in the form or not • Hide Data • No limit to the amount of data. • More secure???

  38. Controller Protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ ControllerHelper helper = new ControllerHelper(this,request,response); Helper.doPost(); }

  39. ControllerHelper Public void doPost() throws ServletException, IOException{ addHelperToSession(“helper”,SessionData.READ); String address = executeButtonMethod(); request.getRequestDispatcher(address).forward( request, response); }

  40. ControllerHelper Public void doGet() throws ServletException, IOException{ addHelperToSession(“helper”,SessionData.IGNORE); String address = editMethod(); request.getRequestDispatcher(address).forward( request, response); }

  41. Hibernate: BEAN  DB • Focuses in the data • Generate SQL statements to • save the bean • Update a bean • Remote a bean • Create tables into database

  42. Hibertante jar files • Zip files in moodle site and book site • Hibernate.zip and non-hibernate.zip • Jar files needed for hibernate(table 5.3)

  43. Configuration • Use java program • Use XML

  44. Configuration via java InitHibernate method – in Controller Helper Properties props = new Properties(); props.setProperty("hibernate.dialect","org.hibernate.dialect.MySQLDialect"); props.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver"); props.setProperty("hibernate.c3p0.min_size", "1"); props.setProperty("hibernate.c3p0.max_size", "5"); props.setProperty("hibernate.c3p0.timeout", "300"); props.setProperty("hibernate.c3p0.max_statements", "50"); props.setProperty("hibernate.c3p0.idle_test_period", "300"); props.setProperty("hibernate.connection.url","jdbc:mysql://SERVER:PORT/DATABASE"); props.setProperty("hibernate.connection.username", "USERNAME"); props.setProperty("hibernate.connection.password", "PASSWORD"); initSessionFactory(props,RequestDataPersistent.class);

  45. Configuration via XML • XML overrides java configuration • Place file in the same directory as your classes. • File name • hibernate.cfg.xml

  46. Creating Tables • 1. Manually create the database • 2. Add a conditional statement in web.xml and created them in the java.code

  47. web.xml <servlet> <servlet-name>PersistentController</servlet-name> <servlet-class>ch5.persistentData.Controller</servlet-class> <init-param> <param-name>create</param-name> <param-value>false</param-value> </init-param> </servlet>

  48. Reading “CREATE” • Boolean create = Boolean.parseBoolean(servlet.getInitParameter(“create”); if (create) { HibernateHelper.createTable(RequestDataPersistent.class); }

  49. Simplify initHibernate Static public void initHibernate(boolean create) { if (create) { HibernateHelper.createTable(RequestDataPersistent.class); } HibernateHelper.initSessionFactory(RequestDataPersistent.class) }

More Related