Developing Web Applications With Ant by Richard Hightower - HTML preview
Download the book in PDF, ePub, Kindle for a complete version.
HelloWorldServlet.java
The servlet is contained in the class xptoolkit.web.HelloWorldServlet (see Listing 20.22). Like the Java application, it uses the Greeting interface and the GreetingFactory class that are packaged in greetmodel.jar, the output of the model project.
package xptoolkit.web;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.RequestDispatcher;
/* import the classes to create a greeting object or type greeting */ import xptoolkit.model.GreetingFactory;
import xptoolkit.model.Greeting;
/* Read in the greeting type that the factory should create */ String clazz = config.getInitParameter("Greeting.class") ; if(clazz!=null)System.setProperty("Greeting.class",clazz);
} public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException{ RequestDispatcher dispatch;
ServletContext context;
/*Get the session, create a greeting bean, map the greeting bean in the session, and redirect to the Hello World JSP.
*/
try {
/* Create the greeting bean and map it to the session. */
HttpSession session = request.getSession(true); Greeting greet = (Greeting)
/* Redirect to the HelloWorld.jsp */
context = getServletContext();
dispatch = context.getRequestDispatcher("/HelloWorldJSP"); dispatch.forward(request, response);
}catch(Exception e){
throw new ServletException(e);
}
}
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException{ doGet(request, response);
}
} Listing 20.22
