CS520 Web Programming Bits and Pieces of Web Programming (II) Chengyu Sun California State University, Los Angeles
Roadmap File upload and download Sending email Task scheduling and execution Application deployment
File Upload – The Form <form action="FileUploadHandler" method="post" enctype="multipart/form-data"> First file: <input type="file" name="file1" /> <br /> Second file: <input type="file" name="file2" /> <br /> <input type="submit" name="upload" value="Upload" /> </form>
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. Content-Disposition: form-data; name="file2"; filename="test2.txt.gz" Content-Type: application/x-gzip ?????????UC
Apache commons-fileupload http://jakarta.apache.org/commons/fileupload/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() ) {...} }
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/MultipartFile.html
Store Uploaded Files In database On disk Pros and Cons?? BLOB, CLOB BINARY VARCAR, VARCHAR On disk Pros and Cons??
File Download Generate an HTTP response directly Understand Java IO 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
How Email Works SMTP, IMAP, POP Email Server A Email Server B ?? ?? ?? Client A Client B
JavaMail http://www.oracle.com/technetwork/java/javamail/index.html Maven dependency: javax.mail:mail Properties props = System.getProperties(); props.put("mail.smtp.host", mailhost); Session session = Session.getInstance( props ); Message msg = new MimeMessage(session); ... Transport.send( msg );
Spring Email Support Mail message classes Need spring-context-support dependency Declare a mailSender bean JavaMailSender interface See CSNS2 for example Mail message classes SimpleMailMessage No attachment, no special character encoding MimeMailMessage
Set Up a Local Email Server for Testing hMailServer on Windows - http://csns.calstatela.edu/wiki/content/cysun/course_materials/hmailserver postfix on Linux - http://csns.calstatela.edu/wiki/content/cysun/notes/ubuntu_server#email
Examples of Scheduled Tasks Periodic email notifications Weekly sales Member interests Payment reminders Batch data processing
Concepts in Task Scheduling and Execution … Executor Use one thread for each task Use one thread for all tasks Use a thread pool Schedule (a.k.a. Trigger) E.g. every 5 minutes, every day at midnight, every first Sunday of each month at 8am …
… Concepts in Task Scheduling and Execution Scheduler Task Schedule/Trigger Executor
Spring Configuration <task> namespace <task:scheduler id="scheduler" /> <task:executor id="executor" pool-size="5"/> <task:annotation-driven scheduler="scheduler" executor="executor" /> Creates a scheduler Creates a thread pool Based executor Enables annotations @Scheduled and @Async
@Scheduled http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/scheduling/annotation/Scheduled.html
Cron Expression Six field separated by space Seconds (0-59) Minutes (0-59) Hours (0-23) Day of month (1-31) Month (1-12 or JAN-DEC) Day of week (1-7 or MON-SUN)
Cron Field Values Single value, e.g. 12 Set of values, e.g. 0,15,30,45 Range, e.g. 1-12,MON-FRI,* (first-last) Range with step, e.g. 1-12/3
Cron Expression Examples Every hour at the beginning of the hour 0 0 * * * * 0 */10 * * * * 0 0 8 * * MON-FRI 0 0 1 1,15 * * Every 10 minutes Every weekday at 8AM Every 1st and 15th of each month at 1AM
Application Deployment Deploy Application Computer for Development Server Hosting the Web Application
Directory Structure of a Java Web Application Application Root Directory JSPs and static resources WEB-INF web.xml classes Compiled Java classes lib Additional Java libraries
WAR Files Web application ARchive A JAR file for packaging, distributing, and deploying Java web applications Create WAR files The command line tool jar Eclipse Export -> Web -> WAR file mvn package
Deploy WAR Files to a Tomcat Server Copy the WAR file to the webapps folder Use the Manager App Need a user with the manager-gui role More options at http://tomcat.apache.org/tomcat-7.0-doc/deployer-howto.html