简体   繁体   中英

Print a value using JSP and Expression Language (EL)

I have a servlet, and I want to print some data from the servlet with a JSP file, and I have to use compulsory the Expression Language.

I have this code in the servlet:

String saludo="hi";  
req.setAttribute("exito",saludo);

And I have this in my JSP file:

${exito}

And I also tried with this:

${requestScope.exito}  

But when I try to see it with my browser (Google Chrome), instead of seeing hi , I see

${exito}

What am I doing wrong?

When you are sending information to the JSP you need to dispacth the current request to the JSP, I try the above code and I don't have any problem, this is my code:

protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    String saludo="hi";  
    req.setAttribute("exito",saludo);
    req.getRequestDispatcher("MyPage.jsp").forward(req, resp);
}

And this is the code for MyPage.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>Title</title>
</head>
<body>
    ${exito}
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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