Januari 05, 2010

MVC Without Framework: JSP and Servlets Integration

Introduction

This post is used to showcase a MVC pattern for web development without framework. Its functionality is display a random number which is set by a bean and populated by a servlet. I use the source code from book “Core Servlets and Java Server Pages” – chapter 15, which is available online at http://pdf.coreservlets.com/. Credits should goes to Marty Hall and Larry Brown, I only make some minor replacement (for Tomcat 6.0.20) and more details instruction for the shake of my student’s clearlyness.

Directory Structures and Files

[bpdp@bpdp-arch webapps]$ tree mvc/
mvc/
`-- WEB-INF
 |-- classes
 |   |-- beans
 |   |   |-- NumberBean.class
 |   |   `-- NumberBean.java
 |   `-- servlets
 |       |-- RandomNumberServlet.class
 |       `-- RandomNumberServlet.java
 |-- views
 |   `-- RandomNum.jsp
 `-- web.xml
5 directories, 6 files
[bpdp@bpdp-arch webapps]$
Files

NumberBean.java



package beans;

public class NumberBean {

 private double num = 0;

 public NumberBean(double number) {
   setNumber(number);
 }

 public double getNumber() {
   return(num);
 }

 public void setNumber(double number) {
   num = number;
 }

}
RandomNumberServlet.java

package servlets;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import beans.NumberBean;

/** Servlet that generates a random number, stores it in a bean,
 * and forwards to JSP page to display it.
 */

public class RandomNumberServlet extends HttpServlet {
  public void doGet(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, IOException {

    NumberBean bean = new NumberBean(Math.random());
    request.setAttribute("randomNum", bean);
    String address = "/WEB-INF/views/RandomNum.jsp";
    RequestDispatcher dispatcher = request.getRequestDispatcher(address);
    dispatcher.forward(request, response);
  }
}
RandomNum.jsp



 
web.xml




From these source code, you should know that I have prepare directory for views (only for JSP), servlets (all servlets), beans (all beans). That way, we can separate our application into 3 functionalities: Model (beans) – View (JSP) – and Controller (servlets). This will keep your mind stay organized.

Run Application

Execute the application by typing its URL: http://server:8080/mvc/randomize. Here’s the screenshot:









Notes:

If you want to copy, the source code, have a look here: http://bambangpdp.wordpress.com/2010/01/06/mvc-without-framework-jsp-and-servlets-integration/

Quick Hack for the Impatients: JSP Custom Tag

Introduction

Source code from this posting was taken from http://java.sun.com/developer/technicalArticles/xml/WebAppDev3/ with some minor replacement needed for Apache Tomcat 6.0.20 (latest as of this date) and more details on how to run the application.

Purpose of Application

This application is used as a showcase for jsp custom tag. Its functionality is create custom tag for jsp to display string in lowercase.

Directory Structure and Files

[bpdp@bpdp-arch webapps]$ tree customtags/
customtags/
|-- WEB-INF
|   |-- classes
|   |   `-- tags
|   |       |-- ToLowerCaseTag.class
|   |       `-- ToLowerCaseTag.java
|   |-- mytaglib.tld
|   `-- web.xml
`-- coba.jsp

3 directories, 5 files
[bpdp@bpdp-arch webapps]$
We created customtags directory under webapps (which is needed for an application to be executed by Tomcat).  Pay attention to file mytaglib.tld, this file is used as tag lib descriptor to describe descriptor for taglib.


Files

ToLowerCaseTag.java

package tags;

import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

public class ToLowerCaseTag extends BodyTagSupport {

   public int doAfterBody() throws JspException {
      try {
         BodyContent bc = getBodyContent();
         // get the bodycontent as string
         String body = bc.getString();
         // getJspWriter to output content
         JspWriter out = bc.getEnclosingWriter();
         if(body != null) {
            out.print(body.toLowerCase());
         }
      } catch(IOException ioe) {
         throw new JspException("Error: "+ioe.getMessage());   
      }
      return SKIP_BODY;
   }
}
web.xml




mytaglib.tld



coba.jsp



Notes: 

From  all of the files above, you should know the relationship between taglib uri="mytags" prefix="first"  (in coba.jsp) with mytags (in web.xml).

I used screenshot and not embedded code because this post interpret tags as HTML and keep yelling about error, so if you want to copy the source code, have a look here: http://bambangpdp.wordpress.com/2010/01/06/217/

Run Application

To run this application, type the URL: http://server:8080/customtags/coba.jsp
Here's the screenshot:


Januari 03, 2010

Configuring mutt for IMAPS


Here’s a trivial guide for desperately busy people to start using mutt, yes the mail client that suck less. If you like simplicity and more on functionalities than eye candy, this should be your mail client (and who the hell are you to ask me to do that? — you said :p).

Ok, to begin, you should know your user name, IMAP server, and SMTP server details. This should not be difficult since you should have this first place when you got your mail hosted somewhere else (for example, you may login to cpanel and than get this information from your e-mail account.
Firstly, the configuration file. Mutt uses $HOME/.muttrc as the default configuration file. There are some configuration. Here’s mine (this is not complete .muttrc, I put this only the parts which is useful for this guide):
# put your email address here
set from="bpdp@contextualmind.net"
set use_from=yes
set envelope_from="yes"

set editor=vim

set folder="imaps://username@imapserver"
set spoolfile="imaps://username@imapserver/INBOX"

set imap_pass="putpasswordhere"

set smtp_url="smtp://username@server/"
set smtp_pass="putpasswordhere"

bind index 'G' imap-fetch-mail
set mbox="!"
Run mutt from shell and use G key to refresh (just as you have already defined in bind index).

Happy mutt-ing, people :)