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. Bill has posted 12 posts at DZone. You can read more from them at their website. View Full User Profile

Tutorial covering JSP 2.2 and Servlets 3.0 with OpenSource Resin Servlet Container: Part 1

10.25.2012
| 25787 views |
  • submit to reddit
...

@WebServlet("/book/")
public class BookListServlet extends HttpServlet {

This WebServlet annotation is a new feature of Servlets 3.0, prior to this, all Servlet configuration went in WEB-INF/web.xml. Anything that avoids a lot of XML configuration is a good thing. Also, the annotation puts the configuration near the things it configuring making it easier to read and more cohesive to understand.

This Servlet is going to use the repository model object to look up a list of BookRepository books. We used Java dependency injection to inject the BookRepository into the Servlet with the @Inject annotation as follows:

	@Inject
	private BookRepository bookRepo;

The Servlet is typically used as a controller. It talks to the model (BookRepository to get a list of Book),

The doGet method gets called when somebody loads the page from the browser and corresponds to the HTTP GET.

The doGet method uses the request object (HttpServletRequest request) to put the list of books into request scope using setAttribute as follows:

...

	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);
	}

 

Java EE / Servlets scopes (page, request, conversation, session, application)

Servlets and Java EE have various scopes as follows (from least to greatest): page, request, conversation, session, application. Page scope is around for just for that page (10 to 200 miliseconds). Request scope is around for one HTTP request (1/2 second to several seconds). Session scope is around for the entire user session (5 minutes to 90 minutes). Conversation scope is around for one workflow (user registration, shopping cart, etc.). Application scope is around from the time that the application server starts the web application until it shuts it down (days, months, years, good for helper classes and reference/config data). Scopes are buckets to put your objects in. When the scope lifecycle ends, it gets rid of all the objects in the scope. This allows you to put objects in a location where multiple resources (Servlets, JSPs, Tag files, etc.) can access them to render pages.

By putting the books list into request scope, we make it available for the JSP page to access the book list to render the book listing. You could use the response object (HttpServletResponse response), and render the listing directly, but the code would be ugly and hard to change the HTML. Instead, we are going to dispatch the request to the JSP to actually render the listing.

After the doGet method puts books into request scope, it forwards the rest of the rendering to the book-list.jsp as follows:

		getServletContext().getRequestDispatcher("/WEB-INF/pages/book-list.jsp").forward(request, response);

The book-list JPS uses Unified EL and JSTL to render the books as HTML to the end user.

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

 

AttachmentSize
Tutorial covering JSP 2.2 and Servlets 3.0 with OpenSource Resin Servlet Container - Part 1.pdf849.79 KB
Published at DZone with permission of its author, Bill Digman.

(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*

Keenee Madison replied on Thu, 2013/10/31 - 6:33am

JSP technology enables rapid development of web-based applications that are server- and platform-independent.  

WordPress Plugin development

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.