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
NumberBean.java
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/
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
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]$
NumberBean.java
RandomNumberServlet.javapackage 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; } }
RandomNum.jsppackage 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); } }
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/
MVC Without Framework: JSP and Servlets Integration