How to get jsp and servlet interaction.

S

Steve Burrus

I need some help/assistance from someone with this problem of mine of
trying to get a jsp which displays a table with some table data in it,
i.e., some of my favorite movies, to interact successfully with a
servlet!!! Now, in the servlet, I have used--probably for the very 1st
time ever--the servlet method "RequestDispatcher" and both of the
getAttribute() and the setAttribute() methods, but alas, when I try to
view the jsp in my web browser, I only get the jsp title showing!!! Now
whasssssssssup with that anyway???!!

Here is first my servlet code :

/* Here is a particular servlet which is used with a jsp that will
* print out a movie list.
*/

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class MovieServlet extends HttpServlet {
public void service (HttpServletRequest request, HttpServletResponse resp)
throws IOException, ServletException {
PrintWriter out = resp.getWriter();
String [] movieList = { "Patton", "Some Like It Hot", "Bank
Shot","Beyond Atlantis", "Titanic"};
java.util.List mymovie = new java.util.ArrayList();
mymovie.add(movieList);
request.setAttribute("movieList", mymovie);
String[] items = (String[]) request.getAttribute("movieList");
for(int i = 0; i < items.length; i++){
String movie = items;
out.println(movie);
}
RequestDispatcher steve = request.getRequestDispatcher("mymovies.jsp");
steve.forward(request, resp);
}
}

and then my jsp code :

<%@ taglib prefix = "c" uri="http://java.sun.com/jsp/jstl/core" %>
<html><head>
</head><body>
<b><i>My Personal Movie List</b></i><br><br><br>

<table>
<c:forEach var = "movie" items = "${movieList}"varStatus =
"movieLoopCount"> >
<tr>
<td>Count : ${movieLoopCount.count}</td>
</tr>
<tr>
<td>${movie}</td>
</tr>
<tr><td>Patton</td></tr>

</c:forEach>
</table>
</body></html>
 
K

kaeli

burrus1 said:
and then my jsp code :

I don't use jstl, but I do wonder if this is a typo...
<%@ taglib prefix = "c" uri="http://java.sun.com/jsp/jstl/core" %>
<html><head>
</head><body>
<b><i>My Personal Movie List</b></i><br><br><br>

<table>
<c:forEach var = "movie" items = "${movieList}"varStatus =
"movieLoopCount"> >

Shouldn't there be a space between the quote and varStatus?
And isn't there an extra greater-than sign there?
Try this:
<c:forEach var = "movie" items = "${movieList}" varStatus =
"movieLoopCount">

Other than that, I can't help much. Sorry.

--
 
C

Chris Riesbeck

Steve Burrus said:
I need some help/assistance from someone with this problem of mine of
trying to get a jsp which displays a table with some table data in it,
i.e., some of my favorite movies, to interact successfully with a
servlet!!!

Several problems, but not, as far as I can see, with
your use of the dispatcher.

?? notes are about poor practice not bugs as such.

!! are about bugs.
Here is first my servlet code :

import java.io.*;
import java.util.*;

?? Since you only use ArrayList and List, just import them explicitly.
Maintainers to come will thank you.
import javax.servlet.*;
import javax.servlet.http.*;

public class MovieServlet extends HttpServlet {
public void service (HttpServletRequest request, HttpServletResponse resp)
throws IOException, ServletException {

!! Don't override service(). The default service() calls doPost() or doGet().
You should override them. And only one needs a real
definition. The other just calls the method with the real code.
See

http://www.phptr.com/articles/article.asp?p=29817&seqNum=3
http://www.stardeveloper.com/articles/display.html?article=2001061901&page=1

PrintWriter out = resp.getWriter();

!! Delete. If you're passing the buck to a JSP page, it
makes no sense to write anything to the response.
String [] movieList = { "Patton", "Some Like It Hot", "Bank
Shot","Beyond Atlantis", "Titanic"};
java.util.List mymovie = new java.util.ArrayList();

?? You import ArrayList and List, so just write

List mymovie = new ArrayList();

?? That should be spelled myMovie.
myMovie.add(movieList);

!! This puts *one* element in the ArrayList, namely the array.
What's the point of that? Assuming you wanted the array elements
to become elements of the ArrayList, then import java.utils.Arrays
(note the plural) and do

List mymovie = Arrays.asList(movieList);

One step, no explicit ArrayList needed.
request.setAttribute("movieList", mymovie);

OK, though you could condense this into just

request.setAttribute("movieList", Arrays.asList(movieList));
String[] items = (String[]) request.getAttribute("movieList");
for(int i = 0; i < items.length; i++){
String movie = items;
out.println(movie);
}


!! Delete all of these lines. You shouldn't be writing to the response.
RequestDispatcher steve = request.getRequestDispatcher("mymovies.jsp");
steve.forward(request, resp);
}
}

?? There's no need for the variable steve. Just write

request.getRequestDispatcher("mymovies.jsp").forward(request, resp);

and then my jsp code :

<%@ taglib prefix = "c" uri="http://java.sun.com/jsp/jstl/core" %>
<html><head>
</head><body>
<b><i>My Personal Movie List</b></i><br><br><br>

<table>
<c:forEach var = "movie" items = "${movieList}"varStatus =
"movieLoopCount"> >

?? Standard HTML/XML style is no spaces around = and space
between a value and the next attribute. You also have a
spurious > as noted by a previous poster.

movieLoopCount is a misleading name since the variable holds
a status object which is much more than just a count. So
this should be

<tr>
<td>Count : ${movieLoopStatus.count}</td>
</tr>
<tr>
<td>${movie}</td>
</tr>
<tr><td>Patton</td></tr>

</c:forEach>
</table>
</body></html>

I may be missing something else but this looks right.

HTH
 
W

Will Hartung

Steve Burrus said:
I need some help/assistance from someone with this problem of mine of
trying to get a jsp which displays a table with some table data in it,
i.e., some of my favorite movies, to interact successfully with a
servlet!!! Now, in the servlet, I have used--probably for the very 1st
time ever--the servlet method "RequestDispatcher" and both of the
getAttribute() and the setAttribute() methods, but alas, when I try to
view the jsp in my web browser, I only get the jsp title showing!!! Now
whasssssssssup with that anyway???!!

*snip*

The basic problem is that you're storing a String array in your list. Rather
you should just store the movies in the list.

public class MovieServlet extends HttpServlet {
public void service (HttpServletRequest request, HttpServletResponse resp)
throws IOException, ServletException
{
PrintWriter out = resp.getWriter();
String [] movieList = { "Patton", "Some Like It Hot",
"Bank Shot","Beyond Atlantis", "Titanic"};
java.util.List mymovie = java.util.Arrays.asList(movieList);
request.setAttribute("movieList", mymovie);
RequestDispatcher steve = request.getRequestDispatcher("mymovies.jsp");
steve.forward(request, resp);
}
}

Then, in your JSP:
<table>
<c:forEach var = "movie" items = "${movieList}">
<tr>
<td><c:eek:ut value="${movie}"/></td>
</tr>
</c:forEach>
</table>

That should work for you.

Regards,

Will Hartung
([email protected])
 
J

Jean-Fran?ois Bri?re

'movieList' is an attribute of the request.
You must do:

<c:forEach var="movie" items="${requestScope.movieList}" varStatus="movieLoopCount">
 
C

Chris Riesbeck

'movieList' is an attribute of the request.
You must do:

<c:forEach var="movie" items="${requestScope.movieList}" varStatus="movieLoopCount">

Not necessary. JSTL's expression language automatically
searches all the scopes.

"For example, ${product} will look for the attribute named product,
searching the page, request, session, and application scopes and
will return its value."

http://java.sun.com/webservices/docs/1.0/tutorial/doc/JSTL4.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top