JSP在页面用<%=调用声明函数时出现HTTP 500错误
错误描述:
Eclipse在编写JSP页面时,在其中采用<%!%>方式声明了函数,然后在页面中用<%=函数名%>方式调用时,出现HTTP状态500错误,提示为:
The method print(boolean) in the type JspWriter is not applicable for the arguments (void)
如图:
源码如下:
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%!String sTodayString;void today(){Date dToday = new Date();sTodayString = "今天是"+dToday;}
%>
<%=today() %>
</body>
</html>
错误解决:
原因是:采用<%=表达式%>方式进行表达式求值输出时,不能调用没有返回值的声明函数!
因此,解决方法是:
将today修改为具有return的函数。如下:
<%!String today(){Date dToday = new Date();return "今天是"+dToday;}
%>
<%=today() %>
</body>
</html>
运行成功!