Tuesday, January 04, 2011

JSF 2.x Forwarding Request to a Servlet

The question of how to forward a JSF Request to a server was something that came up the other day. I have some legacy servlets which will handle some of the information which is being generated from my JSF pages.

So I decided to write an example of how to do it. The example is designed to go from a JSF page to a servlet which will process the headers and display the information. The alternate example passes the request to a servlet which then passes it back to another JSF page for display. The example also demonstrates using a <f:param /> to pass additional information. The project was done using NetBeans and Maven 2.

Note: The project files can be downloaded here: jsf-servlet-forwarding.zip

As you can see from the images, the forms are very simple and used for demonstration only. 
JSF Request Form

INDEX.XHTML


       <h:form id="form1">
            <h:panelGrid id="panelGrid1" columns="1">
                <h:outputLabel id="outputLabel1" for="inputText1" value="Please enter your name:"/>
                <h:inputText id="inputText1" value="#{indexBean.name}" required="true"/>
                <h:message id="message1" for="inputText1"/>
                <h:panelGroup id="panelGroup1">
                    <h:commandButton id="commandButton1" value="Forward" type="submit" action="#{indexBean.forward}"/>
                    <h:commandButton id="commandButton2" value="Process" type="submit" action="#{indexBean.forward}">
                        <f:param id="param1" name="target" value="/results.xhtml"/>
                    </h:commandButton>
                </h:panelGroup>
            </h:panelGrid>
        </h:form>

JSF -Servlet -JSF Response

RESULTS.XHTML


This file has some interesting syntax which a number of people may not be used to seeing, for example #{param['form1:inputText1']} which is using Expression Language (EL) to read the param[] and find the specific form element which is in single quotes.


    <h:body>
        <p>
            <h:outputText id="outputText1" value="TA DA!" style="font-weight: bold; font-size: larger; text-align: center;"/>
        </p>
        <p>
            <h:outputText id="outputText2" value="Hello #{param['form1:inputText1']}!"/>
        </p>
        <p>
            <h:outputText id="outputText3" value="'target' ==> #{param.target}"/>
        </p>
        <p>
            <h:outputText id="outputText4" value="'form1:inputText1' (name) ==> #{param['form1:inputText1']}"/>
        </p>
        <h:form id="form1">
            <h:commandButton id="commandButton1" type="submit" value="Back" action="index.xhtml"/>
        </h:form>
    </h:body>

Servlet Response

PROCESSSERVLET.JAVA



/*
 *  Copyright 2011 Blue Lotus Software, LLC.
 *  Copyright 2011 John Yeary <jyeary@bluelotussoftware.com>.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
/*
 * $Id: ProcessServlet.java 324 2011-01-05 03:02:41Z jyeary $
 */
package com.bluelotussoftware.example.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * An example servlet which reads the {@link javax.servlet.http.HttpServletRequest} and
 * displays the values on an HTML page, or if the parameter 'target' is set, it will
 * forward the {@link javax.servlet.http.HttpServletRequest} and
 * {@link javax.servlet.http.HttpServletResponse}.
 * 
 * @author John Yeary <jyeary@bluelotussoftware.com>
 * @version 1.0
 */
public class ProcessServlet extends HttpServlet {

