Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS520 Web Programming Bits and Pieces of Web Programming (I) Chengyu Sun California State University, Los Angeles.

Similar presentations


Presentation on theme: "CS520 Web Programming Bits and Pieces of Web Programming (I) Chengyu Sun California State University, Los Angeles."— Presentation transcript:

1 CS520 Web Programming Bits and Pieces of Web Programming (I) Chengyu Sun California State University, Los Angeles

2 Roadmap Logging Testing File upload and download Exception Handling

3 Logging Recording events happened during software execution, e.g. using print statements to assist debugging Why do we want to do that when we have GUI debugger?? public void foo() { System.out.println( “loop started” ); // some code that might get into infinite loop … System.out.println( “loop finished” ); }

4 Requirements of Good Logging Tools Minimize performance penalty Support different log output Console, file, database, … Support different message levels Fatal, error, warn, info, debug, trace Easy configuration

5 Java Logging Libraries Logging implementations Log4j - http://logging.apache.org/log4j/http://logging.apache.org/log4j/ java.util.logging in JDK Logging API Apache Commons Logging (JCL) - http://commons.apache.org/logging/ http://commons.apache.org/logging/ Simple Logging Façade for Java (SLF4J) - http://www.slf4j.org/ http://www.slf4j.org/

6 Choose Your Logging Libraries Log4j Widely used Good performance Rich features Easy configuration java.util.logging Part of JDK, i.e. no extra library dependency Commons Logging Determines logging implementation at runtime SLF4j Statically linked to a logging implementation  Cleaner design  Better performance  Less problem

7 Using Log4j and SLF4j Library dependencies Coding Creating a Logger Logging statements Configuration Output format

8 Log4j Configuration File log4j.xml or log4j.properties Appender Output type Output format Logger Package or class selection Message level

9 Log4j PatternLayout http://logging.apache.org/log4j/1.2/api docs/org/apache/log4j/PatternLayout.ht ml

10 Logging in CSNS2 src/main/resources/log4j.xml build.properties app.log.dir app.log.level Use of Log Viewer plugin in Eclipse

11 Software Testing Unit Testing Integration Testing System Testing User Acceptance Testing (Beta Testing)

12 Java Testing Frameworks JUnit http://www.junit.org/ Widely used and supported TestNG http://testng.org/ Technically superior to JUnit (for a while) but not as widely used or supported

13 What We Want From a Testing Framework Automation Including setup and tear-down Grouping and selection Test Dependency Skip tests that depend on tests that already failed Run independent test in parallel Report Other, e.g. tool support, API, dependency injection, and so on

14 Maven Support for JUnit/TestNG Library dependency Directory structure src/test/java src/test/resources The surefire plugin

15 Basic TestNG Annotations @Test Method Class Annotations for various before/after methods http://testng.org/doc/documentation-main.html#annotations

16 Group and Dependency @Test groups dependsOnMethods dependsOnGroups

17 Test Suite A test suite is represented by an XML file and contains one or more tests. Select tests by method, class, package, and group Include and exclude

18 Test Suite Example testng.xml

19 TestNG in CSNS2 Configuration Test classes inherit from Spring TestNG support classes @ContextConfiguration BeforeSuite and AfterSuite methods

20 More About TestNG TestNG Documenation – http://testng.org/doc/documentation- main.html http://testng.org/doc/documentation- main.html Next Generation Java Testing by Cédric Beust and Hani Suleiman

21 File Upload and Download in Web Applications Pictures on online albums and social networking sites Attachments in web forum posts and web emails Resumes on job search sites Student homework submissions in learning management systems (LMS) …

22 File Upload – The Form <form action="FileUploadHandler" method="post" enctype="multipart/form-data"> First file: Second file:

23 File Upload – The Request POST / HTTP/1.1 Host: cs.calstatela.edu:4040 […] Cookie: SITESERVER=ID=289f7e73912343a2d7d1e6e44f931195 Content-Type: multipart/form-data; boundary=---------------------------146043902153 Content-Length: 509 -----------------------------146043902153 Content-Disposition: form-data; name="file1"; filename="test.txt" Content-Type: text/plain this is a test file. -----------------------------146043902153 Content-Disposition: form-data; name="file2"; filename="test2.txt.gz" Content-Type: application/x-gzip ?????????UC

24 Apache commons-fileupload http://jakarta.apache.org/commons/fileuploa d/using.html Maven dependency: commons-fileupload:commons-fileupload FileItemFactory fileItemFactory = DiskFileItemFactory(); ServletFileUpload fileUpload = new ServletFileUpload( fileItemFactory ); List items = fileUpload.parseRequest( request ); for( Object o : items ) { FileItem item = (FileItem) items; if( ! item.isFormFiled() ) {...} }

25 Handle File Upload in Spring multipartResolver bean Support multiple request parser libraries See CSNS2 for example Add an @RequestParam argument of type MultipartFile to the controller method http://docs.spring.io/spring/docs/current/javadoc- api/org/springframework/web/multipart/MultipartF ile.html http://docs.spring.io/spring/docs/current/javadoc- api/org/springframework/web/multipart/MultipartF ile.html

26 Store Uploaded Files In database BLOB, CLOB BINARY VARCAR, VARCHAR On disk Pros and Cons??

27 File Download Generate an HTTP response directly Add an HttpServletResponse argument to the controller method Return null for view Understand Java IO Reader and Writer InputStream and OutputStream Use of the Content-Disposition header

28 Problem with Java Exceptions Too many checked exceptions Checked vs. Runtime exceptions Require lots of boilerplate exception handling code

29 Spring’s Solution to the Exception Problem Use primarily runtime exceptions Separate exception handling code into exception handlers

30 Controller Exception Handing in Spring An Exception Resolver handles all runtime exceptions thrown by controllers SimpleMappingExceptionResolverSimpleMappingExceptionResolver maps different exceptions to different views

31 ExceptionResolver in CSNS2 <bean id="exceptionResolver“ class="csns.web.resolver.ExceptionResolver"> exception/access exception/mail A SimpleMappingExceptionResolver with additional logging.


Download ppt "CS520 Web Programming Bits and Pieces of Web Programming (I) Chengyu Sun California State University, Los Angeles."

Similar presentations


Ads by Google