In this blog I'll explain with a simple example about creating a Java Web Application using MVC pattern. I'm using a simple java program as model, JSP as view and Servlet as controller.
This application is developed using eclipse (Juno) IDE, jdk1.6.0_43 and it is hosted on Apache tomcat 7.0.
Here is the definition from Wiki.
Model–view–controller (MVC) is a software architecture pattern which separates the representation of information from the user's interaction with it.
The model consists of application data, business rules, logic, and functions.
A view can be any output representation of data, such as a chart or a diagram. Multiple views of the same data are possible, such as a bar chart for management and a tabular view for accountants. The controller mediates input, converting it to commands for the model or view.
FindMonth is my first simple java webapp
How it looks?
The files ticked in
RED font are the files created by me for this project.
- NumberToMonth.java acts as Model
- SelectMonth.java is the Servlet
- FindMonth.html is the home page
- result.jsp is the View
- web.xml is a web descriptor file which links the Homepage to Servlet
Code
Model: NumberToMonth.java
I have created a package blog.sliceoffcodes.model which holds the file NumberToMonth.java
- Right click on src (under Java Resources) -> New -> Package -> Provide package name
Create a java file
- Right click on above created package -> New -> Class -> provide name NumberToMonth
package blog.sliceoffcodes.model;
public class NumberToMonth {
int month;
String monthString;
public NumberToMonth(int month) {
super();
this.month = month;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public String getMonthString() {
return monthString;
}
private void setMonthString(String monthString) {
this.monthString = monthString;
}
public void FindMonth() {
switch (getMonth()) {
case 1:
setMonthString("January");
break;
case 2:
setMonthString("February");
break;
case 3:
setMonthString("March");
break;
case 4:
setMonthString("April");
break;
case 5:
setMonthString("May");
break;
case 6:
setMonthString("June");
break;
case 7:
setMonthString("July");
break;
case 8:
setMonthString("August");
break;
case 9:
setMonthString("September");
break;
case 10:
setMonthString("October");
break;
case 11:
setMonthString("November");
break;
case 12:
setMonthString("December");
break;
default:
setMonthString("Invalid");
break;
}
}
}
Controller (Servlet): SelectMonth.java
I have created a package blog.sliceoffcodes.web which holds the file SelectMonth.java
- Right click on src (under Java Resources) -> New -> Package -> Provide package name
Create a java file
- Right click on above created package -> New -> Servlet -> provide name SelectMonth.java
package blog.sliceoffcodes.web;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import blog.sliceoffcodes.model.NumberToMonth;
public class SelectMonth extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String m = request.getParameter("inputMonth");
/* response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Month Input = " + m); */
NumberToMonth nm = new NumberToMonth(Integer.parseInt(m));
nm.FindMonth();
String result = nm.getMonthString();
//out.println("Month output = " + result);
request.setAttribute("monthExtract", result);
RequestDispatcher view = request.getRequestDispatcher("result.jsp");
view.forward(request, response);
}
}
view (Home page): FindMonth.html
- Right click on WebContent -> New -> HTML file -> provide name FindMonth.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Find Month</title>
</head>
<body>
<h1 align="center">Find Month</h1>
<form method="POST" action="FetchMonth.do">
Select Month Type: <select name="inputMonth" size=1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select> <br>
<br>
<center>
<input type="Submit">
</center>
</form>
</body>
</html>
view (jsp): result.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.*" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Result of Month Finder </title>
</head>
<body>
<h1 align="center">Month</h1>
<p>
<%
String month = (String) request.getAttribute("monthExtract");
out.print("<b> Month in English: " + month);
%>
<br> <br>
<A HREF="FindMonth.html" align="center">HOME</A>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>FindMonth</display-name>
<welcome-file-list>
<welcome-file>FindMonth.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Month</servlet-name>
<servlet-class>blog.sliceoffcodes.web.SelectMonth</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Month</servlet-name>
<url-pattern>/FetchMonth.do</url-pattern>
</servlet-mapping>
</web-app>