Year
|
Java EE
|
EJB
| What’s new |
1998
|
1.0
| Stateful Session Beans Stateless Session Beans Entity Beans JNDI Thread safe JTA | |
1999
|
1.2
|
1.1
| XML descriptors RMI over IIOP |
2001
|
1.3
|
2.0
| local beans |
2003
|
1.4
|
2.1
| SOAP support Message driven beans |
2006
|
5
|
3.0
| JPA replaces Entity beans POJOs/annotations |
2009
|
6
|
3.1
| CDI @Asynchronous @Singleton |
2013
|
7
|
3.2
|
7 September 2012
EJB version history
Labels:
EJB,
FJ-310,
java,
Java EE,
SL-351-EE5
29 July 2012
Programmatic servlet configuration
Servlet 3 allows you to configure your webapplication in Java.
A set of methods have been added to ServletContext to support this, e.g.
- addFilter
- addListener
- addServlet
@WebListener
public class MyServletContextListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent event) {
ServletContext ctx = event.getServletContext();
ServletRegistration servlet = ctx.addServlet ("ProServlet","org.edu.ProServlet");
servlet.addMapping("/dyna/*");
}
public void contextDestroyed(ServletContextEvent event) {}
}
After the ServletContext is initialised, you can't call these methods anymore.
Labels:
servlet,
servlet 3,
ServletContextListener,
SL-314-EE6
Servlet listeners
| Scope | Lifecycle listeners | Attribute change listeners | Register |
| Application | ServletContextListener | ServletContextAttributeListener | @WebListener |
| Servlet | HttpServlet | @PostConstruct @PreDestroy | |
| Session | HttpSessionListener | HttpSessionAttributeListener | @WebListener |
| Request | ServletRequestListener | ServletRequestAttributeListener | @WebListener |
| Asynchronous Request | AsyncListener | AsyncContext#addListener |
- Additional Session Listeners:
- HttpSessionBindingListener: Notifies an attribute that it is bound/unbound from the session attribute map
- HttpSessionActivationListener: Notifies an attribute that it is passivated to/activated from persistent storage (session passivation)
- WebListeners can also be registered using
- web.xml
<web-app> <listener> <listener-class>org.edu.MyListener</listener-class> </listener> … </web-app>
- programmatically using ServletContext#addListener from a ServletContextListener
- web.xml
- Example of using a ServletContextListener
Labels:
java,
Java EE,
servlet,
SL-314-EE6
What’s new in Servlet 3 (edit)
Servlets 3 (JSR 315) highlights:
- Instead of configuring the web application in the web.xml deployment description you now have more options
- modular deployment descriptor
jars can be bundled with your webapp containing a web-fragment.xml file. These files are merged with the main web.xml file of your webapp. This allows for easy plugging in of web application modules. If you plugin a framework jar, you won’t have to modify the web.xml of your application anymore (e.g. to send all *.do files to a framework servlet). - annotations
@WebServlet ("/jeeves") public class ZServlet extends HttpServlet { .... }- Extends HttpServlet, no POJO (yes!)
- programmatic configuration
- Asynchronous servlets
@WebServlet ("/jeeves", asyncSupported=true) public class ZServlet extends HttpServlet { private AsyncContext ctx public void doGet( HttpServletRequest req, HttpServletResponse res) { ctx = req.startAsync(); // kick off a thread for async work ctx.start(new L8r()); //method returns immediatly, no response sent } // end doGet method class L8r implements Runnable{ public void run(){ // do work ... // response ready ctx.complete(); // alternative: ctx.dispatch("responseViewer.jsp"); } // end run method } //end L8r class } //end ZServlet class
The asynchronous servlet doGet() method does not wait for the L8r thread to complete. The request/response parameters however are not committed, but they are cached in AsyncContext. The L8r thread can then use these to reply to the waiting client. - Security
- ProgrammaticLogin class
- Java EE5 authorisation annotations
- @RolesAllowed
- @PermitAll
- @DenyAll
- @transportprotected: use SSL
- Session security
<session-config> <!-- do not expose session id by using URL rewriting --> <tracking-mode>COOKIE</tracking-mod> <cookie-config> <!-- do not expose cookie to javascript--> <http-only>true</ttp-only> <!-- only transit cookie over encrypted connection--> <secure>true</secure> </cookie-config> </session-config>
Method calls are now possible from EL, e.g.:
#{portfolio.add('ORCL',100)}
Labels:
FJ-310,
java,
servlet,
servlet 3,
SL-314-EE5,
SL-314-EE6,
SL-340-EE6
26 July 2012
Web components: extra exercises
Module 3 Exercise 2 (extra): a DAO model
- Extend the Anniversary class with an extra attribute: flowers
- Add the following files with a new model to the project: AnniversaryMemoryDao with interface AnniversaryDao
- Modify the AnniversaryControl servlet
- Instantiate an AnniversaryDao
AnniversaryDao dao = AnniversaryMemoryDao.getAnniversaryDAO(); - Use anniversaryDao as the model
- Modify the JSP to show the flowers you get at the anniversary
- (Optional) Create a trivial unit test for the model
- Right click the project and select New > Other... > Unit TestsTest Packages > JU Test for existing class
- Browse to the AnniversaryDao class
- Click Finish
- In the generated Test class
- Remove the TestGetAnniversaryDao class
- In the testGetByYear method
- Create an AnniversaryDao instance like you did in the Controller servlet
- Supply an anniversary year to the getByYear method
- Modify the assertEquals method
- As a first argument type the name of the flowers matching the anniversary year
- As a second argument call the getFlowers method
- Add another assertEquals method testing the material
- Remove the fail method at the end of the test
- In the project window, right click the AnniversaryMemoryTest class and select Test File
Module 4 Exercise 2 (extra): working with sessions
1. Add a “Set colour preferences” link
to the Anniversary application index page
a.
The
link should take the user to a colourpicker web page.
2. Create the ColourPicker web page, that shows a drop down list with some of these colours:
a. Aqua Black
Blue Fuchsia Gray Green Lime Maroon Navy Olive Purple Red Silver Teal Yellow
(these are HTML color names defined in the
HTML 3 standard)
b.
Add an OK button that takes you to the ColourControl
servlet
3. Create the ColourControl servlet. In the servlet
- Save the colour in the session
- Go back to the home page
4. On the showAnniversary.jsp, show the
text in the chosen colour. If there is no chosen colour, use black.
Hints:
HTML font tag example: <font
color=”Black"> See the
difference? </font>
EL ternary
expression example ${aVariable == “”?”empty”:bVariable}
Module 5 Exercise 2 (extra): Default Colour
- Specify an initialization parameter with a default colour for the ColourControl servlet.
- Save the value in a session attribute
- Use the session attribute to set this colour as the default selected colour in the drop down list in the colourpicker page
- Test your application.
- The first time you go to the page the default colour is not selected. If you choose a colour and go back to the page the default colour is selected. Explain this behaviour.
Hint: HTML to set an option as the selected option:
<option selected>Peach</option>
Module 6 Exercise 1 Task 2 (extra): Display multivalued headers
Write a version of the HTTP Headers list that is capable of displaying headers with multiple valuesModule 6 Exercise 3 (extra): Call methods from EL
- Add a method calculateWeddingYear() to the Anniversary class in the wedding anniversary project.
- Add a message to showAnniversary.jsp: You were married in the year xxxx!
- Test the project and correct any errors until you have the expected result.
- Optional: If the AnniversaryMemoryDao does not have an anniversary (e.g. 100) in its list it will return an Anniversary object where the material is empty. Show the message with material and flowers only if you have found an Anniversary object with a material. Show the message with the year of marriage only if you have found an Anniversary object with an empty material.
Module 8 Exercise 3 (extra): Count headers
Change Module 6 exercise 1 to display the number of headers on top of the list.Module 8 Exercise 4 (extra): Colourpicker
Modify the
colourpicker in the anniversary project:
- supply the colours as a comma separated string: aqua,black,blue,fuchsia gray,green,lime,maroon,navy,olive,purple,red,silver,teal,yellow.
- generate the choice menu using a foreach loop
- Optional: When you return to the picker the previously chosen color should be selected by default.
Module 8 Exercise 5 (extra): Write a jspx document
- Copy index.jsp from Module 6 exercise 1 to index.jspx.
- Modify the code to be compliant with a jspx document.
Module 10 Exercise 2 (extra): AnniversaryJpaDao
In the wedding anniversary project, add a AnniversaryJpaDao.- Create the AnniversaryJpaDao class that implements the AnniversaryDao interface
- Add a constructor that takes an EntityManagerFactory as an argument.
- Implement the interface methods
- In the methods create an EntityManager:
Entitymanager em = emf.createEntityManager();
- Use the EntityManager to access the database.
- Adapt the AnniversaryControl servlet to use the AnniversaryJpaDao.
- Inject an EntityManagerFactory in the AnniversaryControl
@PersistenceUnit private EntityManagerFactory emf;
- When the servlet is created, create the AnniversaryJpaDao and assign it to an AnniversaryDao attribute.
- Remove any reference to other AnniversaryDao implementations.
- Change the Anniversary class into a JPA Entity.
- Use the year as a primary key.
- Create a persistence unit in the project, use an existing glassfish DB resource
- Run the project.
- Ignore any errors that complain that the Anniversary table already exists.
- Correct other errors
- Enter the flowers entries into the database
- Collect database characteristics
- In the services tab, right click Servers> GlassFish Server> Resources> JDBC> JDBC Resources
- Right click the database you used in the project, and select Properties
- Write down the Pool Name
- Rght click Servers> GlassFish Server> Resources> JDBC> Connection Pools
- Right click the pool name you found and select properties
- Write down the properties
- Create a connection to the database in Netbeans
- Right click the Databases tab and select New Connection...
- Select a Java DB (Network) driver and click Next>
- Enter the properties of the server database
- Append to the JDBC URL
;create=true
- Click Finish
- Insert the entries
- Right click the database connection you created an select Connect...
- Right click the database connection you created an select Execute Command...
- Copy the contents of this file into the window
- Right click in the window and select Run File
- Test the application. Verify that all entries returned from the database are in CAPITALS.
Labels:
SL-314-EE6
21 July 2012
12 July 2012
Java EE Applications: Calling a JSF page from a non-JSF page
Lab 6 replaces the CustomerController servlet with the ustomerDetails.xhtml JSF page.
Existing JSP pages need to be adapted to call the JSF page. Here's how you call a JSF page from a JSP or plain HTML page and pass a parameter to it.
- Change the AllCustomers.jsp page to call the JSF page.
- Existing call to the servlet
- New call to the JSF page
- Change the JSF backing bean to receive parameters and call code when it is created by a GET request.
- Receive a GET parameter in an attribute
- Call a method when the bean is created. The method looks up data using the value stored in the attribute in the previous step.
<a href="CustomerController?customerIdentity=<%= customers[i].getId()%>&submit=Get Customer">
<a href="CustomerDetails.xhtml?customerIdentity=<%= customers[i].getId()%>">
@ManagedBean(name="customerDetails")
@RequestScoped
public class CustomerManagedBean {
@ManagedProperty("#{param.customerIdentity}")
private String customerId = "";
...
}
@PostConstruct
public void initCustomer() {
if (customerId != null && customerId.length()!= 0){
retrieveCustomer();
}
}
Subscribe to:
Posts (Atom)
