【发布时间】:2012-03-31 18:00:19
【问题描述】:
在我的 Web 应用程序(Apache Tomcat 7、JSP、Servlet)中,我使用了过滤器。在doChain 方法中,我输入了以下代码:
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
System.out.println(req.getServletPath());
//... until doFilter
}
现在,我不明白为什么有时在控制台中我打印了 JSP 中包含的 JavaScript 文件路径(任何 *.js 文件)。 *.css 文件不会发生这种情况,只有 *.js 文件会发生这种情况。更重要的是,我每次访问 Servlet 时都不会打印 *.js 文件。我觉得那是随机打印的。
有人对此有解释吗? 为什么会随机发生?
谢谢。
编辑:实际上它也发生在 *.png 文件中。但考虑到这个问题的随机性,其他文件(包括 CSS)的行为可能相同。
注意:
- 我尝试根据页面路径进行身份验证,这就是我尝试这样做的原因。问题是我不想考虑 *.js 页面。
- 如果您想要一些代码,请告诉我。
更新
servlet 中的一个通用代码:
@WebServlet("/site/network")
public class NetworkSettingsServlet extends HttpServlet {
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
//... business logic goes here
request.getRequestDispatcher("/site/network/network_view.jsp").forward(
request, response);
//TODO log this authentication
}
}
还有 network_view.jsp 代码:
<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@include file="/site/fragments/lang.jspf"%>
<fmt:setLocale value="${siteLanguage}" />
<fmt:setBundle basename="i18n.network.network" var="networkBundle" />
<fmt:setBundle basename="i18n.home.home" var="homeBundle" />
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="/netnfork/stylesheets/commons.css" />
<script type="text/javascript" src="/netnfork/site/scripts/network/network_scripts.js"></script>
<title><fmt:message key="common_title" bundle="${homeBundle}" /></title>
</head>
<html>
html code here
</html>
这就是我的代码中的模式。
【问题讨论】:
-
是的,我们可以从
web.xml中看到 servlet 映射。另外,如果你直接从浏览器或者curl访问js文件,消息是不是也随机打印出来的? -
@TomaszNurkiewicz 当我直接访问 JSP 文件时,我没有将 *.js 文件打印到控制台,但由于某些逻辑,JSP 会抛出错误。无论如何,这不应该影响我感兴趣的行为。与
web.xml相关:如您所见,我使用Servlet3.0注释。
标签: jsp servlets servlet-filters