• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jstl problems

 
John Gregory
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to a jstl exercise. I have 4 radio buttons and a submit
button from which a user selects a choice:
Performance
Speed
Safety
Maintenance

From there, I go into a servlet to set the userPref. I forward
this to a jsp where I'm using a jstl choose statement. This is
done similar to the CH3Beer servlet. Regardless of which radio
button I select, I always get the default option, or I get ALL
of the options printing. Obviously, this isn't what I want.

I'm using netbeans ide. Under my libraries tab, I included the
jstl.jar library, in addition to copying it into my /WEB-INF/lib
directory, just like the Head First book recommends.

Next, on my jsp where I'm using jstl, I uncommented the appropriate
taglib comment. Still, I'm not getting the functionality I'm
expecting.

If I use scripting, everything works fine. If I use EL, not
at all.

Can anybody give me some things to try?

Thanks,

John
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you also put standard.jar in your lib directory ?
Check this : http://faq.javaranch.com/view?JstlTagLibDefinitions

And make sure your web.xml is set to the correct version.
Check this : http://faq.javaranch.com/view?ServletsWebXml

Set everything that way, and restart the container.
If it still does not work, please show us your JSP.
 
John Gregory
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Satou,

Thanks for the hints, though they didn't change anything.
I verified the info you referenced and copied the standard.jar
into the /WEB-INF/lib directory - I only had jstl.jar.

So, here's the 2 pages of my little app. Hopefully, you can
spot the error!
==============index.jsp==============
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form method="POST" action="result.do">
When buying a car, what is most important to you?<br>
<br><input type="radio" name="userPref" value="Performance" />Performance
<br><input type="radio" name="userPref" value="Safety" />Safety
<br><input type="radio" name="userPref" value="Maintenance" />Maintenance
<br><input type="radio" name="userPref" value="Speed" />Speed<br>
<input type="submit" value="Submit" />
</form>
</body>
</html>
======================

=================result.jsp==============================
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<h1>JSP Page</h1>
<% String myPref = (String) request.getAttribute("userPref");
out.print("myPref is: " + myPref);
%>
Here's some advice for you....<br>
<c:choose>
<c:when test="${myPref == 'Performance'}">
Now you can stop even if you <em>do</em> drive like a maniac.<br>
</c:when>
<c:when test="${myPref == 'Safety'}">
Our cars are the safest on the road...<br>
</c:when>
<c:when test="${myPref == 'Maintenance'}">
We have the best mechanics in town....They're <br>
cheap, too!<br>
</c:when>
<c therwise>
I feel the need, the need for speed.
</c therwise>
</c:choose>
</body>
</html>

Thanks,

John
 
Sergio Tridente
Ranch Hand
Posts: 329
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by John Gregory:
<% String myPref = (String) request.getAttribute("userPref");
out.print("myPref is: " + myPref);
%>


First, your scriptlet is not correct: you are mixing attributes with parameters. The way to retrieve the parameter using scripting would be:
<% String myPref = request.getParameter("userPref");
out.print("myPref is: " + myPref);
%>

It still won't work because you are declaring a scripting variable (java), but EL is only able to see EL implicit objects or 'objects/variables bound to one of the 4 scopes (application, session, request and page)'.

Try replacing it with the following:


or you could change your <c:choose> code to use the request parameter directly:


I hope that clears it up a little.
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very nice explanation, Sergio.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic