Announcement Announcement Module
Collapse
No announcement yet.
Controller Reading New Session Each Time Err Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Controller Reading New Session Each Time Err

    I have a controller that I've written as an interface to a searching service within our web application. I think it's all "right", but for some reason the thing is writing an attribute out to the session the first time through, but a *different* session on subsequent calls. I've been going around and around in circles on this, but getting nowhere! I figure either I'm missing something simple or the complex configuration for the project is messing me up.

    This "page" calls the search.htm which is mapped to go to the SearchController, submitting the searchInput (keywords to search on). The Apache we're using to access this Tomcat is tacking on an opco parameter with mod_rewrite, so it's there, just not "visible".

    HTML Code:
    <div id="search">
    	<form id="searchForm" method="get" action="/search.htm" >
    		<label for="searchInput">
                <fmt:message key="label.common.SEARCH"/>
            </label>
            <input id="searchInput" name="searchInput" type="text" maxlength="80" />
    		<input class="form-input-submit" type="image"
                   src="/images/common/buttons/arrow_search.gif"
                   alt="Search this site" />
    	</form>
    </div>
    This is the controller, pretty basic, I think (modeled on ViewCategoryController from jpetstore). I'm skipping the imports for right now since I'm pretty sure they're not relevant, but I can put them back if anyone thinks they matter. The ISearchService is an interface to a class I wrote that does a back-end search. Basically I'm just reading the opco and searchInput parameters off the request, doing a lookup, and trying to store that back on the session. That all seems to work, but when I come back through (via a next/prev link on the results page), it doesn't find the searchResults param because it's trying to read from a different session.

    Code:
    public class SearchController implements Controller{
    	Log logger = LogFactory.getLog(SearchController.class);
    
    	private Map<String, ISearchService> opcoSvcMap;
    	
    	private String resultView;
    	private Log log = LogFactory.getLog(SearchController.class);
    	private int pageSize;
    	
    	public void setResultView(String url) {
    		this.resultView = url;
    	}
    	
    	public void setOpcoSvcMap(Map<String, ISearchService> svcMap) {
    		this.opcoSvcMap = svcMap;
    	}
    	
    	public void setPageSize( int size ) {
    		this.pageSize = size;
    	}
    	
    	public ModelAndView handleRequest(HttpServletRequest req,
    			                         HttpServletResponse res) throws Exception {
    		PagedListHolder results = null;
    		
    		String words = req.getParameter("searchInput");
    		String id = req.getParameter("opco");
    		HashMap model = new HashMap();
    		log.debug("Trying to search on " + words + " in " +
    				"opco " + id);
    		if( words != null && id != null ) {
    			/* must be doing first time through so do search & store */
    			ISearchService svc = (ISearchService) opcoSvcMap.get(id);
    			if( svc != null ) {
    				results = new PagedListHolder(svc.search(words));
    				results.setPageSize(this.pageSize);
    				req.getSession().setAttribute("searchResults", results);
    				log.debug("Setting attribute on Session: " + req.getSession().getId());
    				model.put("searchResults", results);
    			}
    			else {
    				log.error("Request trying to search with OPCO: " + id);
    			}
    		}
    		else {
    			/* check page param and deal with that */
    			String page = req.getParameter("page");
    			log.debug("About to get attribute on session: " + req.getSession().getId());
    			PagedListHolder searchResults = (PagedListHolder) req.getSession().getAttribute("searchResults");
    			
    			/* are we on first page, last page, or regular page */
    			if( "next".equals(page) ) {
    				searchResults.nextPage();
    			}
    			else if( "previous".equals(page) ) {
    				searchResults.previousPage();
    			}
    			model.put("searchResults", searchResults);
    		}
    		return new ModelAndView(resultView, model);
    	}
    
    }
    The controller then forwards to this "page". The "page" just tries to iterate through the searchResults passed in and provides a "next"/"previous" link as appropriate. (We were *given* this design--pulls in a couple of "page fragments" like this for a each page)
    HTML Code:
    <div class="page-content" id="searchResult">
    <c:forEach items="${searchResults.pageList}" var="hit">
      ${hit.title}
    </c:forEach>
    <c:if test="${!searchResults.firstPage}">
        <a href="?page=previous"><em>Prev</em></a>
      </c:if>
      <c:if test="${!searchResults.lastPage}">
        <a href="?page=next"><em>Next</em></a>
      </c:if>
    </div>
    Finally, this is the configuration for the controller bean (sorry if I'm supposed to wrap this somehow--didn't see a "tag" for it). The actual file is quite large, so I'm skipping the rest for the moment. I can post the rest if it would help.

    <bean id="searchController"
    class="SearchController"
    p:resultView="search/searchResult" pageSize="2">
    <property name="opcoSvcMap">
    <map>
    <entry key="SANS" value-ref="snsSearchService" />
    <entry key="GNTL" value-ref="giantSearchService" />
    </map>
    </property>
    </bean>
    Last edited by dodgson; Jun 30th, 2008, 04:05 PM. Reason: Changed Opinionated Comments

  • #2
    Found answer

    Turns out the problem is solved by adding emptySessionPath="true" to the AJP 1.3 connector in your Tomcat server.xml file like this:

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" emptySessionPath="true" redirectPort="8443"/>

    Apparently that makes it so that only one cookie gets written no matter which servlet context you're pointing to.

    Comment

    Working...
    X