February 3, 2011

Part 4: Creating a Test Servlet

If you've been following Part 3 of this tutorial, you are on your way to creating a Java Web Application using MyBatis,MySQL and Tomcat in Eclipse.

Creating a test Servlet

So far we have created the MySQL database for the ModelingAgency application in Part 2 and we have setup the ModelingAgency project in Eclipse in Part 4, but we haven't used any Servlets yet.

Even if you're not familiar with the MVC model (which you should be if you're reading this tutorial), if you're using plain old Servlets these days it's typical to use them as a Controller.

In other words as a java classes responsible for reading request parameters, performing business logic, and passing on the necessary data to a view (eg. a jsp file) in the form of request parameters.

If all this sounds confusing to you, and you're considering learning Java Web Application Development, I would strongly recommend grabbing a copy of Head 1st Servlets.

To take our application to the next level, and help you understand servlets better, we're going to create a test servlet.

The Servlet

Create a new java class called TestServlet.java under the com.modelingagency.servlets package:

package com.modelingagency.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class TestServlet  extends HttpServlet{

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
  HttpSession session = request.getSession();   
  ServletContext sc = getServletContext();
  request.setAttribute("message","This is a message from the TestServlet. It works!"); 
  RequestDispatcher view = request.getRequestDispatcher("index.jsp");
  view.forward(request, response); 
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

We're going to use this servlet to display a simple text string to make sure things are working, before moving forward.

Don't forget to configure your servlet in the deployment descriptor, so the webapp knows how to 'serve' it.

Your web.xml file should now look like this:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
      <description>DB Connection</description>


Before we re-start our server and run our application again to see if all this works, don't forget to modify index.jsp so it will display the attribute you set in the servlet

The index.jsp file should now look like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <title>Modeling Agency Index</title>

Things are looking good.


If you run the application again, and go the the servlet url that we setup in the deployment descriptor, http://localhost:8080/ModelingAgency/test.do you should see the output:

Things are looking good.
This is a message from the TestServlet. It works!

What next

If you got so far, you are well on your way to setting up a web application using MySQL,MyBatis and Tomcat using Eclipse. Before we get to the MyBatis part, lets see how to setup logging for the ModelingAgency web application (or any java web application for that matter).

Part 5: Setting up Logging