Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge pull request #2 from hzuapps/master #352

Merged
merged 2 commits into from
Nov 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions jweb/src/edu/hzu/javaweb/labs/se1414080902226/EncodingFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package edu.hzu.javaweb.labs.se1414080902226;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

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.annotation.WebFilter;
@WebFilter(filterName="EncodingFilter", urlPatterns="/*")
public class EncodingFilter implements Filter {
private String encoding;
private Map<String, String> params = new HashMap<String, String>();
// ��Ŀ����ʱ���Ѿ���������
public void destroy() {
System.out.println("end do the encoding filter!");
params=null;
encoding=null;
}
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
//UtilTimerStack.push("EncodingFilter_doFilter:");
System.out.println(chain==null);
req.setCharacterEncoding(encoding);
chain.doFilter(req, resp);
}

// ��Ŀ����ʱ���Ѿ����ж�ȡ
public void init(FilterConfig config) throws ServletException {
System.out.println("begin do the encoding filter!");
encoding = config.getInitParameter("encoding");
for (Enumeration e = config.getInitParameterNames(); e
.hasMoreElements();) {
String name = (String) e.nextElement();
String value = config.getInitParameter(name);
params.put(name, value);
}
}
}
46 changes: 46 additions & 0 deletions jweb/src/edu/hzu/javaweb/labs/se1414080902226/LoggerFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package edu.hzu.javaweb.labs.se1414080902226;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@WebFilter(filterName="LoggerFilter", urlPatterns="/qiandaoxitong/servlet/qiandao.do")
public class LoggerFilter implements Filter{
FilterConfig config;

public void destroy() {
this.config = null;
}

@SuppressWarnings("deprecation")
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
ServletContext context = this.config.getServletContext();
HttpServletRequest hreq = (HttpServletRequest) req;
HttpSession session = hreq.getSession();
System.out.println(req.getParameter("count").toString());
if(session.getAttribute(req.getParameter("count").toString())!=null){
chain.doFilter(req, res);
}else{
req.getRequestDispatcher("/qiandao.jsp");
}
try {
// Filter ֻ����ʽ������������Ȼת����Ŀ�ĵ�ַ��
chain.doFilter(req, res);
} catch (Exception e) {
e.printStackTrace();
}
}

public void init(FilterConfig config) throws ServletException {
this.config = config;
}
}