我无法从JSP下面传递userName并在Servlet过滤器中访问它。以下是JSP&过滤。我尝试使用请求Paramter,属性和ParameterNames访问,但只获取空值。 我已经在调试模式下运行应用程序,在按下提交按钮后它正在点击过滤器。 如果我做错了,请告诉我。 JSP:
<html>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<head>
</head>
<body>
<p><bean:message key="logonForm.info.text"/></p>
<form action="/uchpweb/login.do" name="login_form" method="post">
<div id="layLogInContainer">
<div id="layLogInFields" class="inputAreaLight">
<b>UserName</b><br>
<input type="text" name="username" id="username" tabindex="1" size="10" maxlength="10" /><br />
</div>
<div id="layLogInButtonBar" class="btnBar">
<input type="submit" name="submit" value="submit"/>
</div>
</div>
</form>
</body>
</html>`
过滤器:
public class UserFilter implements Filter {
FilterConfig config;
Boolean flag;
@SuppressWarnings("unused")
private Logger log = ResourceLocator.getInstance().getLogger(this);
public void init(FilterConfig config) {
this.config = config;
}
@SuppressWarnings("unchecked")
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpSession httpSession = httpRequest.getSession();
Enumeration<String> params = httpRequest.getParameterNames();
while(params.hasMoreElements()){
String name = params.nextElement();
String value = request.getParameter(name);
log.debug("Name : ",name);
log.debug("value : ",value);
}
if (request.getParameter("username") == null && (request.getAttribute("username") == null)) {
RequestDispatcher dispatcher1 = request.getRequestDispatcher("/logon.jsp");
if (dispatcher1 != null) {
dispatcher1.forward(request, response);
return;
}
}
}
的web.xml
<filter>
<filter-name>CommonUserContextFilter</filter-name>
<filter-class>UserContextFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CommonUserContextFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>CommonUserContextFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>CommonUserContextFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>