pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.in28minutes</groupId>
<artifactId>in28Minutes-first-webapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.6</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>1.9.1</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<verbose>true</verbose>
<source>1.7</source>
<target>1.7</target>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
<contextReloadable>true</contextReloadable>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
src\main\java\in28minutes\login\LoginService.java
package in28minutes.login;
public class LoginService {
public boolean validateUser(String user, String password) {
return user.equalsIgnoreCase("in28Minutes") && password.equals("dummy");
}
}
src\main\java\in28minutes\login\LoginServlet.java
package in28minutes.login;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = "/login.do")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private LoginService service = new LoginService();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String name = request.getParameter("name");
String password = request.getParameter("password");
boolean isValidUser = service.validateUser(name, password);
if (isValidUser) {
request.getSession().setAttribute("name", name);
response.sendRedirect("/todo.do");
} else {
request.setAttribute("errorMessage", "Invalid Credentials!!");
request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response);
}
}
}
src\main\java\in28minutes\todo\DeleteTodoServlet.java
package in28minutes.todo;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = "/delete-todo.do")
public class DeleteTodoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private TodoService todoService = new TodoService();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
todoService.deleteTodo(request.getParameter("todo"));
response.sendRedirect("/todo.do");
}
}
src\main\java\in28minutes\todo\ListTodoServlet.java
package in28minutes.todo;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = "/todo.do")
public class ListTodoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private TodoService todoService = new TodoService();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
request.setAttribute("todos", todoService.retrieveTodos());
request.getRequestDispatcher("/WEB-INF/views/todo.jsp").forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String todo = request.getParameter("todo");
if ("".equals(todo)) {
request.setAttribute("errorMessage", "Enter a valid Todo");
} else {
todoService.addTodo(todo);
}
request.setAttribute("todos", todoService.retrieveTodos());
request.getRequestDispatcher("/WEB-INF/views/todo.jsp").forward(request, response);
}
}
src\main\java\in28minutes\todo\Todo.java
package in28minutes.todo;
public class Todo {
public Todo(String name) {
super();
this.name = name;
}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Todo [name=" + name + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Todo other = (Todo) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
src\main\java\in28minutes\todo\TodoService.java
package in28minutes.todo;
import java.util.ArrayList;
import java.util.List;
public class TodoService {
private static List<Todo> todos = new ArrayList<Todo>();
static {
todos.add(new Todo("Learn Web Application"));
todos.add(new Todo("Learn Spring"));
todos.add(new Todo("Learn Spring MVC"));
}
public List<Todo> retrieveTodos() {
return todos;
}
public void addTodo(String todo) {
todos.add(new Todo(todo));
}
public void deleteTodo(String todo) {
todos.remove(new Todo(todo));
}
}
src\main\webapp\WEB-INF\views\login.jsp
<html>
<head>
<title>Yahoo!!</title>
</head>
<body>
<p><font color="red">${errorMessage}</font></p>
<form action="/login.do" method="POST">
Name : <input name="name" type="text" /> Password : <input name="password" type="password" /> <input type="submit" />
</form>
</body>
</html>
src\main\webapp\WEB-INF\views\todo.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<title>Yahoo!!</title>
<!-- Bootstrap core CSS -->
<link href="webjars/bootstrap/3.3.6/css/bootstrap.min.css"
rel="stylesheet">
</head>
<body>
<H1>Welcome ${name}</H1>
<div>
Your Todos are
<ol>
<c:forEach items="${todos}" var="todo">
<li>${todo.name} <a href="/deletetodo.do?todo=${todo.name}">Delete</a></li>
</c:forEach>
</ol>
<p>
<font color="red">${errorMessage}</font>
</p>
<form method="POST" action="/todo.do">
New Todo : <input name="todo" type="text" /> <input name="add"
type="submit" />
</form>
</div>
<script src="webjars/jquery/1.9.1/jquery.min.js"></script>
<script src="webjars/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</body>
</html>
src\main\webapp\WEB-INF\web.xml
<!-- webapp/WEB-INF/web.xml -->
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>To do List</display-name>
<welcome-file-list>
<welcome-file>login.do</welcome-file>
</welcome-file-list>
</web-app>