How to get jsp and servlet interaction.  
Author Message
Steve Burrus





PostPosted: 2004-9-22 12:36:00 Top

java-programmer, How to get jsp and servlet interaction. 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[i];
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>
 
kaeli





PostPosted: 2004-9-22 22:56:00 Top

java-programmer >> How to get jsp and servlet interaction. In article <Yi74d.23547$email***@***.com>, burrus1
@swbell.net enlightened us with...
>>
> 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.

--
 
Chris Riesbeck





PostPosted: 2004-9-23 3:08:00 Top

java-programmer >> How to get jsp and servlet interaction. In article <Yi74d.23547$email***@***.com>,
Steve Burrus <email***@***.com> wrote:

> 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[i];
> 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

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

> <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
 
 
Will Hartung





PostPosted: 2004-9-23 3:26:00 Top

java-programmer >> How to get jsp and servlet interaction.
"Steve Burrus" <email***@***.com> wrote in message
news:Yi74d.23547$email***@***.com...
> 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:out value="${movie}"/></td>
</tr>
</c:forEach>
</table>

That should work for you.

Regards,

Will Hartung
(email***@***.com)




 
 
jfbriere





PostPosted: 2004-9-23 4:49:00 Top

java-programmer >> How to get jsp and servlet interaction. 'movieList' is an attribute of the request.
You must do:

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





PostPosted: 2004-9-23 6:35:00 Top

java-programmer >> How to get jsp and servlet interaction. In article <email***@***.com>,
email***@***.com (Jean-Fran?ois Bri?re) wrote:

> '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