Transcript CS10 Java Programming Basic Language Features
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" />
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
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() ) {...} }
Handle File Upload in Spring multipartResolver bean See CSNS2 for example Add an @RequestParam type MultipartFile argument of to the controller method Support multiple request parser libraries http://docs.spring.io/spring/docs/current/javadoc api/org/springframework/web/multipart/MultipartF ile.html
Store Uploaded Files In database BLOB, CLOB BINARY VARCAR, VARCHAR On disk Pros and Cons??
File Download Generate an HTTP response directly Add an HttpServletResponse controller method argument to the 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/jav amail/index.html
Maven dependency: javax.mail:mail Properties props props.put("mail.smtp.host", mailhost); Session session = System.getProperties(); = Session.getInstance( props ); Message msg = new MimeMessage(session); ...
Transport.send( msg );
Spring Email Support 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 … Task 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 Executor Task Schedule/Trigger
Spring Configuration
@Scheduled http://docs.spring.io/spring/docs/curren t/javadoc api/org/springframework/scheduling/an notation/Scheduled.html
Cron Expression 1.
2.
3.
4.
5.
6.
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,* last) Range with step, e.g. 1-12/3 (first-
Cron Expression Examples 0 0 * * * * 0 */10 * * * * 0 0 8 * * MON-FRI 0 0 1 1,15 * * Every hour at the beginning of the hour Every 10 minutes Every weekday at 8AM Every 1 st and 15 th month at 1AM of each
Application Deployment Computer for Development Deploy Application 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 W eb application AR chive 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