2013-12-10 111 views
1

我有一个servlet,并且我想用JSP文件打印servlet中的一些数据,并且必须使用表达式语言。使用JSP和表达式语言(EL)打印值

我有这样的代码在servlet:

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

而且我有这个在我的JSP文件:

${exito} 

而且我也试图与此:

${requestScope.exito} 

但当我试着用我的浏览器(谷歌浏览器)看到它,而不是看到hi,我看到

${exito} 

我在做什么错?

+1

[在JSP不工作表达式语言(http://stackoverflow.com/questions/2168832/expression-language-in-jsp-not-working) –

+0

的重复尝试使用JSTL include <%@ taglib prefix =“c”uri =“http://java.sun.com/jsp/jstl/core”%>然后尝试 user1769790

回答

2

当你发送信息到JSP,你需要dispacth当前请求JSP,我尝试了上面的代码,我没有任何问题,这是我的代码:

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

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

而且这是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> 
+1

非常感谢。我的问题是调度员,我有另一种方式,我试过你的代码,它工作正常。再次感谢 –