I have a jsp page that i edit some info of a user. When a user logins to website, i keep the information in the session. Then in my edit page i try the following:
我有一个jsp页面,可以编辑用户的一些信息。当用户登录网站时,我将信息保存在会话中。然后在我的编辑页面,我尝试以下:
<%! String username=session.getAttribute("username"); %>
form action="editinfo" method="post">
<table>
<tr>
<td>Username: </td><td> <input type="text" value="<%=username %>" /> </td>
</tr>
</table>
but it gives error saying session cannot be resolved. What can i do about it?
但是说会话无法解决会出错。我该怎么办呢?
Thanks
谢谢
6 个解决方案
#1
76
JSP implicit objects likes session
, request
etc. are not available inside JSP declaration <%! %>
tags.
JSP隐式对象如会话、请求等在JSP声明<%中不可用!% >标记。
You could use it directly in your expression as
你可以直接在你的表达式as中使用它
<td>Username: </td>
<td><input type="text" value="<%= session.getAttribute("username") %>" /></td>
On other note, using scriptlets in JSP has been long deprecated. Use of EL (expression language) and JSTL tags is highly recommended. For example, here you could use EL as
另一方面,在JSP中使用scriptlet已经被长期反对。强烈推荐使用EL(表达式语言)和JSTL标记。例如,在这里您可以使用EL作为。
<td>Username: </td>
<td><input type="text" value="${username}" /></td>
The best part is that scope resolution is done automatically. So, here username could come from page, or request, or session, or application scopes in that order. If for a particular instance you need to override this because of a name collision you can explicitly specify the scope as
最好的部分是范围解析是自动完成的。这里用户名可以来自页面,请求,会话,或应用范围。如果由于名称冲突而需要覆盖特定实例,则可以显式地将范围指定为
<td><input type="text" value="${requestScope.username}" /></td> or,
<td><input type="text" value="${sessionScope.username}" /></td> or,
<td><input type="text" value="${applicationScope.username}" /></td>
#2
8
The reason why you are getting the compilation error is, you are trying to access the session in declaration block (<%! %>
) where it is not available. All the implicit objects of jsp are available in service method only. Code of declarative blocks goes outside the service method.
您获得编译错误的原因是,您正在尝试访问声明块中的会话(<%!%>)在不可用的地方。jsp的所有隐式对象仅在服务方法中可用。声明性块的代码位于服务方法之外。
I'd advice you to use EL. It is a simplified approach.
我建议你使用EL。这是一种简化的方法。
${sessionScope.username}
would give you the desired output.
$ { sessionScope。username}将为您提供所需的输出。
#3
6
Use
使用
<% String username = (String)request.getSession().getAttribute(...); %>
Note that your use of <%! ... %>
is translated to class-level, but request is only available in the service()
method of the translated servlet.
注意您使用的<%!…%>被转换为类级别,但是请求只能在已翻译的servlet的service()方法中使用。
See how JSP code is translated to a servlet.
查看如何将JSP代码转换为servlet。
#4
-1
<%! String username=(String)session.getAttribute("username"); %>
form action="editinfo" method="post">
<table>
<tr>
<td>Username: </td><td> <input type="text" value="<%=username %>" /> </td>
</tr>
</table>
add <%! String username=(String)session.getAttribute("username"); %>
添加< % !字符串的用户名=(字符串)session.getAttribute(“用户名”);% >
#5
-1
You can directly use (String)session.getAttribute("username"); inside scriptlet tag ie <% %>.
可以直接使用(String)session.getAttribute("username");在scriptlet中标签ie <% >。
#6
-1
form action="editinfo" method="post">
<table>
<tr>
<td>Username:</td>
<td>
<input type="text" value="<%if( request.getSession().getAttribute(" parameter_whatever_you_passed ") != null
{
request.getSession().getAttribute("parameter_whatever_you_passed ").toString();
}
%>" />
</td>
</tr>
</table>
</form>
#1
76
JSP implicit objects likes session
, request
etc. are not available inside JSP declaration <%! %>
tags.
JSP隐式对象如会话、请求等在JSP声明<%中不可用!% >标记。
You could use it directly in your expression as
你可以直接在你的表达式as中使用它
<td>Username: </td>
<td><input type="text" value="<%= session.getAttribute("username") %>" /></td>
On other note, using scriptlets in JSP has been long deprecated. Use of EL (expression language) and JSTL tags is highly recommended. For example, here you could use EL as
另一方面,在JSP中使用scriptlet已经被长期反对。强烈推荐使用EL(表达式语言)和JSTL标记。例如,在这里您可以使用EL作为。
<td>Username: </td>
<td><input type="text" value="${username}" /></td>
The best part is that scope resolution is done automatically. So, here username could come from page, or request, or session, or application scopes in that order. If for a particular instance you need to override this because of a name collision you can explicitly specify the scope as
最好的部分是范围解析是自动完成的。这里用户名可以来自页面,请求,会话,或应用范围。如果由于名称冲突而需要覆盖特定实例,则可以显式地将范围指定为
<td><input type="text" value="${requestScope.username}" /></td> or,
<td><input type="text" value="${sessionScope.username}" /></td> or,
<td><input type="text" value="${applicationScope.username}" /></td>
#2
8
The reason why you are getting the compilation error is, you are trying to access the session in declaration block (<%! %>
) where it is not available. All the implicit objects of jsp are available in service method only. Code of declarative blocks goes outside the service method.
您获得编译错误的原因是,您正在尝试访问声明块中的会话(<%!%>)在不可用的地方。jsp的所有隐式对象仅在服务方法中可用。声明性块的代码位于服务方法之外。
I'd advice you to use EL. It is a simplified approach.
我建议你使用EL。这是一种简化的方法。
${sessionScope.username}
would give you the desired output.
$ { sessionScope。username}将为您提供所需的输出。
#3
6
Use
使用
<% String username = (String)request.getSession().getAttribute(...); %>
Note that your use of <%! ... %>
is translated to class-level, but request is only available in the service()
method of the translated servlet.
注意您使用的<%!…%>被转换为类级别,但是请求只能在已翻译的servlet的service()方法中使用。
See how JSP code is translated to a servlet.
查看如何将JSP代码转换为servlet。
#4
-1
<%! String username=(String)session.getAttribute("username"); %>
form action="editinfo" method="post">
<table>
<tr>
<td>Username: </td><td> <input type="text" value="<%=username %>" /> </td>
</tr>
</table>
add <%! String username=(String)session.getAttribute("username"); %>
添加< % !字符串的用户名=(字符串)session.getAttribute(“用户名”);% >
#5
-1
You can directly use (String)session.getAttribute("username"); inside scriptlet tag ie <% %>.
可以直接使用(String)session.getAttribute("username");在scriptlet中标签ie <% >。
#6
-1
form action="editinfo" method="post">
<table>
<tr>
<td>Username:</td>
<td>
<input type="text" value="<%if( request.getSession().getAttribute(" parameter_whatever_you_passed ") != null
{
request.getSession().getAttribute("parameter_whatever_you_passed ").toString();
}
%>" />
</td>
</tr>
</table>
</form>