示例#1
0
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    ResultSet rs = null;
    try {
      // SET UP Context environment, to look for Data Pooling Resource
      Context initCtx = new InitialContext();
      Context envCtx = (Context) initCtx.lookup("java:comp/env");
      DataSource ds = (DataSource) envCtx.lookup("jdbc/TestDB");
      Connection dbcon = ds.getConnection();

      // ########### SEARCH INPUT PARAMETERS, EXECUTE search
      // #####################################################
      Vector<String> params = new Vector<String>();
      params.add(request.getParameter("fname"));
      params.add(request.getParameter("lname"));
      params.add(request.getParameter("title"));
      params.add(request.getParameter("year"));
      params.add(request.getParameter("director"));

      List<Movie> movies = new ArrayList<Movie>();
      movies = SQLServices.getMovies(params.toArray(new String[params.size()]), dbcon);

      // ########## SET DEFAULT SESSION() PARAMETERS ####################################
      request.getSession().removeAttribute("movies");
      request.getSession().removeAttribute("linkedListMovies");
      request.getSession().removeAttribute("hasPaged");
      request.getSession().setAttribute("hasPaged", "no");
      request.getSession().setAttribute("movies", movies);
      request.getSession().setAttribute("currentIndex", "0");
      request.getSession().setAttribute("defaultN", "5");

      // ########## IF MOVIES FROM SEARCH NON-EMPTY ###########################################
      List<String> fields = Movie.fieldNames();
      int count = 1;
      if (!movies.isEmpty()) {
        request.setAttribute("movies", movies);
        for (String field : fields) {
          request.setAttribute("f" + count++, field);
        }
        request.getRequestDispatcher("../movieList.jsp").forward(request, response);
      } else {
        out.println("<html><head><title>error</title></head>");
        out.println("<body><h1>could not find any movies, try your search again.</h1>");
        out.println("<p> we are terribly sorry, please go back. </p>");
        out.println("<table border>");
        out.println("</table>");
      }
      dbcon.close();

    } catch (SQLException ex) {
      while (ex != null) {
        System.out.println("SQL Exception:  " + ex.getMessage());
        ex = ex.getNextException();
      }
    } catch (java.lang.Exception ex) {
      out.println(
          "<html>"
              + "<head><title>"
              + "moviedb: error"
              + "</title></head>\n<body>"
              + "<p>SQL error in doGet: "
              + ex.getMessage()
              + "</p></body></html>");
      return;
    }
    out.close();
  }