Passing Request session object in Struts


Web tier: servlets, JSP, Web frameworks: Passing Request session object in Struts

  1. Passing Request session object in Struts (3 messages)

    This is probably a stupid newbie question, but I'm having a problem passing an object in the Request session from a Struts Action. If I place the object in "Session" scope it works fine. Everything I'm reading and the examples given show it should work fine [but it doesn't]. The error I keep receiving in my JSP is "Error 500: Cannot find bean cbnlist in any scope."

    Here's my code:

    import javax.servlet.http.HttpSession;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionError;
    import org.apache.struts.action.ActionErrors;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import java.util.List;

    public class getBrokerListingAction extends Action {

    public ActionForward execute(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws Exception {

    ActionErrors errors = new ActionErrors();
    HttpSession session = request.getSession(false);

    try {

    String search = ((BrokerListingForm)form).getSearch();
    System.out.println("Search Criteria=" + search);

    if(search == null) {search = "SMITH";}

    List rs = DatabaseLookup.personLookup(search);

    } catch (Exception e) {

    // Report the error using the appropriate name and ID.
    errors.add("name", new ActionError("id"));


    return mapping.findForward("success");

    JSP file

    <%@ page language="java" %>
    <%@taglib uri="/tags/struts-html" prefix="html"%>
    <%@taglib uri="/tags/struts-bean" prefix="bean"%>
    <%@taglib uri="/tags/struts-logic" prefix="logic"%>

    <body style="margin-top: 0px; margin-left: 0px">

    <html:errors />

    <html:form action="getBrokerListing" focus="search" >
    <html:text property="search" size="16" />
    <bean:message key="button.submit" /></html:submit>

    <logic:iterate id="bbo" name="cbnlist">
    Name: <bean:write name="bbo" property="firstname" />&nbsp;<bean:write name="bbo" property="lastname" /><br>
    Pbno: <bean:write name="bbo" property="pbno" /><br>


    <?xml version="1.0" encoding="ISO-8859-1" ?>

    <!DOCTYPE struts-config PUBLIC
              "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"


    <!-- ==================================== Data Source Configuration -->

    <!-- ======================================== Form Bean Definitions -->


           type="cbn.generic_view.BrokerListingForm" />
         <form-property name="cbnlist" type="cbn.generic_view.BrokerBaseObject" />


    <!-- ================================= Global Exception Definitions -->


    <!-- =================================== Global Forward Definitions -->

    path="" />

    <!-- =================================== Action Mapping Definitions -->

    validate="false" >
    <forward name="success" path="/pages/broker_listing.jsp" redirect="true" />
    <forward name="failure" path="/pages/licensing.jsp" redirect="true">

    <!-- ===================================== Controller Configuration -->

    <!-- ================================ Message Resources Definitions -->

           parameter="resources.application" />

    <!-- ======================================= Plug Ins Configuration -->

      <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
        <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />


    Any help would be greatly appreciated.
  2. Sorry, gang. I meant to say Request SCOPE, not session.
  3. Sorry, gang. I meant to say Request SCOPE, not session.
    In that case, here is your problem:

    <forward name="success" path="/pages/broker_listing.jsp" redirect="true" />

    If you specify redirect="true", Struts uses a client-side redirect [response.sendRedirect()]. The JSP will be invoked by a new browser request, and any data stored in the old request will be lost.

    Your options are:

    1) Store the data in session scope.
    2) Don't set redirect="true".
  4. That worked! Thanks.[ Go to top ]

    That helps a lot. Thanks Paul.