Beer Selector

I originally wrote this post when I was spending some time in my evenings reading again Head First Servlet and JSP, as a way to get back in touch with JavaEE. Here I am dealing with chapter three, where a little application is designed and developed, as a way to show the Model-View-Controller (MVC) pattern in action.

The application is about beer selection. The user would set his preferences and ask the application to search in the database for beer matching the requirements and show him the result.
  • View: We'll write an HTML page for get input, then the user would click a button to submit a post request. The application will use a JSP page to show the output to the user.
  • Controller: A servlet will receive the post request from the HTML page and, after some work, would generate the output JSP page.
  • Model: Some Java code would take care of providing the data output based on the input retrieved from the user.

That's simple enough, but here we are going to implement a first version, even simpler. No Model, and the servlet in the Controller would just echo the input received from the View.

BeerSelector.html is the user input manager:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <title>Beer Selector</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <h1 align="center">Beer Selection Page</h1>
        <form method="POST" action="SelectBeer.do">
            <p>Select beer characteristics</p>
            Color:
            <select name="color">
                <option value="light">light</option>
                <option value="amber">amber</option>
                <option value="brown">brown</option>
                <option value="dark">dark</option>
            </select>
            <p><input type="SUBMIT"></p>
        </form>
    </body>
</html>
All you need to know is a bit of HTML to get what is going on here. If basic HTML is not an issue (otherwise I'm sorry to say that you are in big trouble), you should quite easily see that we have a form that generates a call to the HTTP post method for the processing agent "SelectBeer.do" (our servlet, as we'll see in a moment).
To actually send the request, the user should click on the button that is rendered by the input control of submit type we have placed at the end of the form.
The select control determines the information that we are sending: the parameter name is "color" and the value is the value of the option currently selected when the submit button is clicked.

What we specified in the html file as processing agent, is the logical name of our servlet, SelectBeer.do, we'll use it as "url-pattern" in the DD (deployment descriptor, web.xml) to define our servlet besides its servlet-name and servlet-class.

Notice that in the action tag of the form method we specify the URL relative to the current path for the HTML referring to it. If we wanted to mark this more explicitely, we could have written "./SelectBeer.do".

In any case, this is what we are going to add to the DD file:
<servlet>
    <servlet-name>Beer Selector</servlet-name>
    <servlet-class>ch03.BeerSelector</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Beer Selector</servlet-name>
    <url-pattern>/SelectBeer.do</url-pattern>
</servlet-mapping>
I haven't actually done that explicitely, since I'm working with NetBeans, but I filled the information required to create a new servlet in the project, giving as class name "ch03.BeerSelector", servlet name "Beer Selector", and URL pattern "/SelectBeer.do". Notice the slash in the URL pattern.

NetBeans generated for me the servlet skeleton, and I just completed the processRequest() method in this way:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet BeerSelector</title>");  
        out.println("</head>");
        out.println("<body>");
        out.println("Beer Selection Advice");
        out.println("<p>Got beer color " + request.getParameter("color") +"</p>");
        out.println("</body>");
        out.println("</html>");
    } finally { 
        out.close();
    }
}
After that, we just have to compile, generate the war, deploy it to Tomcat, and test the result.

What we expect to get is a page, BeerSelector.html, in the base directory of our application. Selecting an item in the drop-down list and clicking the button we should get back another HTML file (named SelectBeer.do) that gives us as advice just the value of the item we selected.

No comments:

Post a Comment