過濾器中的doFilter方法: public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws IOException,ServletException
{
String str=request.getParameter("uname");
if(str!=null)
{
chain.doFilter(request,response);
}
else
{
str="error";
chain.doFilter(request,response);
} }form.html
<html>
<head></head>
<body>
<form action="Filterdemo.jsp" method="post">
用户名:<input type="text" name="uname">
<input type="submit" value="提交">
</form>
</body>
</html>Filterdemo.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head></head>
<body>
<%
request.setCharacterEncoding("GB2312");
%>
<%=request.getParameter("uname") %>
</body>
</html>web.xml配置
<filter>
<filter-name>guolv</filter-name>
<filter-class>jian.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>guolv</filter-name>
<url-pattern>/Filterdemo.jsp</url-pattern>
</filter-mapping>為什麽我在form.html中的文本框中沒有輸入任何值的時候,跳转到Filterdemo.jsp不会出现error,还有chain.doFilter(request,response);这句是干什么用的
{
String str=request.getParameter("uname");
if(str!=null)
{
chain.doFilter(request,response);
}
else
{
str="error";
chain.doFilter(request,response);
} }form.html
<html>
<head></head>
<body>
<form action="Filterdemo.jsp" method="post">
用户名:<input type="text" name="uname">
<input type="submit" value="提交">
</form>
</body>
</html>Filterdemo.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head></head>
<body>
<%
request.setCharacterEncoding("GB2312");
%>
<%=request.getParameter("uname") %>
</body>
</html>web.xml配置
<filter>
<filter-name>guolv</filter-name>
<filter-class>jian.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>guolv</filter-name>
<url-pattern>/Filterdemo.jsp</url-pattern>
</filter-mapping>為什麽我在form.html中的文本框中沒有輸入任何值的時候,跳转到Filterdemo.jsp不会出现error,还有chain.doFilter(request,response);这句是干什么用的
因为还要判断“”空字符串啊!chain.doFilter(request,response)这个是把相应的请求与响应转发到响应的servlet