Using Servlets in the Java Studio Creator IDE

I am working on a tutorial on how to use JasperReports in JSF web applications. At first, like most examples out there, I built a servlet and called the servlet from my web page. I eventually figured out that it is quicker and easier to use a managed bean, and took that route instead.

However, I do see a lot of posts in the forum asking how to use servlets in JSF web applications built using the IDE, so here is what I learned.

  1. Make the servlet and put it in the web applications source package structure or add it to the library node. In this example, I called the servlet ReportGenerator.

  2. Make a mapping for the servlet. To do so, go to the Files window, expand web, expand WEB-INF, and double-click web.xml to open the xml editor.

  3. Click the Servlets button in the editing toolbar and click Add Servlet Element.

  4. Set the Servlet Name to whatever you choose (I used ReportGenerator).

  5. For the Servlet Class, browse to the source for your servlet (or type in the fully-qualified class name if using a library).

  6. Type a URL Pattern. This is how you will invoke the servlet from your web page. I used /makeReport. One thing to notice is that you need to prepend a forward slash here but you don't include the forward slash when you use it in a redirect, as you will see later. Click OK.

  7. Now you might think that you next bring up the Page Navigation editor to add a navigation mapping for makeReport. But that is not how you do it. From everything that I have read, JSF navigation entries are for navigation from a JSF view to a JSF view. So, what I did was to use a redirect, as shown in the following code:

     
        public String button1_action() {
            try {
            FacesContext facesContext = getFacesContext();
            HttpServletResponse response = 
    		(HttpServletResponse) facesContext.getExternalContext().getResponse();
            response.sendRedirect("makeReport");
            facesContext.responseComplete();
            } catch (Exception e) {
                log(e.getMessage());
                error(e.getMessage());
            }
            return null;
        }
    	


    Note that after redirecting the request, I send a responseComplete. This is extremely important. It is like passing the baton and notifying that you are dropping out of the lifecycle. If you don't complete the response, then you will get this exception:

    Exception Details: java.lang.IllegalStateException Cannot forward after response has been committed

  8. Alternatively, if you want a link instead of an action handler, you can add a link component and set it's url property to /faces + your pattern (this time include the forward slash). In my case, I set the url property to /faces/makeReport.

Comments:

Wow, fantastic timing, I just started getting this exception in an application I'm working on now. :-) Thanks, and keep up the wonderful work!

Posted by Dave on September 24, 2006 at 02:58 AM PDT #

Post a Comment:
Comments are closed for this entry.
About

divas

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today