Franz17
Franz17

Reputation: 51

JSP doesn't pass values on request

I'don't understand why servlet doesn't pass values to .jsp file. If somebody could explain my, why it works only when a call request.getSession.setAtribute() method. What should i do to avoid creating session.

Servlet:

      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        moviesList(request, response);
        if (request.getParameter("command").equals("ADD"))
            addMovie(request, response);
        if (request.getParameter("command").equals("DELETE"))
            deleteMovie(request, response);
        if (request.getParameter("command").equals("SEARCH_IN_TMDB"))
            searchInTmdb(request, response);
    } catch (Exception e) {
        e.getMessage();
    }

}
  private void searchInTmdb(HttpServletRequest request, HttpServletResponse response) throws Exception {
    List<MovieDb> movieDbs;
    String title = request.getParameter("title_themoviedb");
    String year = request.getParameter("year_themoviedb");
    System.out.println("title " + title + " year " + year);
    int year_int = Integer.parseInt(year);
    movieDbs = TheMovieDbApiUtil.getInstance().getListFoundMovies(title, year_int);

    request.getSession().setAttribute("TMDB_LIST", movieDbs);
    RequestDispatcher dispatcher = request.getRequestDispatcher("/movie-list.jsp");
    dispatcher.forward(request, response);
}

JSP file:

 <div class="row top-buffer">
    <form action="ServletMovieController" method="get">
        <input type="hidden" name="command" value="SEARCH_IN_TMDB">
    <div class="col-xs-3 col-sm-3 col-md-3 col-lg-3">
    </div>
    <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
        <button type="submit">Szukaj...</button>

        <input type="text" name="title_themoviedb" title="title_themoviedb" class="form-control">

        <input type="text" name="year_themoviedb"  title="year_themoviedb"  class="form-control">
        <ul class="list-group" id="myList">
            <c:forEach var="tempMovieTmdb" items="${TMDB_LIST}">
            <li class="list-group-item">${tempMovieTmdb.getTitle()}</li>
            </c:forEach>
        </ul>
        </input>
    </div>
    <div class="col-xs-3 col-sm-3 col-md-3 col-lg-3">
    </div>
    </form>
</div>

As I mentioned before it works when I use requset.getSession(), but whitout it passed value is null.

Upvotes: 1

Views: 581

Answers (2)

Jonathan Laliberte
Jonathan Laliberte

Reputation: 2725

I think i've understood what is going wrong and it's a common mistake people make when they first start out with the servlet/jsp request flow.

As someone has already mentioned you need to swap out

request.getSession().setAttribute("TMDB_LIST", movieDbs);

with:

request.setAttribute("TMDB_LIST", movieDbs);

Now the reason why you're getting null, is because you're not running the servlet. You're trying to access the jsp directly and expecting the request variable to be there. That's not how servlet/jsp request flow works. The servlet sets the request variable "TMDB_LIST" and it forwards this variable to the jsp page ("/movie-list.jsp"). Once you go to a different page the variable expires and will be null. If you try and access the jsp directly, it will just be null because you need the servlet to pass the variable to the jsp. I'm guessing this is what you're doing or you have a redirect after reaching the jsp.

Session variables are different in that the variables you set persist across the whole application/website (until you delete them or change them), for as long as the server specifies.

Request variables are only available after the first request. (i.e. servlet passes request variables to the jsp or the jsp passes request variables to the servlet)

Upvotes: 1

J Zou
J Zou

Reputation: 108

if you want to avoid using Session, in your servlet, use

request.setAttribute("TMDB_LIST", movieDbs);

Upvotes: 0

Related Questions