z atef
z atef

Reputation: 7679

How to pass String paramaters in jsp?

Below code is an output.jsp to display the data submitted from submit.jsp in a format. I am fairly new to JSP and would like some guidance in how to pass paramaters firstname/lastname into html tags. Please advise . I have tried couple of approcahes with no success.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
<h>The first and last name are</h>
<%
String firstname = (String) request.getAttribute("firstname");
String lastname  = (String) request.getAttribute("lastname");

//out.println(firstname  + "                     " + lastname);
%>
<table>
<tr>
<td>First Name : </td><td><% firstname  %></td>
</tr>
<tr>
<td>Last Name : </td><td><% lastname %></td>
</tr>
</table>
</body>
</html>

Here is the error that are thrown when running output.jsp.

An error occurred at line: 20 in the jsp file: /output.jsp
firstname cannot be resolved to a type
17: %>
18: <table>
19: <tr>
20: <td>First Name : </td><td><% firstname  %></td>
21: </tr>
22: <tr>
23: <td>Last Name : </td><td><% lastname %></td>


An error occurred at line: 23 in the jsp file: /output.jsp
lastname cannot be resolved to a type
20: <td>First Name : </td><td><% firstname  %></td>
21: </tr>
22: <tr>
23: <td>Last Name : </td><td><% lastname %></td>
24: </tr>
25: </table>
26: </body>

Upvotes: 1

Views: 87

Answers (2)

Reimeus
Reimeus

Reputation: 159754

Apart from the fact that you're using an invalid scriptlet, they should be avoided

Try using instead

<td>${firstname}</td>

Related: How to avoid Java code in JSP files?

Upvotes: 1

gefei
gefei

Reputation: 19766

instead of <td><% firstname %></td> and <td><% lastname %></td> try <td><%= firstname %></td> and <td><%= lastname %></td>

Upvotes: 1

Related Questions