Overview
CS520 Web Programming Logging Message bundles Bits and Pieces of Web Programming Testing Input validation File upload Email Chengyu Sun California State University, Los Angeles
Requirements of Good Logging Logging Tools
Use print statements to assist debugging Minimize performance penalty Why do we want to do that when we have GUI Support different log output debugger?? Console, file, database, … public void foo() { Support different message levels System.out.println( “loop started” ); Fatal, error, warn, info, debug, trace // some code that might get into infinite loop … Example: logging System.out.println( “loop finished” ); } Easy configuration
Java Logging Libraries Choose Your Logging Libraries
Logging implementations Log4j Commons Logging Widely used Determines logging Log4j - http://logging.apache.org/log4j/ Good performance implementation at java.util.logging in JDK runtime Easy configuration Logging API java.util.logging SLF4j Statically linked to a Apache Commons Logging (JCL) - Part of JDK, i.e. no extra http://commons.apache.org/logging/ library dependency logging implementation Cleaner design Simple Logging Façade for Java (SLF4J) - Better performance http://www.slf4j.org/ Less problem
1 Using Log4j and SLF4j Log4j Configuration File
Library dependencies log4j.xml or log4j.properties Coding Appender
Creating a Logger Output type
Logging statements Output format Configuration Logger Output format Package or class selection Message level
Log4j PatternLayout Testing Basics
http://logging.apache.org/log4j/1.2/api Unit Testing docs/org/apache/log4j/PatternLayout.ht System Testing ml Integration Testing User Acceptance Testing (Beta Testing)
Maven Support for Java Testing Frameworks JUnit/TestNG
JUnit Library dependency http://www.junit.org/ Directory structure Widely used and supported src/test/java TestNG src/test/resources http://testng.org/ The surefire plugin Technical superior to JUnit but not as widely used or supported
Example: testing BubbleSort
2 Basic TestNG Annotations Ordering Tests
@Test @Test
Method dependsOnMethods
Class dependsOnGroups
Group Annotations for various before/after methods
Test Suite TestNG and Spring
testng.xml Test classes inherit from Spring TestNG support classes
More About TestNG File Upload – The Form
TestNG Documenation –
3 File Upload – The Request Apache commons-fileupload
POST / HTTP/1.1 Host: cs.calstatela.edu:4040 http://jakarta.apache.org/commons/fileuploa […] Cookie: SITESERVER=ID=289f7e73912343a2d7d1e6e44f931195 d/using.html Content-Type: multipart/form-data; boundary=------146043902153 Content-Length: 509 FileItemFactory fileItemFactory = DiskFileItemFactory(); ------146043902153 ServletFileUpload fileUpload = new ServletFileUpload( fileItemFactory ); Content-Disposition: form-data; name="file1"; filename="test.txt" Content-Type: text/plain List items = fileUpload.parseRequest( request ); for( Object o : items ) this is a test file. { ------146043902153 FileItem item = (FileItem) items; Content-Disposition: form-data; name="file2"; filename="test2.txt.gz" if( ! item.isFormFiled() ) {...} Content-Type: application/x-gzip } ??? ??????UC
Spring File Upload Support Store Uploaded Files
multipartResolver bean In database Support multiple request parser libraries BLOB, CLOB Handle uploaded files BINARY VARCAR, VARCHAR Add an MultipartFile argument to the controller method On disk CSNS2 Example: upload() in SubmissionController
Pros and Cons??
Store Uploaded Files How Email Works
In database SMTP, IMAP, POP
ACID
BLOB/CLOB types are not very portable Email Server A Email Server B
Bad performance ?? On disk ?? ?? Not ACID
Do not need BLOB/CLOB types Client A Client B Good performance
4 JavaMail Spring Email Support
http://java.sun.com/products/javamail/ Declare a mailSender bean Mail message classes Properties props = System.getProperties(); SimpleMailMessage props.put("mail.smtp.host", mailhost); Session session = Session.getInstance( props ); http://static.springsource.org/spring/docs/current/spring- framework-reference/html/mail.html#mail-usage Message msg = new MimeMessage(session); No attachment, no special character encoding ... MimeMailMessage Transport.send( msg ); CSNS2 Example: resetPassword() in UserController
Advantages of Using Message Message Bundles Bundles
Separate text messages from Change text messages without application code and put them into their modifying the source code own files Internationalization (I18N) E.g. messages.properties messages.properties
error.username.required=A username is required. messages_en_US.properties error.password.short=The password is too short. error.username.taken=The username {0} is already taken. messages_zh_CN.properties …
Using Message Bundles with Using Message Bundles with JSTL Spring
Declare a messageSource bean
5 Example: Validate Add/Edit Input Validation in Spring User
Implement a Validator Add error messages to the message
CSNS2 Example: DepartmentValidator bundle Add a BindingResult parameter to Implement a validator and wire it to the the controller method controller Return the form view if validation fails Validate Display errors using
Commons-Validator Other Validation Options Declarative Validation Example
JavaScript validation
Commons-Validator Routines
http://commons.apache.org/validator/api- 1.3.1/org/apache/commons/validator/routines /package-summary.html Independent of the declarative validation framework A set of methods to validate Date and time Numeric values Currency ...
6