title: "HTTP Status 500 - org.apache.catalina.session.StandardSessionFacade cannot be cast to javax.servlet.ServletRequest" error related to doPost method
body: <p>I got a search.jsp page which I want to route to a doPost method inside a ControllerServlet.java class, but I keep getting an error saying "HTTP Status 500 - org.apache.catalina.session.StandardSessionFacade cannot be cast to javax.servlet.ServletRequest.</p>
  
  <p>The error:</p>
  
  <pre><code>HTTP Status 500 - org.apache.catalina.session.StandardSessionFacade cannot be cast to javax.servlet.ServletRequest
  
  type Exception report
  
  message org.apache.catalina.session.StandardSessionFacade cannot be cast to javax.servlet.ServletRequest
  
  description The server encountered an internal error that prevented it from fulfilling this request.
  
  exception
  
  java.lang.ClassCastException: org.apache.catalina.session.StandardSessionFacade cannot be cast to javax.servlet.ServletRequest
      edu.unsw.comp9321.ControllerServlet.doPost(ControllerServlet.java:95)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
  note The full stack trace of the root cause is available in the Apache Tomcat/7.0.42 logs.
  </code></pre>
  
  <p>Anyone has any idea what is wrong?</p>
  
  <p>This is the form code for the search.jsp file:</p>
  
  <pre><code>&lt;!-- HTML for SEARCH BAR --&gt;
  &lt;div id="header"&gt;
  &lt;center&gt;
      &lt;form id="new-search" method="post" action="search"&gt;
          &lt;!-- hidden input tag to identify this form action as search --&gt;
          &lt;input type="hidden" name="action" value="search"&gt;&lt;/input&gt;
  
          &lt;input type="text" name="content" class="searchtextinput" size="80" maxlength="100" placeholder="Search Artist, Title, Albums or Songs..."&gt;&lt;/input&gt;
          &lt;input type="submit" value="search" class="searchbutton"&gt;&lt;/input&gt;
  
          &lt;div&gt;&amp;nbsp;&lt;/div&gt;
          &lt;div&gt;Advanced Search:
          &lt;select name="options"&gt;
              &lt;option value="anything"&gt;Anything&lt;/option&gt;
              &lt;option value="album"&gt;Album&lt;/option&gt;
              &lt;option value="artist"&gt;Artist&lt;/option&gt;
              &lt;option value="songs"&gt;Songs&lt;/option&gt;
          &lt;/select&gt;
          &lt;/div&gt;
  
      &lt;/form&gt;
  &lt;/center&gt;
  &lt;div class="searchclear"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>This is the ControllerServlet which I want to route the search results through into the doPost method:</p>
  
  <pre><code>@WebServlet(urlPatterns="/search", displayName="ControllerServlet")
  public class ControllerServlet extends HttpServlet {
      private static final long serialVersionUID = 1L;
      Logger logger = Logger.getLogger(this.getClass().getName());
  
      /**
       * @see HttpServlet#HttpServlet()
       */
      public ControllerServlet() {
          super();
          // TODO Auto-generated constructor stub
      }
  
      /**
       * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          // TODO Auto-generated method stub
  
          String action = ((ServletRequest) request.getSession()).getParameter("action");
  
          System.out.println("Action: " + action);
  
          // get the songs and albums objects containing data needed from session
          ArrayList&lt;SongBean&gt; songs = (ArrayList&lt;SongBean&gt;) request.getSession().getAttribute("songs");
          ArrayList&lt;AlbumBean&gt; albums = (ArrayList&lt;AlbumBean&gt;) request.getSession().getAttribute("albums");
  
          // initialize nextPage empty String
          String nextPage = "";
  
          if (action.equals("search")) {
              String content = request.getParameter("content");
              String options = request.getParameter("options");
  
                      // when nothing entered in search box
                      if (content.length() == 0) {
                          nextPage = "search.jsp";
                          System.out.println("Nothing");
                      // when something entered in search box
                      } else {
                          // check what Advanced Search option is selected (default = Anything)
                          if (options.equals("Songs")) {
                              System.out.println("Songs selected");
                              // setting the context so results.jsp knows what tables to return
                              request.getSession().setAttribute("searchType", "songs");
                              ArrayList&lt;SongBean&gt; resultsArray = new ArrayList&lt;SongBean&gt;();
  
                              for (SongBean song : songs) {
                                  if (song.getTitle().contains(content)) {
                                         resultsArray.add(song);
                                     }
                              }
  
                              request.getSession().setAttribute("searchresults", resultsArray);
                      }
  
                      // need to getSession as well to make results available to all in session
  
                      }
                  }
          RequestDispatcher requestdispatcher = request.getRequestDispatcher("/results.jsp");
  
          requestdispatcher.forward(request, response);
      }
  
  }
  </code></pre>