Friday, November 02, 2012

Servlet 3.0 and JSF 2.x Tip of the Day: JSF Forward to External Servlet in Different Context

I posted a previous postServlet 3.0 Tip: How Do I Get to Another Servlet Context? which demonstrates how to get to another context. This extends that discussion by showing how to forward a JSF request to the other context and have the servlet output the results. Here is the basic code to be called from the JSF page.

1
2
3
4
5
<h:form id="form1">
    <h:inputText id="inputText1" value="#{indexBean.name}"/>
    <h:commandButton id="commandButton1" value="Submit"
                     action="#{indexBean.doForward('/XYZ','/CrossAppServlet')}"/>
</h:form>
Here is the method.

1
2
3
4
5
6
7
8
9
10
public String doForward(String targetContext, String targetServlet) throws ServletException, IOException {
       FacesContext fc = FacesContext.getCurrentInstance();
       ExternalContext ec = fc.getExternalContext();
       ServletContext sc = (ServletContext) ec.getContext();
       ServletContext scx = sc.getContext(targetContext);
       RequestDispatcher rd = scx.getRequestDispatcher(targetServlet);
       rd.forward((HttpServletRequest) ec.getRequest(), (HttpServletResponse) ec.getResponse());
       fc.responseComplete();
       return null;
   }
Here is the servlet I called.

CrossAppServlet.java


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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package servlet;
 
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 *
 * @author John Yeary
 */
@WebServlet(name = "CrossAppServlet", urlPatterns = {"/CrossAppServlet"})
public class CrossAppServlet extends HttpServlet {
 
    /**
     * 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 {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet CrossAppServlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet CrossAppServlet at " + request.getContextPath() + "</h1>");
 
            for (String s : request.getParameterMap().keySet()) {
                out.println("<p>Parameter=" + s + " value=" + request.getParameter(s) + "</p>");
            }
 
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }
 
    /**
     * 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);
    }
}

Success

Note: If you were going to pass it to JSF, you would need to remove the view state since the Faces Servlet receiving the values would result in a ViewExpiredException since this view state is not in the target Faces Servlet.

0 comments :

Popular Posts