1

I've got a servlet that puts something in request but I can't call it with jstl. What am i doing wrong?

<%@ page import="beans.Patient"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri='http://java.sun.com/jstl/fmt' prefix='fmt' %>
<jsp:useBean id="patBean" class="beans.Patient" scope="session"/>
<c:set var="patientName" value="${patient.name}"/>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
test
<form action="PatientAction" method="post">
<input type="text" name="patientId" id="patientId"></input>
<input type="submit"/>
</form>

<c:out value="${patientName}" />
<c:out value="${patBean.name}" />
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
124697
  • 22,097
  • 68
  • 188
  • 315

2 Answers2

4

You don't need to bother with <useBean> when you're using JSTL, just refer to the bean directly. So if your servlet put a Patient into the session, you can get its name using:

<c:out value="${patient.name}" />
skaffman
  • 398,947
  • 96
  • 818
  • 769
  • it just comes out as text in the webpage like this: "${patient.name}" – 124697 Jan 17 '11 at 14:35
  • 2
    @user521180: That means JSP EL isn't enabled for your webapp. See http://stackoverflow.com/questions/2168832/expression-language-in-jsp-not-working/2168974#2168974 – skaffman Jan 17 '11 at 14:37
  • 1
    Thank. that worked. it turns out i wasnt using 2.4 on my web.xml – 124697 Jan 17 '11 at 14:48
  • 1
    You've got another problem: your JSTL core is declared as per JSTL 1.1/1.2, but your JSTL fmt doesn't comply it. Align it as well to comply JSTL 1.1. See also http://stackoverflow.com/questions/1896449/tag-library-descriptor/1896610#1896610 – BalusC Jan 17 '11 at 14:50
1

request attributes are accessible via the name they have been put in . If you have had request.setAttribute("foo", fooValue) then it is accessible via ${foo}. This is true if you are within the same request. This means the servlet has to do a forward rather than a redirect. If a redirect happens, that's a new request, and the old values are lost.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140