这里的第一个案例是修改字符编码的过滤器,但是实际上我编写的这个留言板并没有存在乱码的问题。

index.jsp

  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <meta charset="utf-8">
  7. <title>系统登录</title>
  8. </head>
  9. <body>
  10. <form action="login.jsp" method="post">
  11. <table>
  12. <tr>
  13. <td class="td1">用户名</td>
  14. <td><input type="text" class="input1" name="username"></td>
  15. </tr>
  16. <tr>
  17. <td class="td1">密码</td>
  18. <td><input type="password" class="input1" name="password"></td>
  19. </tr>
  20. <tr>
  21. <td class="td1" colspan="2">
  22. <input type="checkbox" name="remember" value="true" checked="checked"> 记住用户名</td>
  23. </tr>
  24. <tr>
  25. <td colspan="2">
  26. <div class="btn-red">
  27. <input type="submit" value="登录" id="login-btn">
  28. </div>
  29. </td>
  30. </tr>
  31. </table>
  32. </form>
  33. </body>
  34. </html>

login.jsp

  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  2. pageEncoding="ISO-8859-1"%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <meta charset="ISO-8859-1">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <%
  11. String username = request.getParameter("username");
  12. String password = request.getParameter("password");
  13. session.setAttribute("username", username);
  14. response.sendRedirect(request.getContextPath()+"/message.jsp");
  15. %>
  16. </body>
  17. </html>

message.jsp
这里的session我改成了application是因为我把留言的功能封装成了一个servlet addMessage,但是没有找到session的作用域,就用了application的作用域,如果用application的作用域这里会出现一个问题,数据共享。就类似于数据库的功能了哈哈。
然后我现在找到了怎么设置session了,ServletRequest request = new ServletRequest;然后request.getSession()就是那个session了。

  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <%@ page import="java.util.*,model.*" %>
  4. <%
  5. String user = (String)session.getAttribute("username");
  6. String subFlag = request.getParameter("subFlag");
  7. // List <Message> messages = (List)application.getAttribute("messages");
  8. List <Message> messages = (List)session.getAttribute("messages");
  9. %>
  10. <!DOCTYPE html>
  11. <html>
  12. <head>
  13. <meta charset="utf-8">
  14. <title>留言板</title>
  15. <script type="text/javascript">
  16. var subFlag = "<%=subFlag %>";
  17. if(subFlag=='1'){
  18. alert("留言成功!");
  19. }
  20. </script>
  21. </head>
  22. <body>
  23. <!-- <form action="<%=request.getContextPath() %>/addMessage"> -->
  24. <form action="messageSub.jsp">
  25. <div align="center">
  26. <h1>留言板</h1>
  27. <lable>
  28. 留言人<input id="user" type="text" name="user" value="<%=user %>" readonly>
  29. </lable>
  30. <lable>
  31. 标题<input id="user" type="text" name="title" >
  32. </lable>
  33. <lable>
  34. 内容<input id="user" type="text" name="content">
  35. </lable>
  36. <input type="submit" value="提交">
  37. </div>
  38. <hr>
  39. <hr>
  40. <table border="1" align="center">
  41. <tr>
  42. <th>留言人</th>
  43. <th>标题</th>
  44. <th>内容</th>
  45. </tr>
  46. <%
  47. if(messages !=null){
  48. for(Message message:messages){
  49. %>
  50. <tr>
  51. <td><%=user %></td>
  52. <td><%=message.getTitle() %></td>
  53. <td><%=message.getContent() %></td>
  54. </tr>
  55. <%
  56. }
  57. }
  58. %>
  59. </table>
  60. </form>
  61. </body>
  62. </html>

messageSub.jsp

  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <%@ page import="java.util.*,model.*" %>
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. <meta charset="utf-8">
  8. <title>Insert title here</title>
  9. </head>
  10. <body>
  11. <%
  12. String title = request.getParameter("title");
  13. String content = request.getParameter("content");
  14. Message message = new Message();
  15. message.setTitle(title);
  16. message.setContent(content);
  17. //从session中获取messages属性
  18. //ServletContext context = this.getServletContext();
  19. //List<Message> messages =(List)context.getAttribute("messages");
  20. List<Message> messages =(List)session.getAttribute("messages");
  21. if(messages == null){
  22. //如果session中没有messages属性,创建一个,并且把当前的messages赋值给他
  23. messages = new ArrayList<Message>();
  24. // context.setAttribute("messages", messages);
  25. session.setAttribute("messages", messages);
  26. }
  27. //messages队列中添加获取到的message
  28. messages.add(message);
  29. response.sendRedirect(request.getContextPath()+"/message.jsp?subFlag=1");
  30. System.out.println(title+content);
  31. %>
  32. </body>
  33. </html>

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <display-name>FilterDemo</display-name>
  4. <welcome-file-list>
  5. <welcome-file>index.jsp</welcome-file>
  6. <welcome-file>index.htm</welcome-file>
  7. <welcome-file>index.jsp</welcome-file>
  8. <welcome-file>default.html</welcome-file>
  9. <welcome-file>default.htm</welcome-file>
  10. <welcome-file>default.jsp</welcome-file>
  11. </welcome-file-list>
  12. <filter>
  13. <!-- 注册过滤器 -->
  14. <filter-name>characterEncodingFilter</filter-name>
  15. <filter-class>filter.CharacterEncodingFilter</filter-class>
  16. <init-param>
  17. <param-name>charset</param-name>
  18. <param-value>utf-8</param-value>
  19. </init-param>
  20. </filter>
  21. <filter>
  22. <!-- 注册过滤器 -->
  23. <filter-name>SessionFilter</filter-name>
  24. <filter-class>filter.SessionFilter</filter-class>
  25. </filter>
  26. <filter-mapping>
  27. <!-- 指定过滤器映射,对所有的请求都使用characterEncodingFilter的过滤器 -->
  28. <filter-name>characterEncodingFilter</filter-name>
  29. <url-pattern>/*</url-pattern>
  30. </filter-mapping>
  31. <filter-mapping>
  32. <!-- 用户登录安全控制过滤器配置,用户在留言时进行过滤 -->
  33. <filter-name>SessionFilter</filter-name>
  34. <url-pattern>/message.jsp</url-pattern>
  35. </filter-mapping>
  36. </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 ;
        }

    }

}

过滤器配置

web.xml文件中
的子元素dispatcher

REQUEST

默认是REQUEST

INCLUDE

通过include访问方式访问所对应的url-pattern,INCLUDE,例如,过滤器就会被执行

FORWARD

通过forward调用的FORWARD,例如request.getRequestDispatcher(‘/test.jsp’).forward(request,response)l; 过滤器就会被执行

ERROR

通过异常过来的,使用error过滤器
ERROR
同时,配置文件中可以写


404
/xx.jsp