    private static final long serialVersionUID = -380096497389125142L;

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        if (request.getParameter("target") != null) {
            System.out.println("TARGET: " + request.getParameter("target"));
            RequestDispatcher dispatcher = request.getRequestDispatcher(request.getParameter("target"));
            dispatcher.forward(request, response);
        } else {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            try {

                out.println("<html>");
                out.println("<head>");
                out.println("<title>Servlet ProcessServlet</title>");
                out.println("</head>");
                out.println("<body>");
                out.println("<h1>Servlet ProcessServlet at " + request.getContextPath() + "</h1>");

                out.println("<h2>Headers</h2>");
                Enumeration<String> headers = request.getHeaderNames();

                while (headers.hasMoreElements()) {
                    String s = headers.nextElement();
                    out.println("<p>" + s + " : " + request.getHeader(s) + "</p>");
                }

                out.println("<h2>Attributes</h2>");
                Enumeration<String> attributes = request.getAttributeNames();

                while (attributes.hasMoreElements()) {
                    String s = attributes.nextElement();
                    out.println("<p>" + s + " : " + request.getAttribute(s) + "</p>");
                }

                out.println("<h2>Parameters</h2>");
                Enumeration<String> parameters = request.getParameterNames();

                while (parameters.hasMoreElements()) {
                    String s = parameters.nextElement();
                    out.println("<p>" + s + " : " + request.getParameter(s) + "</p>");
                }

                out.println("</body>");
                out.println("</html>");

            } finally {
                out.close();
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}

INDEXBEAN.JAVA



/*
 *  Copyright 2011 Blue Lotus Software, LLC.
 *  Copyright 2011 John Yeary <jyeary@bluelotussoftware.com>.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
/*
 * $Id: IndexBean.java 324 2011-01-05 03:02:41Z jyeary $
 */
package com.bluelotussoftware.example.jsf.bean;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * {@link javax.faces.bean.ManagedBean} for the {@literal index.xhtml} which is
 * used for demonstration of forwarding {@link javax.servlet.http.HttpServletRequest}
 * and {@link javax.servlet.http.HttpServletResponse} to an external servlet.
 *
 * @author John Yeary <jyeary@bluelotussoftware.com>
 * @version 1.0
 */
@ManagedBean
@RequestScoped
public class IndexBean {

    /**
     * Field for name.
     */
    private String name;

    /**
     * Default Constructor
     */
    public IndexBean() {
    }

    /**
     * Getter for name field
     * @return name provided.
     */
    public String getName() {
        return name;
    }

    /**
     * Setter for name field.
     * @param name value provided.
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Forwards the {@link javax.servlet.http.HttpServletRequest} and
     * {@link javax.servlet.http.HttpServletResponse} to a servlet URL
     * pattern called {@literal /process}, and sets the {@link javax.faces.context.FacesContext#responseComplete()}.
     */
    public void forward() {
        try {
            FacesContext ctx = FacesContext.getCurrentInstance();
            ExternalContext ectx = ctx.getExternalContext();
            HttpServletRequest request = (HttpServletRequest) ectx.getRequest();
            HttpServletResponse response = (HttpServletResponse) ectx.getResponse();
            RequestDispatcher dispatcher = request.getRequestDispatcher("/process");
            dispatcher.forward(request, response);
            ctx.responseComplete();
        } catch (ServletException ex) {
            Logger.getLogger(IndexBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(IndexBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

Sunday, January 02, 2011

JSF 2.x ComponentSystemEvent UML Class Diagram

I have been working on creating examples of how to use the ComponentSystemEvent model. As a result, I reverse engineered the model in NetBeans and thought someone may find it useful. Once I have the examples completed, I will publish them.


Saturday, January 01, 2011

JSF 2.x Component prerender() (PreRenderComponentEvent)

In Project Woodstock (Visual JSF), the page bean had a number of methods which could be overridden to provide additional functionality by adding convenient interception points in the JSF life cycle. This additional functionality was one of the selling points of Woodstock (Project Rave). If you are using JSF 1.2, it is still compelling.

One method which was very important was the prerender() method. This method was invoked prior to the page being rendered on the client. The com.sun.rave.web.ui.appbase.AbstractPageBean has a very complete explanation:



/**
* Callback method that is called just before rendering takes place.
* This method will only be called for the page that
* will actually be rendered (and not, for example, on a page that
* handled a postback and then navigated to a different page). Customize
* this method to allocate resources that will be required for rendering
* this page.
*/

This allowed for late binding of the component. This functionality has been added to JSF 2.0 in the SystemEvent facility. It is captured in two interfaces: PreRenderComponentEvent and PreRenderViewEvent. I will focus on the first interface, and provide an example for you.

This functionality is accomplished using the <f:event/> tag.  In this series of examples we have an <h:outputText/> which has its value injected displaying the event which was called, and a second more useful example which pre-populates <f:selectItems/> for use in an <h:selectOneListbox/> which also includes <f:ajax/> to display the selected value.


event.xhtml



<?xml version="1.0" encoding="UTF-8"?>
<!--
 Copyright 2010 Blue Lotus Software, LLC.
 Copyright 2010 John Yeary <jyeary@bluelotussoftware.com>.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 under the License.
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>&lt;f;event/&gt;</title>
    </h:head>
    <h:body>
        <h:form id="form1">
            <h:panelGrid id="panelGrid1" columns="1">
                <h:outputText id="outputText1">
                    <f:event id="event1" type="javax.faces.event.PreRenderComponentEvent" listener="#{eventBean.preRenderComponent}"/>
                </h:outputText>
                <h:selectOneListbox id="selectOneListbox" value="#{eventBean.selectedItem}">
                    <f:event id="event2" type="javax.faces.event.PreRenderComponentEvent" listener="#{eventBean.updateSelectItems}"/>
                    <f:ajax id="ajax1" render="outputText2"/>
                </h:selectOneListbox>
                <h:outputText id="outputText2" value="#{eventBean.selection}"/>
            </h:panelGrid>
        </h:form>
    </h:body>
</html>


EventBean.java



/*
 *  Copyright 2010 Blue Lotus Software, LLC.
 *  Copyright 2010 John Yeary <jyeary@bluelotussoftware.com>.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
/*
 * $Id:$
 */
package com.bluelotussoftware.example.jsf;

import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIOutput;
import javax.faces.component.UISelectItems;
import javax.faces.component.UISelectOne;
import javax.faces.event.ComponentSystemEvent;
import javax.faces.model.SelectItem;

/**
 * This is the {@link javax.faces.bean.ManagedBean} for the {@literal event.xhtml}
 * page. It is provided to demonstrate the {@literal <f:event/>} tag and its usage
 * in handling {@link javax.faces.event.PreRenderComponentEvent} events to populate
 * the representative components with values.
 * 
 * @author John Yeary <jyeary@bluelotussoftware.com>
 * @version 1.0
 */
@ManagedBean
@RequestScoped
public class EventBean {

    /**
     * value holder for the selected {@link javax.faces.component.html.HtmlSelectOneListbox}.
     */
    private String selectedItem;

    /**
     * Default constructor.
     */
    public EventBean() {
    }

    /**
     * Getter for the selected {@link javax.faces.component.html.HtmlSelectOneListbox}.
     * @return selected value.
     */
    public String getSelectedItem() {
        return selectedItem;
    }

    /**
     * Setter for the selected {@link javax.faces.component.html.HtmlSelectOneListbox}.
     * @param selectedItem set the component value.
     */
    public void setSelectedItem(String selectedItem) {
        this.selectedItem = selectedItem;
    }

    /**
     * Event Handler for {@link javax.faces.event.ComponentSystemEvent}. The usage here captures
     * the higher level event, but we are using it to capture {@link javax.faces.event.PreRenderComponentEvent}
     * and set the appropriate values.
     * @param event triggering component event to handle.
     */
    public void preRenderComponent(ComponentSystemEvent event) {
        UIOutput output = (UIOutput) event.getComponent();
        output.setValue(event.getClass().getName() + " fired.");
    }

    /**
     * Event Handler for {@link javax.faces.event.ComponentSystemEvent}. This method
     * sets the {@link javax.faces.component.UISelectItems} prior to the component being
     * rendered.
     * @param event triggering component event to handle.
     */
    public void updateSelectItems(ComponentSystemEvent event) {
        ArrayList<SelectItem> items = new ArrayList<SelectItem>();
        items.add(new SelectItem("Tuna", "Tuna"));
        items.add(new SelectItem("Salmon", "Salmon"));
        items.add(new SelectItem("Mojarra", "Mojarra", "A delicious fish, and UI framework."));
        items.add(new SelectItem("Tilapia", "Tilapia", null, true));
        UISelectItems selectItems = new UISelectItems();
        selectItems.setId("selectItems1");
        selectItems.setValue(items);

        UISelectOne uISelectOne = (UISelectOne) event.getComponent();
        uISelectOne.getChildren().add(selectItems);
    }

    /**
     * Returns the selected value.
     * @return selected value.
     */
    public String getSelection() {
        if (selectedItem != null) {
            return selectedItem;
        }
        return "Nothing Selected";
    }
}
}

Here is the result:


Popular Posts