Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS520 Web Programming Bits and Pieces of Web Programming (II)

Similar presentations


Presentation on theme: "CS520 Web Programming Bits and Pieces of Web Programming (II)"— Presentation transcript:

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

2 Roadmap File upload and download Sending email
Task scheduling and execution Application deployment

3 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>

4 File Upload – The Request
POST / HTTP/1.1 Host: cs.calstatela.edu:4040 […] Cookie: SITESERVER=ID=289f7e a2d7d1e6e44f931195 Content-Type: multipart/form-data; boundary= Content-Length: 509 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

5 Apache commons-fileupload
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() ) {...} }

6 Handle File Upload in Spring
multipartResolver bean Support multiple request parser libraries See CSNS2 for example Add argument of type MultipartFile to the controller method

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

8 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

9 How Email Works SMTP, IMAP, POP Email Server A Email Server B ?? ?? ??
Client A Client B

10 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 );

11 Spring Email Support Mail message classes
Need spring-context-support dependency Declare a mailSender bean JavaMailSender interface See CSNS2 for example Mail message classes Simpl Message No attachment, no special character encoding Mim Message

12 Set Up a Local Email Server for Testing
hMailServer on Windows - postfix on Linux -

13 Examples of Scheduled Tasks
Periodic notifications Weekly sales Member interests Payment reminders Batch data processing

14 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 …

15 … Concepts in Task Scheduling and Execution
Scheduler Task Schedule/Trigger Executor

16 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

17 @Scheduled

18 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)

19 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

20 Cron Expression Examples
Every hour at the beginning of the hour 0 0 * * * * 0 */10 * * * * 0 0 8 * * MON-FRI ,15 * * Every 10 minutes Every weekday at 8AM Every 1st and 15th of each month at 1AM

21 Application Deployment
Deploy Application Computer for Development Server Hosting the Web Application

22 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

23 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

24 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


Download ppt "CS520 Web Programming Bits and Pieces of Web Programming (II)"

Similar presentations


Ads by Google