CS520 Web Programming Bits and Pieces of Web Programming (II) Chengyu Sun California State University, Los Angeles
Roadmap Sending Task scheduling and execution View Template Framework Application deployment
Function in Web Applications Various notifications, e.g. order, payment, shipment, forum post and reply, account activities, and so on Advertisement campaigns and product promotions Password reset …
How Works SMTP, IMAP, POP Client AClient B Server A Server B ??
JavaMail amail/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 );
Set Up a Local Server for Testing hMailServer on Windows - cysun/course_materials/hmailserver cysun/course_materials/hmailserver postfix on Linux - cysun/notes/ubuntu_server# cysun/notes/ubuntu_server#
Spring Support Need spring-context-support dependency Declare a mailSender bean JavaMailSender interface JavaMailSender See CSNS2 for example Mail message classes Simpl Message No attachment, no special character encoding Mim Message
Examples of Scheduled Tasks Periodic s, e.g. Information about weekly sale Forum daily digests Member newsletters 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 namespace <task:executor id="executor" pool-size="5"/> <task:annotation-driven scheduler="scheduler" executor="executor" /> Creates a scheduler Creates a thread pool Based executor Enables
@Scheduled t/javadoc- api/org/springframework/scheduling/an notation/Scheduled.html
Cron Expression Six field separated by space 1. Seconds (0-59) 2. Minutes (0-59) 3. Hours (0-23) 4. Day of month (1-31) 5. Month (1-12 or JAN-DEC) 6. 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 0 0 * * * * 0 */10 * * * * * * MON-FRI ,15 * * Every hour at the beginning of the hour Every 10 minutes Every weekday at 8AM Every 1 st and 15 th of each month at 1AM
Apache Tiles A template framework for JSP Similar to Master Page and Razor in ASP.NET
The Need for View Templates Header Footer Content 1 Header Footer Content N page1.jsppageN.jsp
Why JSP Is Not Enough It breaks HTML syntax, e.g. in header.jsp and in footer.jsp. It’s difficult to create and manage multiple complex templates.
Basic Tiles Usage Template page = JSP + placeholders (i.e. attributes), Content: JSP, string, other tiles Definition: combine template and content Tiles Tag Library
A Tiles Example Header Footer Content 1 Header Footer Content 2 page1.jsppage2.jsp
Tiles – Template Page Header Footer template.jsp
Tiles – Content Pages Content 1Content 2 page1.jsppage2.jsp
Tiles – Definitions
Resolve Tiles Views in Spring /WEB-INF/tiles.xml
Tiles Usage in CSNS2 WEB-INF/layout for template and header/footer pages WEB-INF/content for content pages WEB-INF/tiles.xml for definitions
Application Deployment Computer for Development Server Hosting the Web Application Deploy Application
Directory Structure of a Java Web Application Application Root Directory WEB-INF JSPs and static resources classes web.xml lib Compiled Java classes 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 doc/deployer-howto.html doc/deployer-howto.html