Case Studies

How to logout.

Acornsnap makes judgement of an session's principal using property(session scope) 'acornsnap.principal'. Remove it to clean an session's principal in a controller's action.

public void logout() throws Exception{
  session.removeAttribute("acornsnap.principal");
  response.sendRedirect("../../index.jsp");
}

How to send an e-mail.

Acornsnap includes JavaMail library. You can use it directly. And also you can use Mailer class as followings. It helps you to use JavaMail API.

Mailer mailer = new Mailer(application);
// Constructs a message.
MimeMessage message = mailer.createMessage();
message.setFrom(new InternetAddress("foo1@bar.com"));
message.setRecipient(RecipientType.TO, new InternetAddress("foo2@bar.com"));
message.setSubject("Greetings","iso-2022-jp");
message.setText("Hello. How are you?");
// Send a message.
mailer.send(message);

How to set timer task.

Acornsnap has ScheduledExecutorService's instance in the application attrubute. It can be used in your own ServletContextListener.

public void contextInitialized(ServletContextEvent sce) {
  final ServletContext sc = sce.getServletContext();
  ScheduledExecutorService ses = (ScheduledExecutorService) sc.getAttribute("acornsnap.scheduled_executor");
  
  Runnable r = new Runnable() {
    @Override
    public void run() {
      sc.log("Scheduled task processing ..");
    }
  };
  // Invoke at every AM03:00.
  Calendar c = Calendar.getInstance();
  c.add(Calendar.DATE, 1);
  c.set(Calendar.HOUR_OF_DAY, 3);
  c.set(Calendar.MINUTE, 0);
  c.set(Calendar.SECOND, 0);
  c.set(Calendar.MILLISECOND, 0);
  long first_time = c.getTimeInMillis() - System.currentTimeMillis();
  long one_day = 24 * 60 * 60 * 1000;
  ses.scheduleAtFixedRate(r, first_time, one_day,TimeUnit.MILLISECONDS);
}

How to record logs.

public void logout() throws Exception{
  // in your controller.
  log("Message.");
}

How to determine downloaded file's name.

Use Content-Disposition header.

  String name = "foo.txt";
  response.setHeader("Content-Disposition", "filename=\"" + name + "\"");
Managed by LumberMill, Inc.