![]() STEP 5 (Servlet) Now at last we are in the most important part of this tutorial, the Servlet. In this step, create a new package in the "src" folder named "controllers" and inside create a new class named "ItemController". Servlets are a special class that extends from "HttpServlet". Servlets are capable of working with Http Request and response data. This is what in php looks like $_GET and $_POST. (I point this so php developers can understand it better.) Here is our servlet. We are going to use the do_Post() function because our html form uses the "POST" method.
package controllers;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.Item;
public class ItemController extends HttpServlet {
private static final long serialVersionUID = 1L;
public ItemController() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String todo = request.getParameter("todo"); //we get the post field named "todo" from the form data
Item item = new Item(); //we create a new model object
//the dispatcher will let us send a request to the "result.jsp" file
RequestDispatcher dispatcher = request.getRequestDispatcher("result.jsp");
if (item.insert(todo)) //we try to insert the data in the database
{
request.setAttribute("insert", "true");
}else{
request.setAttribute("insert", "false");
}
item.disconnect();
//the forward method actually send the request with all the request//attributes in it.
dispatcher.forward(request, response);
}
}
I commented the code so you can understand. Whats happening is that the servlet is getting the POST data from the form. With request.getParameter("name_of_field") we can fetch the POST data related to that field in the form. Then we use our model to insert data and if the insertion was done, then we create an attribute that we will later use in the JSP file. After that we disconnect from database and send a request to "result.jsp". STEP 6 (Modify result.jsp and index.jsp) Here is how the result.jsp file will look. We will fetch the Attribute we set in the servlet to see if the insertion was done:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%if (request.getAttribute("insert").equals("true"))
{ %>
<h2>Item added to Todo-List</h2>
<%}else{%>
<h2>There was a problem and the item wasn't added. Please try again</h2>
<%}%>
<!-- This will always show -->
To return to the main page click <a href="index.jsp">Here!</a>
</body>
</html>
And for the index.jsp form we will add the action value. To see what will be the servlet url go to the "webcontent" folder then into "WEB-INF" and open the "web.xml" file and search for this:
<servlet> <description></description> <display-name>ItemController</display-name> <servlet-name>ItemController</servlet-name> <servlet-class>controllers.ItemController</servlet-class> </servlet> <servlet-mapping> <servlet-name>ItemController</servlet-name> <url-pattern>/ItemController</url-pattern> </servlet-mapping> The mapping says the controller url is: "/ItemController" you can change that. For example i will put "addItem". It will look like this: <servlet> <description></description> <display-name>ItemController</display-name> <servlet-name>ItemController</servlet-name> <servlet-class>controllers.ItemController</servlet-class> </servlet> <servlet-mapping> <servlet-name>ItemController</servlet-name> <url-pattern>/addItem</url-pattern> </servlet-mapping> Now in index.jsp file just put as the form's action: "addItem" (This is because I changed it to that, if you didnt change it you'll use "ItemController" or any value you put there). <%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="models.Item"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Todo-List App</title>
</head>
<body>
<h2>Add a new item</h2>
<form action="addItem" method="post">
<table>
<tr>
<td><input type="text" value="I have to..." onClick="this.value('')" name="todo"/></td>
<td><input type="submit" value="Send" name="send"/> </td>
</tr>
</table>
</form>
<h2>Todo-List</h2>
<%
Item item = new Item();
String[] lastest = item.lastItems();
for(int i=0;i<lastest.length;i++)
{%>
<%=lastest[i] %> <br/>
<%}item.disconnect();
%>
</body>
</html>
Conclusions This is a complete yet simple tutorial on how Servlets work and how we can implement the MVC pattern using them. You are free to leave comments and suggestions or give some P2L Credits. See you next time. Lex0712
|




















