jsp 获取name属性值
时间: 2023-08-14 21:08:33 浏览: 279
在 JSP 中,你可以使用 EL 表达式和 JSTL 标签库获取 HTML 标签中的 `name` 属性值。
例如,假设你有以下 HTML 代码:
```html
<input type="text" name="my-name" value="Hello World!" />
```
你可以使用 EL 表达式 `${}` 来获取 `name` 属性的值,如下所示:
```jsp
<c:set var="myName" value="my-name" />
<input type="text" name="${myName}" value="Hello World!" />
<p>name: ${myName}</p>
```
在上面的代码中,`<c:set>` 标签用于将 `my-name` 字符串存储到 `myName` 变量中,然后在输入框和 `<p>` 标签中使用该变量。注意,`${}` 表达式中的变量名需要用双引号括起来。
另外,如果你想获取已经提交表单时输入框中的值,可以使用 `request.getParameter()` 方法。例如:
```jsp
<form action="my-action.jsp" method="post">
<input type="text" name="my-name" />
<button type="submit">Submit</button>
</form>
<%
String myName = request.getParameter("my-name");
out.println("myName: " + myName);
%>
```
在上面的代码中,`request.getParameter()` 方法用于获取输入框中的值,并将其存储到 `myName` 变量中。然后,使用 `out.println()` 方法将该变量的值输出到页面上。注意,这段代码需要放在 JSP 页面的 `<body>` 标签内。
阅读全文
相关推荐



请按如下要求设计一个JavaWeb应用程序 1.设计一个用于监测ServletContext对象创建和销毁的程序,类名为:CustomServletContextListener,其中当监听到ServletContext对象被创建时,向ServletContext对象中存储三个属性,第一个属性名称为:sno,属性值为:自己的真实学号;第二个属性名称为:name,属性值为:自己的真实姓名;第三个属性名称为:gender,属性值为:自己的真实性别。 2.设计一个Servlet程序,名称为:SelfInfoServlet,访问地址为:/info,在该Servlet程序中获取ServletContext对象中三个属性(sno、name、gender)的属性值,并将获得的属性值拼接成如下字符串格式:学号:XXX,姓名:XXX,性别:XXX,其中:XXX为获取到的属性值,并将拼接好的字符串作为属性值存储到request对象中属性名为selfInfo中,最后采用请求转发的方式跳转到viewInfo.jsp页面,在该JSP页面中显示request对象的selfInfo属性信息。 3.设计一个JSP页面,名称为:visit.jsp,在该页面中设置一个超链接,用于访问SelfInfoServlet程序。













