简体   繁体   English

如何在Servlet中设置会话变量并在JSP中获取它?

[英]How can I set a session variable in a servlet and get it in a JSP?

I'm learning java and try to pass some variable from servlet to jsp page. 我正在学习Java,并尝试将一些变量从servlet传递到jsp页面。 Here is code from servlet page 这是servlet页面中的代码

@WebServlet("/Welcome")
public class WelcomeServlet extends HttpServlet
{
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)      throws ServletException, IOException
    {
        HttpSession session = request.getSession();
        session.setAttribute("MyAttribute", "test value");

        // response.sendRedirect("index.jsp");
        RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp");
        dispatcher.forward(request, response);
    }

}

And simple jsp 和简单的jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>My Index page</title>
</head>
<body>
Index page
<br />
 <% 
Object sss = request.getAttribute("MyAttribute"); 
String a = "22";

%>

   <%= request.getAttribute("MyAttribute"); %>
</body>
</html>

Whatever I do attribete at jsp is null. 我在jsp上所做的任何操作都为null。

What is wrong at this simple code? 这个简单的代码有什么问题?

you are getting if from request not session. 您正在从请求而不是会话中获取。

It should be 它应该是

session.getAttribute("MyAttribute")

I suggest you to use JavaServer Pages Standard Tag Library or Expression Language instead of Scriplet that is more easy to use and less error prone. 我建议您使用JavaServer Pages标准标记库表达式语言,而不要使用Scriplet ,因为它更易于使用,并且不易出错。

${sessionScope.MyAttribute}

or 要么

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<c:out value="${sessionScope.MyAttribute}" />

you can try ${MyAttribute} , ${sessionScope['MyAttribute']} as well. 您也可以尝试${MyAttribute}${sessionScope['MyAttribute']}

Read more 阅读更多

You set an attribute in a session . 您在会话中设置属性。 You have to retrieve it from a session : 您必须从会话中检索它:

Object sss = session.getAttribute("MyAttribute");

And since you are dispatching the request, you actually don't need a session. 而且由于您正在分派请求,因此实际上不需要会话。 You can set the attribute in a request object in your servlet: 您可以在Servlet的请求对象中设置属性:

request.setAttribute("MyAttribute", "test value");

Then read it as you are already doing in you JSP. 然后像在JSP中一样阅读它。

You should avoid scriptlets because they are java code in HTML, they break MVC pattern, they are ugly, odd and deprecated. 您应该避免使用scriptlet,因为它们是HTML中的Java代码,它们破坏了MVC模式,它们丑陋,奇数和不推荐使用。

Simply replace : 只需替换:

<% 
Object sss = request.getAttribute("MyAttribute"); 
String a = "22";

%>

with simply using EL ${MyAttribute} 只需使用EL ${MyAttribute}

But if you want to stick with scriptlets, you should get the attribute from the proper scope which is session in your case. 但是,如果您要坚持使用scriptlet,则应该从适当的范围(在您的情况下为session中获取属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM