Java Servlet 3.0 (JSR 315), which is part of Java EE 6, is a major revision of the Java Servlet technology, a widely accepted technology for building dynamic content for web-based applications, and includes changes to enable easy pluggability of popular open-source frameworks or libraries, ease of development leveraging annotation and making web.xml optional, support for async processing, which enables writing Comet applications in portable fashion, security enhancements, and other minor updates to the existing APIs.
In this lab, you are going exercise all the newly introduced features of Servlet 3.0.
Software Needed
Before you begin, you need to install JDK 6 and NetBeans IDE 6.8. You also need to download and unzip the hands-on lab zip file below.
- 4532_javaee6_servlet3.0.zip (download)
- It contains this document and the lab contents
- Download it and unzip in a directory of your choice
OS platforms you can use
- Windows
- Solaris x86, Solaris Sparc
- Linux
- Mac OS X
Lab Exercises
For the sake of the simplicity of the lab, most exercises are provided in the form of “ready-to-open-and-run” NetBeans projects. (Many of them are borrowed from “glassfish-samples” and “Java EE 6 tutorial“.) Please feel free to create them from scratch if you want to.
It is strongly encouraged, leveraging what is provided in this lab, you do your own experimentation meaning creating/adding your own code as much as you can.
If you have written some code that might help everyone else, please feel free to share them on this codecamp email alias or directly send them to the instructors. Your name will be recognized in this lab if your sample code is chosen to be included. For the tasks that need to be done, please see the “Things to be done” section above.)
- Exercise 1: Annotation (20 minutes)
- Exercise 2: Dynamic registration (20 minutes)
- Exercise 3: Pluggability of 3rd-party frameworks/libraries with “web-fragment.xml” (20 minutes)
- Exercise 4: Resources in a bundled jar (20 minutes)
- Exercise 5: Asynch. Servlet (60 minutes)
- Exercise 6: Multi-part (20 minutes)
- Exercise 7: Session cookle configuration (20 minutes)
- Exercise 8: Security (30 minutes)
- Homework Exercise
Exercise 1: Annotation
In this exercise, you are going to learn how to use annotations to define a Servlet, Filter, thus eliminating the need of the web.xml.
(1.1) Open, build, and run “annotation-war” sample application (from “glassfish-samples”)
0. Start NetBeans IDE.
1. Open annotation-war NetBeans project.
- Select File->Open Project (Ctrl+Shift+O).
Figure-1.11
- Observe that the Open Project dialog box appears.
- Browse down to <GlassFish-v3-Installation-Directory>/glassfish/samples/javaee6/web/servlet directory.
- Select annotation-war.
- Click Open Project.
Figure-1.12
- Observe that the annotation-war project node appears under Projects tab window.
2. Build and run annotation-war project.
- Right-click annotation-war project and select Run.
Figure-1.13
- Observe that the default browser gets displayed with a result.
Figure-1.14
(1.2) Study the source code
- The @WebServlet annotation is used to define a Servlet.
- The only mandatory attribute is urlPatterns attribute.
Figure-1.21
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only (“GPL”) or the Common Development * and Distribution License(“CDDL”) (collectively, the “License”). You * may not use this file except in compliance with the License. You can obtain * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific * language governing permissions and limitations under the License. * * When distributing the software, include this License Header Notice in each * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt. * Sun designates this particular file as subject to the “Classpath” exception * as provided by Sun in the GPL Version 2 section of the License file that * accompanied this code. If applicable, add the following below the License * Header, with the fields enclosed by brackets [] replaced by your own * identifying information: “Portions Copyrighted [year] * [name of copyright owner]” * * Contributor(s): * * If you wish your version of this file to be governed by only the CDDL or * only the GPL Version 2, indicate your decision by adding “[Contributor] * elects to include this software in this distribution under the [CDDL or GPL * Version 2] license.” If you don’t indicate a single choice of license, a * recipient has the option to distribute your version of this file under * either the CDDL, the GPL Version 2 or to extend the choice of license to * its licensees as provided above. However, if you add GPL Version 2 code * and therefore, elected the GPL Version 2 license, then the option applies * only if the new code is made subject to such option by the copyright * holder. */ package web.servlet.annotation_war; import java.io.IOException; import javax.servlet.ServletConfig; /** @Override @Override PrintWriter writer = res.getWriter(); |
TestServlet.java
2. Display and study the context sensitive Javadoc on @WebServlet annotation. In NetBeans IDE, you can display the Javadoc of a class by right clicking it and selecting Show Javadoc.
- Move the cursor to the @WebServlet(..), right click it, and select Show Javadoc.
Figure-1.22
- Observe that the Javadoc of the @WebServlet annotation class gets displayed.
Figure-1.23
3. Study TestFilter.java.
package web.servlet.annotation_war;
import java.io.IOException; import javax.servlet.Filter; /** public void init(FilterConfig filterConfig) throws ServletException { public void doFilter(ServletRequest req, ServletResponse res, req.setAttribute(“filterMessage”, mesg); public void destroy() { |
TestFilter.java
- Study the @WebFilter annotation by moving the cursor to @WebFilter(..), right clicking it, and selecting Show Javadoc.
4. Study TestServletContextListener.java.
package web.servlet.annotation_war;
import javax.servlet.ServletContext; /** public void contextDestroyed(ServletContextEvent sce) { |
TestServletContext.java
- Study the @WebListener annotation by moving the cursor to @…WebListener(..), right clicking it, and selecting Show Javadoc.
5. Observe that there is no web.xml file.
- There is no need for web.xml since every configuration can be done through annotation.
Figure-1.24
(1.3) Add your own servlet
Figure-1.31
- For the Class Name field, enter MyOwnServlet.
- Click Next.
Figure-1.32
- Observe that you may register the servlet by checking the Add information to deployment descriptor (web.xml). Do not check it for this exercise.
- Click Finish.
Figure-1.32b
Tip: Note that you may use code completion via the Source->Complete code… menu or via the corresponding shortcut (which you may adapt in Preferences if it is already used system-wide).
package web.servlet.annotation_war;
import java.io.IOException; /** /** // <editor-fold defaultstate=”collapsed” desc=”HttpServlet methods. Click on the + sign on the left to edit the code.”> /** /** } |
MyOwnServlet .java
Figure-1.33
3. Build and run.
- Right click annotation-war project node and select Run.
- Observe that the default browser gets displayed with a result.
- Change the URL to http://localhost:8080/annotation-war/MyOwnServlet – add /MyOwnServlet at the end – and click Return.
- Observe that “My message is passion” message gets displayed.
Figure-1.34
Summary
In this exercise, you have learned how to use @WebServlet for defining a servlet, and @WebFilter for defining a filter.
Exercise 2: Dynamic registration
Servlet 3.0 provides enhanced Pluggability where it provides options for adding servlets, filters, servlet mappings and filter mappings during run time.
In this exercise, you are going to learn how to dynamically add a servlet and filter (instead of statically configuring them at the compile time.)
- Open, build, and run “dynamicregistration-war” sample application (from “glassfish-samples”)
- Study the code
- Register your own servlet dynamically
(2.1) Open, build, and run “dynamicregistration-war” sample application (from “glassfish-samples”)
1. Open dynamicregistration-war NetBeans project.
- Select File->Open Project (Ctrl+Shift+O).
- Observe that the Open Project dialog box appears.
- Browse down to <GlassFish-v3-Installation-Directory>/glassfish/samples/javaee6/web/servlet directory.
- Select dynamicregistration-war.
- Click Open Project.
2. Build and run dynamicregistration-war project.
- Right-click dynamicregistration-war project and select Run.
- Observe that the default browser gets displayed with a result.
Figure-2.11
(2.2) Study the code
1. Study TestServlet.java.
package web.servlet.dynamicregistration_war;
import java.io.*; /** @Override // Test if dynamic registration of a servlet with init parameters is // Test if dynamic registration of a filter with init parameters is // Test if dynamic registration of a ServletRequestListener is res.getWriter().println(“HELLO WORLD!\n”); |
TestServlet.java
2. Study TestServletContextListener.java.
package web.servlet.dynamicregistration_war;
import java.util.*; /** @Override ServletContext sc = sce.getServletContext(); // Register Servlet // Register Filter // Register ServletRequestListener @Override |
TestServletContextListener.java
3. TestServletRequestListener.java
package web.servlet.dynamicregistration_war;
import javax.servlet.*; public class TestServletRequestListener implements ServletRequestListener { /** /** } |
TestServletRequestListener.java
(2.3) Register your own servlet dynamically
2. Modify the IDE generated MyOwnServlet2.java as shown below. The code fragments that need to added/modified are highlighted in bold and red-colored font.
package web.servlet.dynamicregistration_war;
import java.io.IOException; /** /** // <editor-fold defaultstate=”collapsed” desc=”HttpServlet methods. Click on the + sign on the left to edit the code.”> /** /** } |
MyOwnServlet2.java
Figure-2.31
2. Modify the TestServletContextListener.java as shown below.
package web.servlet.dynamicregistration_war;
import java.util.*; /** @Override ServletContext sc = sce.getServletContext(); // Register Servlet // Register my own Servlet // Register Filter // Register ServletRequestListener @Override |
TestServletContextListener.java
Figure-2.32
3. Modify TestServlet.java.
package web.servlet.dynamicregistration_war;
import java.io.*; /** @Override if (!”servletInitValue”.equals(getServletConfig().getInitParameter( String initName = getServletConfig().getInitParameter(“mymessage2”); if (!”filterInitValue”.equals(req.getAttribute(“filterInitName”))) { if (!”listenerAttributeValue”.equals(req.getAttribute( res.getWriter().println(“HELLO WORLD!\n”); |
Modified TestServlet.java
Figure-2.32b
4. Build and run.
- Right click the project node and select Run.
- Observe that the default browser gets displayed with a result.
- Change the URL to http://localhost:8080/dynamicregistration-war/myown – add /myown at the end – and click Return.
- Observe that “My message2 is Passion2” message gets displayed.
Figure-2.33
Summary
In this exercise, you have learned how to dynamically add a servlet and filter (instead of statically configuring them at the compile time.)
Exercise 3: Pluggability of 3rd-party frameworks/libraries
- Open, build, and run “absolute-ordering-web-fragments-war” sample application
- Study the code
- Add your own library
(3.1) Open, build, and run “absolute-ordering-web-fragments-war” sample application (from “glassfish-samples”)
1. Open absolute-ordering-web-fragments-war NetBeans project.
- Select File->Open Project (Ctrl+Shift+O).
- Observe that the Open Project dialog box appears.
- Browse down to <GlassFish-v3-Installation-Directory>/glassfish/samples/javaee6/web/servlet directory.
- Select absolute-ordering-web-fragments-war.
- Check Open Required Projects.
- Click Open Project.
Figure-3.12a
- Observe that the absolute-ordering-web-fragments-war project node appears under Projects tab window together with the required projects.
Figure-3.12
2. Build and run absolute-ordering-web-fragments-war project.
- Right-click absolute-ordering-web-fragments-war project and select Run.
- Observe that the default browser gets displayed with a result.
Figure-3.13
(3.2) Study the code
1. Study TestServlet.java.
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only (“GPL”) or the Common Development * and Distribution License(“CDDL”) (collectively, the “License”). You * may not use this file except in compliance with the License. You can obtain * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific * language governing permissions and limitations under the License. * * When distributing the software, include this License Header Notice in each * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt. * Sun designates this particular file as subject to the “Classpath” exception * as provided by Sun in the GPL Version 2 section of the License file that * accompanied this code. If applicable, add the following below the License * Header, with the fields enclosed by brackets [] replaced by your own * identifying information: “Portions Copyrighted [year] * [name of copyright owner]” * * Contributor(s): * * If you wish your version of this file to be governed by only the CDDL or * only the GPL Version 2, indicate your decision by adding “[Contributor] * elects to include this software in this distribution under the [CDDL or GPL * Version 2] license.” If you don’t indicate a single choice of license, a * recipient has the option to distribute your version of this file under * either the CDDL, the GPL Version 2 or to extend the choice of license to * its licensees as provided above. However, if you add GPL Version 2 code * and therefore, elected the GPL Version 2 license, then the option applies * only if the new code is made subject to such option by the copyright * holder. */ package web.servlet.absolute_ordering_web_fragments_war; import java.io.IOException; import javax.servlet.ServletException; @WebServlet(“/”) // Display the value of filterMessage attribute saved in the request scope |
TestServlet.java
2. Study web.xml. The absolute ordering of libraries are specified as a library whose name is A is loaded first, and the one whose name is B is loaded last while all the other libraries are loaded between A and B.
Figure-3.21
3. Observe that three libraries – webfragment1.jar, webfragment2.jar, webfragment3.jar – are configured into the application. They are configured under WEB_INF/lib directory.
- Click Files tab.
- Expand absolute-ordering-web-fragments-war->build->web->WEB-INF->lib.
- Observe that webfragment2.jar and webfragment3.jar have web-fragment.xml files under META-INF directory while webfragment1.jar does not.
Figure-3.22
3. Study the web-fragment.xml of the webfragment2 library.
Figure-3.23
Figure-3.24
<web-fragment xmlns=”http://java.sun.com/xml/ns/javaee” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” version=”3.0″ xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-fragment_3_0.xsd” metadata-complete=”true”> <name>A</name> <filter> <icon/> <filter-name>wf2testfilter</filter-name> <filter-class> web.servlet.absolute_ordering_web_fragments_war.webfragment2.Wf2TestFilter </filter-class> </filter> <filter-mapping> <filter-name>wf2testfilter</filter-name> <url-pattern>/</url-pattern> <dispatcher>REQUEST</dispatcher> </filter-mapping> </web-fragment> |
4. Study the Wf2TestFilter.java of the webfragment2 library.
Figure-3.24
package web.servlet.absolute_ordering_web_fragments_war.webfragment2;
import java.io.IOException; import javax.servlet.Filter; // The Wf2TestFilter is configured in the web-fragment.xml of the webfragment2 library. public void doFilter(ServletRequest req, ServletResponse res, // When doFilter method of this filter is called, add “2” to req.setAttribute(“filterMessage”, filterMessage); public void destroy() { |
5. Study the web-fragment.xml of the webfragment3 library.
Figure-3.26
1. Create a new Java Library project.
Figure-3.31
Figure-3.32
- For the Project Name field, enter my_webfragment (or whatever project name of your choice).
- Click Finish.
Figure-3.33
2. Add a Java class to the project.
Figure-3.34
- For the Class Name field, enter MyFilter (or whatever Class name of your choice).
- For the Package field, enter mypackage (or whatever package name of your choice).
- Click Finish.
Figure-3.35
- Modify the IDE generated MyFilter.java with the code below. This is the same code as webfragment3 library’s Wf3TestFilter.java except the filterMessage is set with ” Passion” as shown below.
package mypackage;
import java.io.IOException; import javax.servlet.DispatcherType; @WebFilter(urlPatterns={ “/” }, dispatcherTypes= { DispatcherType.REQUEST }) public void doFilter(ServletRequest req, ServletResponse res, String filterMessage = (String)req.getAttribute(“filterMessage”); req.setAttribute(“filterMessage”, filterMessage); public void destroy() { |
MyFilter.java
- Observe that there are lots of compile errors.
3. Add Java-EE-GlassFish-v3 library to the my_webfragment project.
Figure-3.36
Figure-3.37
- Observe that all the compile errors are resolved.
4. Add web-fragment.xml to the library.
Figure-3.38
Figure-3.39
Figure-3.40
- Modify the IDE generated web-fragment.xml with the one below. Note that the name of the library is set to Passion.
<?xml version=”1.0″ encoding=”UTF-8″?>
<web-fragment xmlns=”http://java.sun.com/xml/ns/javaee” |
Tip: Alternatively you may enter the name of the library in the Name(s) field of the General tab of the web-fragment.xml file.
Figure-3.40a
5. Build the library.
Figure-3.41
6. Add the Passion library to the absolute-ordering-web-fragments-war application.
Figure-3.42
- Browse down to the directory where you created my_webfragment project – \myprojects in this example.
Figure-3.43
7. Modify the web.xml of the absolute-ordering-web-fragments-war application. The change is to load the Passion library right after A library.
Figure-3.44
Warning: Do not use Ordering in the General part of web-fragment.xml file. It will remove silently the <others/> tag as it has no contents.
8. Run the application.
Figure-3.45
- Observe that the filterMessage attrbute now contains ” Passion”.
Figure-3.46
Summary
In this exercise, you have learned how to add a pluggable framework or library in the form of a jar file and how to define a web fragment file with the name web-fragment.xml in the META-INF directory of the jar file.
Exercise 4: Resources in bundled jar
- Open, build, and run “jsp-resource-in-bundled-jar-war” sample application
- Study the code
- Create a new jar with your own resource
(4.1) Open, build, and run “jsp-resource-in-bundled-jar-war” sample application (from “glassfish-samples”)
1. Open jsp-resource-in-bundled-jar-war NetBeans project.
- Select File->Open Project (Ctrl+Shift+O).
- Observe that the Open Project dialog box appears.
- Browse down to <GlassFish-v3-Installation-Directory>/glassfish/samples/javaee6/web/servlet directory.
- Select jsp-resource-in-bundled-jar-war.
- Click Open Project.
- Observe that the jsp-resource-in-bundled-jar-war project node appears under Projects tab window.
2. Build and run jsp-resource-in-bundled-jar-war project.
- Right-click jsp-resource-in-bundled-jar-war project and select Run.
- Observe that the default browser gets displayed with a result.
Figure-4.11
(4.2) Study the code
Figure-4.21
2. Observe that the Relative URL of the project is set to jsp/helloWorld.jsp.
- Right click project and select Properties.
- Select Run on the left and observe that the Relative URL field is set to jsp/helloWorld.jsp.
Figure-4.22
(4.3) Create a new jar file with our own resource
- Right click resources under dynamicResources.jar->META-INF and select Copy.
Figure-4.31
- Right click META-INF of the my_webfragment and select Paste.
Figure-4.32
2. Modify the helloWorld.jsp.
Figure-4.33
3. Rename the helloWorld.jsp.
Figure-4.34
Figure-3.35
4. Click Save all button, so that the library is recompiled.
5. Add the my_webfragment library to the jsp-resource-in-bundled-jar-war application.
Figure-4.36
- Browse down to the directory where you created the my_webfragment library project.
- Select the my_webfragment library project.
- Click Add Project JAR Files button.
Figure-4.37
6. Change the Relative URL property of the jsp-resource-in-bundled-jar-war application.
Figure-4.38
- Select Run on the left.
- For the Relative URL field, enter jsp/helloKorea.jsp.
- Click OK.
Figure-4.39
7. Build and run the the jsp-resource-in-bundled-jar-war application.
- Right click project and select Run.
- Observe that the default browser gets displayed with a result.
Figure-4.40
Summary
In this exercise, you have learned how dynamic and static resources can be bundled inside the META-INF/resources directory of a JAR file inside the application’s WEB-INF/lib directory as if they had been placed in the application’s document root.
Exercise 5: Asynch. Servlet
One of the significant enhancements made in JSR 315: Java Servlet 3.0, is support for asynchronous processing. With this support, a servlet no longer has to wait for a response from a resource such as a database before its thread can continue processing, that is, the thread is not blocked. Previous to Servlet 3.0, asynchronous support in the Java EE web container was provided in a proprietary way — either through APIs built on top of the Servlet 2.5 API or through non-Servlet APIs or implementations.
- Open, build, and run “servlet3.0_async_simple_dispatch” sample application
- Study the code
- Open, build, and run “servlet3.0_async_simple_complete” sample application
- Study the code
- Open, build, and run “servlet3.0_async_simple_listener_completed” sample application
- Study the code
- Open, build, and run “servlet3.0_async_simple_listener_timeout” sample application
- Study the code
- Open, build, and run “async-request-war” sample application
- Study the code
(5.1) Open, build, and run “servlet3.0_asynch_simple_dispatch” sample application (from “lab samples”)
1. Open servlet3.0_asynch_simple_dispatch NetBeans project.
- Select File->Open Project (Ctrl+Shift+O).
- Observe that the Open Project dialog box appears.
- Browse down to <LAB_UNZIPPED_DIRECTORY>/javaee6_servlet3.0/samples directory.
- Select servlet3.0_asynch_simple_dispatch.
- Click Open Project.
- Observe that the servlet3.0_asynch_simple_dispatch project node appears under Projects tab window.
2. Build and run servlet3.0_asynch_simple_dispatch project.
- Right-click servlet3.0_asynch_simple_dispatch project and select Run.
- Observe that browser gets displayed.
Figure-5.11
- Wait a couple of seconds and observe that a response page is displayed.
Figure-5.12
(5.2) Study the code
<html> <head><title></title></head> <body> <h1>Call an Asynch Servlet</h1> <form action=”AsynchServlet”> </body> |
index.html
2. response.jsp
<%@page contentType=”text/html” pageEncoding=”UTF-8″%> <!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”> <html> |
3. AsynchServlet.java
package asyncpackage;
import javax.servlet.ServletException; @WebServlet(urlPatterns = “/AsynchServlet”, asyncSupported = true) @Override AsyncContext aCtx = request.startAsync(request, response); } |
AsynchServlet.java
- Display Javadoc of the AsyncContext class.
Figure-5.21
Figure-5.22
4. AsynchRunnable.java
package asyncpackage;
import javax.servlet.AsyncContext; public class AsynchRunnable implements Runnable { AsyncContext ctx; public AsynchRunnable(AsyncContext ctx) { public void run() { ctx.dispatch(“/response.jsp”); |
AsynchRunnable.java
(5.3) Open, build, and run “servlet3.0_async_simple_complete” sample application (from “lab samples”)
1. Open servlet3.0_async_simple_complete NetBeans project.
- Select File->Open Project (Ctrl+Shift+O).
- Observe that the Open Project dialog box appears.
- Browse down to <LAB_UNZIPPED_DIRECTORY>/javaee6_servlet3.0/samples directory.
- Select servlet3.0_async_simple_complete.
- Click Open Project.
- Observe that the servlet3.0_async_simple_complete project node appears under Projects tab window.
2. Build and run servlet3.0_async_simple_complete project.
- Right-click servlet3.0_async_simple_complete project and select Run.
- Observe that browser gets displayed.
(5.4) Study code
package asynpackage;
importjavax.servlet.AsyncContext; public class AsyncRunnable implements Runnable { AsyncContext ctx; public AsyncRunnable(AsyncContext ctx) { public void run() { // Complete the asynch task |
package asynpackage;
import javax.servlet.ServletException; @WebServlet(urlPatterns = “/AsyncServlet”, asyncSupported = true) @Override; AsyncContext aCtx = request.startAsync(request, response); // Control is returned after an async. call is done out.println(“<html>”); } finally { } |
AsyncServlet.java
(5.5) Open, build, and run “servlet3.0_async_simple_listener_completed” sample application (from “lab samples”)
1. Open servlet3.0_async_simple_listener_completed NetBeans project.
- Select File->Open Project (Ctrl+Shift+O).
- Observe that the Open Project dialog box appears.
- Browse down to <LAB_UNZIPPED_DIRECTORY>/javaee6_servlet3.0/samples directory.
- Select servlet3.0_async_simple_listener_completed.
- Click Open Project.
- Observe that the servlet3.0_async_simple_listener_completed project node appears under Projects tab window.
2. Build and run servlet3.0_async_simple_listener_completed project.
- Right-click servlet3.0_async_simple_listener_completed project and select Run.
- Observe that browser gets displayed.
Figure-5.51
Figure-5.52
- Observe in the GlassFish log that the onComplete() method is called.
Figure-5.52b
(5.6) Study code
package asyncpackage;
import javax.servlet.ServletException; @WebServlet(urlPatterns = “/AsyncServlet”, asyncSupported = true) @Override AsyncContext aCtx = request.startAsync(request, response); aCtx.addListener(new AsyncListener() { public void onComplete(AsyncEvent event) throws IOException { public void onTimeout(AsyncEvent event) throws IOException { } public void onError(AsyncEvent event) throws IOException { } public void onStartAsync(AsyncEvent event) throws IOException { // Control is returned after an async. call is done out.println(“<html>”); } finally { } |
(5.7) Open, build, and run “servlet3.0_async_simple_listener_timeout” sample application (from “lab samples”)
1. Open servlet3.0_async_simple_listener_timeout NetBeans project.
- Select File->Open Project (Ctrl+Shift+O).
- Observe that the Open Project dialog box appears.
- Browse down to <LAB_UNZIPPED_DIRECTORY>/javaee6_servlet3.0/samples directory.
- Select servlet3.0_async_simple_listener_timeout.
- Click Open Project.
- Observe that the servlet3.0_async_simple_listener_timeout project node appears under Projects tab window.
2. Build and run servlet3.0_async_simple_listener_timeout project.
- Right-click servlet3.0_async_simple_listener_timeout project and select Run.
- Observe that browser gets displayed.
Figure-5.71
Figure-5.72
- Observe in the GlassFish log that onTimeout() is first called, because it has been set shorter than the duration of the asynchroneous call, then onComplete() method is called.
Figure-5.72b
(5.8) Study code
package asynpackage;
import javax.servlet.ServletException; @WebServlet(urlPatterns = “/AsyncServlet”, asyncSupported = true) @Override AsyncContext aCtx = request.startAsync(request, response); // Add timeout less than the duration of Asynch call aCtx.addListener(new AsyncListener() { public void onComplete(AsyncEvent event) throws IOException { public void onTimeout(AsyncEvent event) throws IOException { } public void onError(AsyncEvent event) throws IOException { } public void onStartAsync(AsyncEvent event) throws IOException { // Control is returned after an async. call is done out.println(“<html>”); } finally { } |
AsyncServlet.java
(5.9) Open, build, and run “async-request-war” sample application (from “lab samples”)
The “async-request-war” sample application is a chat application where multiple users are participating in a chat. This shows how Asynch. Servlet is used to support Comet.
1. Open async-request-war NetBeans project.
- Select File->Open Project (Ctrl+Shift+O).
- Observe that the Open Project dialog box appears.
- Browse down to <LAB_UNZIPPED_DIRECTORY>/javaee6_servlet3.0/samples directory. (Note: The async-request-war project in <GlassFish-v3-Installation-Directory>/glassfish/samples/javaee6/web/servlet directory has a bug, that causes Safari to misbehave.)
- Select async-request-war.
- Click Open Project.
- Observe that the async-request-war project node appears under Projects tab window.
2. Build and run async-request-war project.
- Right-click async-request-war project and select Run.
- Observe that the default browser gets displayed.
- For Please input your name field, enter <your name>, Sang Shin in this example.
- Click Login.
Figure-5.91
- Observe that the System Message indicating that you have joined the chat.
- Enter a message and click Post Message.
Figure-5.92
- Observe that the message has been received.
Figure-5.93
4. Start the chatting from a user #2.
- Open another browser type. (If you were using Firefox for user #1, open either IE or Safari or Chrome, Opera for example. If you were using Firefox for user #1 and want to use Firefox for the user #2, you have to start it from a different profile. In this example, Safari is used.)
- In the URL field, enter http://localhost:8080/async-request-war/ and click Return.
Figure-5.94
- Observe that Login page appears.
- For Please input your name field, enter a name of your friend, Arun Gupta in this example.
- Click Login.
- Observe that a message indicating Arun Gupta has joined chat gets displayed.
Figure-5.95
5. Go to the User #1 browser.
- Observe that Arun Gupta has joined the chat.
Figure-9.96
6. Enter a message from User #2.
Figure-5.97
7. Observe the message posted by User #2 in User #1.
Figure-9.98
(5.10) Study the code
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only (“GPL”) or the Common Development * and Distribution License(“CDDL”) (collectively, the “License”). You * may not use this file except in compliance with the License. You can obtain * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific * language governing permissions and limitations under the License. * * When distributing the software, include this License Header Notice in each * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt. * Sun designates this particular file as subject to the “Classpath” exception * as provided by Sun in the GPL Version 2 section of the License file that * accompanied this code. If applicable, add the following below the License * Header, with the fields enclosed by brackets [] replaced by your own * identifying information: “Portions Copyrighted [year] * [name of copyright owner]” * * Contributor(s): * * If you wish your version of this file to be governed by only the CDDL or * only the GPL Version 2, indicate your decision by adding “[Contributor] * elects to include this software in this distribution under the [CDDL or GPL * Version 2] license.” If you don’t indicate a single choice of license, a * recipient has the option to distribute your version of this file under * either the CDDL, the GPL Version 2 or to extend the choice of license to * its licensees as provided above. However, if you add GPL Version 2 code * and therefore, elected the GPL Version 2 license, then the option applies * only if the new code is made subject to such option by the copyright * holder. */ package web.servlet.async_request_war; import java.io.IOException; import javax.servlet.AsyncContext; /** private static final Queue<AsyncContext> queue = new ConcurrentLinkedQueue<AsyncContext>(); private static final BlockingQueue<String> messageQueue = new LinkedBlockingQueue<String>(); private static final String BEGIN_SCRIPT_TAG = “<script type=’text/javascript’>\n”; private static final String END_SCRIPT_TAG = “</script>\n”; private static final long serialVersionUID = -2919167206889576860L; private static final String JUNK = “<!– Comet is a programming technique that enables web ” + private Thread notifierThread = null; @Override // Start the thread as part of Servlet initialization @Override PrintWriter writer = res.getWriter(); // When a new client joined the chat, a new AsyncContext public void onTimeout(AsyncEvent event) throws IOException { public void onError(AsyncEvent event) throws IOException { public void onStartAsync(AsyncEvent event) throws IOException { @Override req.setCharacterEncoding(“UTF-8”); // When a login or message post request is received, call notify() method, res.getWriter().println(“success”); res.getWriter().println(“success”); @Override private void notify(String cMessage) throws IOException { private String escape(String orig) { for (int i = 0; i < orig.length(); i++) { return buffer.toString(); private String toJsonp(String name, String message) { |
AjaxCometServlet.java
Summary
In this exercise, you have learned how to use asynch. Servlet API so that, a servlet no longer has to wait for a response from a resource such as a database before its thread can continue processing.
(6.1) Open, build, and run “multipart-war” sample application
1. Open multipart-war NetBeans project.
- Select File->Open Project (Ctrl+Shift+O).
- Observe that the Open Project dialog box appears.
- Browse down to <GlassFish-v3-Installation-Directory>/glassfish/samples/javaee6/web/servlet directory.
- Select multipart-war.
- Click Open Project.
- Observe that the multipart-war project node appears under Projects tab window.
2. Build and run multipart-war project.
- Right-click multipart-war project and select Run.
- Browser gets displayed.
Figure-6.11
- Click on the first Browse… button and navigate to <LAB_UNZIPPED_DIRECTORY>/javaee6_servlet3.0/files directory.
Choose billofrights.txt as first file to upload. - Click on the second Browse… button and choose binary.png in the same directory as second file to upload.
- Click on the upload button.
Figure-6.11a
- Observe that the server returns in a jsp page the multiparts of the uploaded files.
Figure-6.11b
- Now, uncomment in getParts.jsp, the snippet of code which allows to dump a text file inside the jsp page.
Figure-6.11c
- Click Save all button.
- Click on the back button in the browser, then on the Reset button.
- Choose the same text file as you have chosen previously as the first file to load and click on upload.
Figure-6.11d
- Observe that the server dumps also the contents of the text file in the jsp page.
Figure-6.11e
(6.2) Study the code
<html> <head> <title>File Upload Example</title> </head> <body> |
2. Study getParts.jsp.
<%@ page import=”javax.servlet.http.Part” %>
<html> <body> <% /* out.write(“Part: ” + p.toString() + “<br/>\n”); */ /* java.io.InputStreamReader in = int c = in.read(); } </body> |
getParts.jsp.
3. Study the Javadoc of the HttpServletRequest class.
Figure-6.12
Summary
In this exercise, you have learned how to multi-part methods of HttpServletRequest class.
Exercise 7: Build and run “sessioncookieconfig-war” sample application
(7.1) Open, build, and run “sessioncookieconfig-war” sample application (from “glassfish-samples”)
1. Open sessioncookieconfig-war NetBeans project.
- Select File->Open Project (Ctrl+Shift+O).
- Observe that the Open Project dialog box appears.
- Browse down to <GlassFish-v3-Installation-Directory>/glassfish/samples/javaee6/web/servlet directory.
- Select sessioncookieconfig-war.
- Click Open Project.
- Observe that the sessioncookieconfig-war project node appears under Projects tab window.
2. Build and run sessioncookieconfig-war project.
- Right-click sessioncookieconfig-war project and select Run.
- Browser gets displayed.
Figure-7.11
(7.2) Study the code
package web.servlet.sessioncookieconfig_war;
import java.io.*; public class CreateSession extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) req.getSession(true); String sessionCookie = res.getHeader(“Set-Cookie”); // name // comment // domain // path // secure // http-only // max-age res.getWriter().println(“SUCCESS”); |
2. ConfigListener.java.
package web.servlet.sessioncookieconfig_war;
import java.io.*; public class ConfigListener implements ServletContextListener { /** /** |
Summary
In this exercise, you have learned how to use the new programmatic configuration support for session cookies available with Servlet 3.0.
Exercise 8: Security
Programmatic security is used by security aware applications when declarative security alone is not sufficient to express the security model of the application. Programmatic security consists of the following methods of the HttpServletRequest
interface:
- login
- logout
- getRemoteUser
- isUserInRole
- getUserPrincipal
For detailed description of this tutorial, please see “Using Programmatic Security with Web Applications” section of the “Java EE 6 tutorial”.
- Create the “javaee6user” user or update his password in the File-based security realm of the GlassFish v3
- Open, build, and run “programmatic-login” sample application
- Study the code
- Open, build, and run “programmatic-authenticate” sample application
- Study the code
- Open, build, and run “http-method-omission” sample application
- Study the code
(8.0) Update the password of the “javaee6user” in the File-based security realm of the GlassFish v3
Depending on the system, the “javaee6user” exists or does not exist in the File-based security realm of the GlassFish v3 as part of GlassFish v3 installation. The File-based security realm is the default security realm.
1. Start GlassFish v3 Domain.
- Select Services tab.
- Expand Servers.
- Right click GlassFish v3 Domain and select Start.
2. Open GlassFish v3 Admin console.
- Select Services tab.
- Expand Servers.
- Right click GlassFish v3 Domain and select View Admin Console.
Figure-8.01
3. Open Manage Users page.
- Expand Security->Realms.
- Click file security realm and observe that Edit Realm pane appears on the right.
- Click Manage Users.
Figure-8.02
4. Create the javaee6user if it does not exist.
Do not do this step if the javaee6user exists.
- Observe that there is no user in the File Users database.
- Click New….
Figure-8.02a
- Enter javaee6user in the User ID field.
- Enter javaee6user in the Group List field.
- Enter test (or whatever you want) in the New Password field.
- Enter test (or whatever you chose above) in the Confirm New Password field.
- Click OK.
Figure-8.02b
5. Update the password of the javaee6user.
Do not do this step if you have done step 4.
- Observe that the javaee6user user is already created.
- Click javaee6user.
Figure-8.03
- For the New Password and Confirm New Password fields, enter test (or whatever password of your choice).
- Click Save.
Figure-8.04
(8.1) Open, build, and run “programmatic-login” sample application (from “glassfish-samples”)
1. Open programmatic-login NetBeans project.
- Select File->Open Project (Ctrl+Shift+O).
- Observe that the Open Project dialog box appears.
- Browse down to <GlassFish-v3-Installation-Directory>/glassfish/samples/javaee6/security directory.
- Select programmatic-login.
- Click Open Project.
- Observe that the programmatic-login project node appears under Projects tab window.
2. Build and run programmatic-login project.
- Right-click programmatic-login project and select Run.
- Browser gets displayed.
- For the Username field, enter javaee6user.
- For the Password field, enter test (or whatever password you chose).
Figure-8.11
Figure-8.12
(8.2) Study code
package enterprise.programmatic_login;
import java.io.*; import javax.annotation.security.DeclareRoles; /** /** out.println(“Before Login”+”<br><br>”); try { request.logout(); } finally { // <editor-fold defaultstate=”collapsed” desc=”HttpServlet methods. Click on the + sign on the left to edit the code.”> /** /** |
LoginServlet
(8.3) Open, build, and run “programmatic-authenticate” sample application (from “lab samples”)
1. Open programmatic-authenticate NetBeans project.
- Select File->Open Project (Ctrl+Shift+O).
- Observe that the Open Project dialog box appears.
- Browse down to <LAB_UNZIPPED_DIRECTORY>/javaee6_servlet3.0/samples directory.
- Select programmatic-authenticate.
- Click Open Project.
- Observe that the programmatic-authenticate project node appears under Projects tab window.
2. Build and run programmatic-authenticate project.
- Right-click programmatic-authenticate project and select Run.
- Login dialog box gets displayed.
- For the Username field, enter javaee6user.
- For the Password field, enter test (or whatever password you chose).
Figure-8.31
Trouble-shooting: If you don’t see the dialog box above, it is highly likely because the browser cache the previous login name and password. Close all browser instanced and run the program again.
Figure-8.32
(8.4) Study the code
package mypackage;
import java.io.IOException; /** /** } finally { } // <editor-fold defaultstate=”collapsed” desc=”HttpServlet methods. Click on the + sign on the left to edit the code.”> /** /** } |
(8.5) Open, build, and run “http-method-omission” sample application (from “glassfish-samples”)
1. Open http-method-omission NetBeans project.
- Select File->Open Project (Ctrl+Shift+O).
- Observe that the Open Project dialog box appears.
- Browse down to <GlassFish-v3-Installation-Directory>/glassfish/samples/javaee6/security directory.
- Select http-method-omission.
- Click Open Project.
- Observe that the http-method-omission project node appears under Projects tab window.
2. Build and run http-method-omission project.
- Right-click http-method-omission project and select Run.
- Browser gets displayed.
- Click Try Post button.
Figure-8.51
- Observe that “Welcome javaee6user” message is displayed.
Figure-8.52
- Click Backward button of the browser.
- Click Try Get button.
Figure-8.53
- Observe that the access is denied. (This is expected behavior.)
Figure-8.54
(8.6) Study code
<html>
<body> </body> |
index.jsp
2. OmissionServlet.java
package enterprise.http_method_omission;
import java.io.*; import java.security.Principal; /** /** response.setContentType(“text/html;charset=UTF-8”); } finally { } // <editor-fold defaultstate=”collapsed” desc=”HttpServlet methods. Click on the + sign on the left to edit the code.”> /** |
OmissionServlet.java
2. web.xml
<?xml version=”1.0″ encoding=”UTF-8″?> <web-app version=”3.0″ xmlns=”http://java.sun.com/xml/ns/javaee” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd”> <servlet> <servlet-name>OmissionServlet</servlet-name> <servlet-class>enterprise.http_method_omission.OmissionServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>OmissionServlet</servlet-name> <url-pattern>/omissionservlet</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <security-constraint> <display-name>OmissionConstraint</display-name> <web-resource-collection> <web-resource-name>resource1</web-resource-name> <description/> <url-pattern>/omissionservlet</url-pattern> <http-method-omission>POST</http-method-omission> </web-resource-collection> <auth-constraint> <description/> </auth-constraint> </security-constraint> <security-constraint> <display-name>MethodConstraint</display-name> <web-resource-collection> <web-resource-name>resource1</web-resource-name> <description/> <url-pattern>/omissionservlet</url-pattern> <http-method>GET</http-method> <http-method>POST</http-method> </web-resource-collection> <auth-constraint> <role-name>javaee6user</role-name> </auth-constraint> </security-constraint> <login-config> <auth-method>BASIC</auth-method> <realm-name/> </login-config> <security-role> <description/> <role-name>javaee6user</role-name> </security-role> </web-app> |
web.xml
Summary
In this exercise, you have learned how to use programmatic security (login/logout) feature in Servlets 3.0.
Mandatory Homework Exercise (for people who are taking “Java EE 6 Codecamp”)
- Goal: exercising annotations
- Remove web.xml.
- Use @WebServlet annotation for the GreetingServlet.
- Use dynamic registration for the ResponseServlet.
- Goal: exercising 3rd party library/framework pluggability
- Create a library called mywebfragment1 with its own web-fragment.xml.
- Define a filter in which “myfiltermessage” attribute in the request scope is appended with a value “mymessage1”. The filter configuration should be specified in the web-fragment.xml.
- Create another library called mywebfragment2 without web-fragment.xml. The filter configuration should be configured with @WebFilter annotation.
- Zip file of the the myhelloservlet NetBeans project. (Someone else should be able to open and run it as a NetBeans project.) You can use your favorite zip utility or you can use “jar” utility that comes with JDK as following.
- cd <parent directory that contains myhelloservlet directory> (assuming you named your project as myhelloservlet)
- jar cvf myhelloservlet.zip myhelloservlet (myhelloservlet should contain nbproject directory)
- Captured output screen – name it as homework_javaee6_servlet3.0.gif or homework_javaee6_servlet3.0.jpg (or homework_javaee6_servlet3.0.<whatever graphics format>)
- Any screen capture that shows that your program is working is good enough.
Optional Homework Exercise (for people who are taking “Java EE 6 Codecamp”)
- Add a “Quit chatting” button, when pressed, to close a chatting session.
- Add a “Restart chatting” button, when pressed, to restart a chatting session
- Zip file of the the my-async-request-war NetBeans project. (Someone else should be able to open and run it as a NetBeans project.) You can use your favorite zip utility or you can use “jar” utility that comes with JDK as following.
- cd <parent directory that contains my-async-request-war directory> (assuming you named your project as my-async-request-war)
- jar cvf my-async-request-war.zip my-async-request-war (my-async-request-war should contain nbproject directory)