如何在jsp中传递String参数?

时间:2014-01-12 00:47:36

标签: java jsp

下面的代码是一个output.jsp,用于以格式显示从submit.jsp提交的数据。我是JSP的新手,想要如何将参数名字/姓氏传递给html标签的一些指导。请指教 。我尝试过几次没有成功的approcahes。

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

这是运行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>

2 个答案:

答案 0 :(得分:1)

而不是<td><% firstname %></td><td><% lastname %></td> 试试<td><%= firstname %></td><td><%= lastname %></td>

答案 1 :(得分:1)

除了您使用无效的scriptlet之外,应该避免使用

尝试使用代替

<td>${firstname}</td>

相关:How to avoid Java code in JSP files?