这里的第一个案例是修改字符编码的过滤器,但是实际上我编写的这个留言板并没有存在乱码的问题。
index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>系统登录</title>
</head>
<body>
<form action="login.jsp" method="post">
<table>
<tr>
<td class="td1">用户名</td>
<td><input type="text" class="input1" name="username"></td>
</tr>
<tr>
<td class="td1">密码</td>
<td><input type="password" class="input1" name="password"></td>
</tr>
<tr>
<td class="td1" colspan="2">
<input type="checkbox" name="remember" value="true" checked="checked"> 记住用户名</td>
</tr>
<tr>
<td colspan="2">
<div class="btn-red">
<input type="submit" value="登录" id="login-btn">
</div>
</td>
</tr>
</table>
</form>
</body>
</html>
login.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
session.setAttribute("username", username);
response.sendRedirect(request.getContextPath()+"/message.jsp");
%>
</body>
</html>
message.jsp
这里的session我改成了application是因为我把留言的功能封装成了一个servlet addMessage,但是没有找到session的作用域,就用了application的作用域,如果用application的作用域这里会出现一个问题,数据共享。就类似于数据库的功能了哈哈。
然后我现在找到了怎么设置session了,ServletRequest request = new ServletRequest;然后request.getSession()就是那个session了。
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.util.*,model.*" %>
<%
String user = (String)session.getAttribute("username");
String subFlag = request.getParameter("subFlag");
// List <Message> messages = (List)application.getAttribute("messages");
List <Message> messages = (List)session.getAttribute("messages");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>留言板</title>
<script type="text/javascript">
var subFlag = "<%=subFlag %>";
if(subFlag=='1'){
alert("留言成功!");
}
</script>
</head>
<body>
<!-- <form action="<%=request.getContextPath() %>/addMessage"> -->
<form action="messageSub.jsp">
<div align="center">
<h1>留言板</h1>
<lable>
留言人<input id="user" type="text" name="user" value="<%=user %>" readonly>
</lable>
<lable>
标题<input id="user" type="text" name="title" >
</lable>
<lable>
内容<input id="user" type="text" name="content">
</lable>
<input type="submit" value="提交">
</div>
<hr>
<hr>
<table border="1" align="center">
<tr>
<th>留言人</th>
<th>标题</th>
<th>内容</th>
</tr>
<%
if(messages !=null){
for(Message message:messages){
%>
<tr>
<td><%=user %></td>
<td><%=message.getTitle() %></td>
<td><%=message.getContent() %></td>
</tr>
<%
}
}
%>
</table>
</form>
</body>
</html>
messageSub.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.util.*,model.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
String title = request.getParameter("title");
String content = request.getParameter("content");
Message message = new Message();
message.setTitle(title);
message.setContent(content);
//从session中获取messages属性
//ServletContext context = this.getServletContext();
//List<Message> messages =(List)context.getAttribute("messages");
List<Message> messages =(List)session.getAttribute("messages");
if(messages == null){
//如果session中没有messages属性,创建一个,并且把当前的messages赋值给他
messages = new ArrayList<Message>();
// context.setAttribute("messages", messages);
session.setAttribute("messages", messages);
}
//messages队列中添加获取到的message
messages.add(message);
response.sendRedirect(request.getContextPath()+"/message.jsp?subFlag=1");
System.out.println(title+content);
%>
</body>
</html>
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_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>FilterDemo</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<!-- 注册过滤器 -->
<filter-name>characterEncodingFilter</filter-name>
<filter-class>filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>charset</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter>
<!-- 注册过滤器 -->
<filter-name>SessionFilter</filter-name>
<filter-class>filter.SessionFilter</filter-class>
</filter>
<filter-mapping>
<!-- 指定过滤器映射,对所有的请求都使用characterEncodingFilter的过滤器 -->
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<!-- 用户登录安全控制过滤器配置,用户在留言时进行过滤 -->
<filter-name>SessionFilter</filter-name>
<url-pattern>/message.jsp</url-pattern>
</filter-mapping>
</web-app>
CharacterEncodingFilter
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;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CharacterEncodingFilter implements Filter {
//创建一个类属性
private FilterConfig config;
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
// TODO Auto-generated method stub
System.out.println("我要开始过滤啦");
//这里的获取编码格式是xml文件中的initparam名为charset的value
request.setCharacterEncoding(config.getInitParameter("charset"));
//通知web服务器,拦截处理已完成,请进行下一步操作,请进行下一步操作
chain.doFilter(request, response);
}
@Override
public void destroy() {
Filter.super.destroy();
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
this.config = filterConfig;
}
}
SessionFilter
package filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SessionFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest hrequest = (HttpServletRequest)request;
HttpServletResponse hresponse = (HttpServletResponse)response;
String userName = (String)hrequest.getSession().getAttribute("username");
if(userName == null) {
hresponse.sendRedirect(hrequest.getContextPath()+"/index.jsp?flag=1");
return ;
}else {
chain.doFilter(hrequest, hresponse);
return ;
}
}
}
过滤器配置
REQUEST
INCLUDE
通过include访问方式访问所对应的url-pattern,
FORWARD
通过forward调用的
ERROR
通过异常过来的,使用error过滤器
同时,配置文件中可以写