Tutorial covering JSP 2.2 and Servlets 3.0 with OpenSource Resin Servlet Container: Part 1
Creating your first Servlet
Eclipse: Right Click "Java Resources" in Project Explorer -> New -> Servlet ->
Package = com.bookstore.web -> Class Name = BookListServlet -> Click Next ->
Remove URL mapping, create new mapping /book/
(That is the last time I will tell you how to create a Servlet in Eclipse.)
Note: This application uses the REST style URL mappings so things that end in / imply you are working with a list (like a directory of files). Thus the URI /book/ implies a collection of books since we want to show a list of books this is a good URI.
Modify the Servlet to only handle the doGet method, change the doGet method to forward to a JSP that we have not created yet that lives in WEB-INF.
BookListServlet listing
package com.bookstore.web;
import java.io.IOException;
import javax.inject.Inject;
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 com.bookstore.BookRepository;
@WebServlet("/book/")
public class BookListServlet extends HttpServlet {
@Inject
private BookRepository bookRepo;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("books", bookRepo.listBooks());
getServletContext().getRequestDispatcher("/WEB-INF/pages/book-list.jsp").forward(request, response);
}
}
WEB-INF is a meta directory folder for war files. JSPs in the WEB-INF folder can never be loaded directly from a browser. This allows us to force all JSPs to first go through our Servlet tier (controllers) which is essential for a model 2 architecture (a form of MVC tailored for HTTP applications), which we will use throughout the tutorial. Consider putting JSPs in WEB-INF a best practice.
The annotations @WebServlet("/book/") allow us to map this Servlet to handle requests for the URI /book/ as follows:
Bill Digman is a Java EE / Servlet enthusiast and Open Source enthusiast who loves working with Caucho's Resin Servlet Container, a Java EE Web Profile Servlet Container.
Caucho's Resin OpenSource Servlet Container
Java EE Web Profile Servlet Container
| Attachment | Size |
|---|---|
| Tutorial covering JSP 2.2 and Servlets 3.0 with OpenSource Resin Servlet Container - Part 1.pdf | 849.79 KB |
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)





Comments
Bruce Phillips replied on Thu, 2012/10/25 - 11:00am
This is a good article but it could be made much, much better by providing two items
1. Code download
2. PDF version that a reader can save to read later or to refer to when needed. It is very tedious to click through 15 web pages where on each page the ads and other crap overwhelm the important content.
Bill Digman replied on Thu, 2012/10/25 - 3:12pm
in response to:
Bruce Phillips
I'll work on a PDF version right away.
How do you suggest I provide code download?
Bill Digman replied on Thu, 2012/10/25 - 4:29pm
in response to:
Bill Digman
The PDF version is attached
Bruce Phillips replied on Thu, 2012/10/25 - 7:02pm
in response to:
Bill Digman
Thank you very much for the PDF version.
For the code you could use Eclipse - Export as archived projected which creates a zip of your project. Other Eclipse users can then import the project directly from the zip. Non-Eclipse users can unzip the project and view the files also.
Henk De Boer replied on Fri, 2012/10/26 - 12:11pm
It's thorough and explains things well, but it's a somewhat confusing setup.
The material being explained; Servlets, JSP and then Scriptlets vs JSTL feels like it comes straight from the early 2000s, but then we do see relative modern annotations on the Servlets and even the use of CDI.
Bill Digman replied on Thu, 2012/11/01 - 11:38am
in response to:
Henk De Boer
Yep... This is true. In a future article we are going to cover a more CDI centric approach covering @Produces @RequestScoped. Later we will cover JPA, JAX-RS, JCache, etc. and yes maybe even JSF (grumble, grumble)....
Bill Digman replied on Thu, 2012/11/01 - 11:39am
Stay tuned. I have a lot more to say. I think Java EE is more of a cafeteria plan than a one size fits all. You can use Servlets, CDI and JSP for model 2 apps. JAX-RS for RIA clients. Websockets, JCache, etc. Most tutorials wrt Java EE seem to really push JSF and EJB. If you tie Java EE to JSF and EJB and I think it turns a lot of folks off. Java EE is more than this.
Sam Noor replied on Fri, 2012/12/21 - 4:38am
I just found this blog and have high hopes for it to continue. Keep up the great work, its hard to find good ones. I have added to my favorites. Thank You. http://www.ileatherwear.com !! *Leather outfit* http://www.ileatherwear.com/offer/wwii-bomber-jacket.html !! *Wwii bomber jacket* http://www.ileatherwear.com/offer/white-leather-jackets-for-women.html !! *White leather jackets for women* http://www.ileatherwear.com/offer/tan-leather-jacket-women.html !! *Tan leather jacket women*