为什么每次刷新index.jsp都会执行两次doFilter
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String flag = request.getParameter("flag");
%>
<!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=UTF-8">
<title>用户登录</title>
<link href="form.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
var flag = '<%=flag %>';
if("1"==flag){
alert("请登录系统!");
}
</script>
</head>
<body>
<form action="login.jsp" method="post" class="smart-green">
<h1>系统登录</h1>
<label>
<span>用户名:</span>
<input id="username" type="text" name="username"/>
</label>
<label>
<span>密码:</span>
<input id="password" type="password" name="password"/>
</label>
<span> </span>
<label>
<input type="submit" class="button" value="登录"/>
</label>
</form>
</body>
</html>
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
session.setAttribute("loginUser", username);
response.sendRedirect(request.getContextPath()+"/message.jsp");
%>
message.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.* , model.*"%>
<%
String user = (String) session.getAttribute("loginUser");
String subFlag = request.getParameter("subFlag");
List<Message> messages = (List<Message>) session.getAttribute("messages");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>留言板</title>
<link href="form.css" rel="stylesheet" type="text/css" />
<link href="table.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
var subFlag = '<%=subFlag%>';
if ('1' == subFlag) {
alert('留言成功');
}
</script>
</head>
<body>
<form action="messageSub.jsp" method="post" class="smart-green">
<h1>留言板</h1>
<label> <span>留言人 :</span> <input id="user" type="text"
name="user" value="<%=user%>" readonly />
</label> <label> <span>标题 :</span> <input id="title" type="text"
name="title" value="" />
</label> <label> <span>内容 :</span> <textarea id="content"
name="content"></textarea>
</label> <span> </span> <label> <input type="submit"
class="button" value="提交" />
</label>
</form>
<br />
<table id="table-3" width="85%" align="center">
<tr>
<th width="15%">留言人</th>
<th width="15%">标题</th>
<th width="70%">内容</th>
</tr>
<%
if (messages != null) {
for (Message message : messages) {
%>
<tr>
<td><%=user%></td>
<td><%=message.getTitle()%></td>
<td><%=message.getContent()%></td>
</tr>
<%
}
}
%>
</table>
</body>
</html>
messageSub.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*,model.*"%>
<%
String title = request.getParameter("title");
String content = request.getParameter("content");
Message message = new Message();
message.setTitle(title);
message.setContent(content);
List<Message> messages = (List<Message>)session.getAttribute("messages");
if(messages == null){
messages = new ArrayList<Message>();
session.setAttribute("messages", messages);
}
messages.add(message);
response.sendRedirect(request.getContextPath()+"/message.jsp?subFlag=1");
%>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>Filter</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Servlet的url-pattern只会成功匹配到一个Servlet; Filter的匹配原则和Servlet有些不同,只要url-pattern匹配成功,就会调用这些Filter -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>SystemName</param-name>
<param-value>filter Encoding</param-value>
</init-param>
<init-param>
<param-name>Version</param-name>
<param-value>2</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<!-- 映射的配置 /* 对所有请求都做处理 .jsp,.js和*.html 这是具体拦截请求的写法 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
CharacterEncodingFilter.java
package filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class CharacterEncodingFilter implements Filter {
private FilterConfig config;
@Override
public void destroy() {
System.out.println("destroy");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)// 过滤器链对象chain
throws IOException, ServletException {
System.out.println("doFilter");
//过滤器拦截请求已经完成,可以进行下一步操作,执行下一个过滤器
System.out.println(request.getServletContext().getContextPath());
String sys = config.getInitParameter("SystemName");
String ver = config.getInitParameter("Version");
System.out.println(sys);
System.out.println(ver);
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig config) throws ServletException {
this.config=config;
System.out.println("init");
}
public FilterConfig getConfig() {
return config;
}
public void setConfig(FilterConfig config) {
this.config = config;
}
}
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星