« | September 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | | | | | |
| 公告 |
|
Blog信息 |
blog名称: 日志总数:22 评论数量:55 留言数量:0 访问次数:129547 建立时间:2006年3月13日 |

| |
[java程序设计]filter过滤 软件技术
jjs_love 发表于 2007/3/31 11:12:22 |
web.xml <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>ShowImgServlet</servlet-name> <servlet-class>rcom.rf.common.ShowImgServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ShowImgServlet</servlet-name> <url-pattern>/servlet/ShowImgServlet</url-pattern> </servlet-mapping> <filter> <filter-name>checksessionfilter</filter-name> <filter-class>rcom.rf.common.CheckSessionFilter</filter-class> </filter> <filter-mapping> <filter-name>checksessionfilter</filter-name> <url-pattern>/admin/*</url-pattern> </filter-mapping>CheckSessionFilter.java package rcom.rf.common;
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.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;
public class CheckSessionFilter extends HttpServlet implements Filter {
private FilterConfig filterConfig; //Handle the passed-in FilterConfig public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; }
//校验session public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) { HttpSession session = ((HttpServletRequest)request).getSession(); try { if(session.getAttribute("username")==null){ //System.out.println("session is null!没有登陆···");///////////////// ((HttpServletResponse)response).sendRedirect("/rfnet/index.jsp"); } filterChain.doFilter(request, response); } catch (IOException e) { System.out.println("CheckSessionFilter ERR IOException: "+e.getMessage());/////////// filterConfig.getServletContext().log(e.getMessage()); } catch (ServletException e) { System.out.println("CheckSessionFilter ERR ServletException: "+e.getMessage());/////////////////////////////// filterConfig.getServletContext().log(e.getMessage()); } }
//Clean up resources public void destroy() { }
}ShowImgServlet.java package rcom.rf.common;
import java.io.BufferedOutputStream;import java.io.IOException;import java.io.InputStream;import java.sql.Blob;import java.sql.SQLException;
import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;
import rcom.rf.dao.ImgNewsDAO;
public class ShowImgServlet extends HttpServlet { private static final String CONTENT_TYPE = "text/html; charset=GBK"; /** * Constructor of the object. */ public ShowImgServlet() { super(); }
/** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here }
/** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ //显示图片 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(CONTENT_TYPE); InputStream in = null; String id = request.getParameter("id"); byte[] buff = new byte[10240]; Blob blob = null; ImgNewsDAO imgDAO = null; int length = 0 ; try { BufferedOutputStream buffout = new BufferedOutputStream(response.getOutputStream()); if(id != null && !id.equals("")){ imgDAO = new ImgNewsDAO(); blob = imgDAO.getImg(new Long(id)); in = blob.getBinaryStream(); } while((length = in.read(buff))!=-1){ buffout.write(buff,0,length); } in.close(); buffout.close(); } catch (NumberFormatException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } }
/** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }
/** * Initialization of the servlet. <br> * * @throws ServletException if an error occure */ public void init() throws ServletException { // Put your code here }
}http://www.javaworld.com.tw/confluence/pages/viewpage.action?pageId=2727 |
|
回复:filter过滤 软件技术
yjepia发表评论于2007/4/4 13:09:18 |
如果您把过滤器的基本知识也带一下,再来点注释,这个Blog就完美啦 |
|
» 1 »
|