• 20151203--filter


    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    
    <form id="uform" action="saveUser">      
    
    用户代码:<input id="userid" type="text" name="userid" width=30 />
    <br><br>
    用户名称:<input id="username" type="text" name="username" width=30 />
    <br><br>
    登录密码:<input id="password" type="password" name="password" width=30 />
    <br><br>
    确认密码:<input id="passwore" type="password" name="pueren" width=30 />
    <br><br>
    <input type="submit" value="提交" />
    </form>
    
    </body>
    </html>
    index
    package com.hanqi;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class saveUser
     */
    public class saveUser extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public saveUser() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            
            //获取
            String strUserName = request.getParameter("username");
            
            //转码
            //response.setCharacterEncoding("UTF-8");//输入的是UTF-8
            
            //response.setContentType("text/html;charset=UTF-8");//返回的是UTF-8
            
            //判断
            if(strUserName != null)
            {
                //初始化
                strUserName = new String(strUserName.getBytes("ISO-8859-1"),"UTF-8");
                
                response.getWriter().append("strUserName = " + strUserName);
            }
            response.getWriter().append("Served at: ").append(request.getContextPath());
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }
    
    }
    SaveUser
    package com.hanqi;
    
    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;
    
    /**
     * Servlet Filter implementation class MyFilter
     */
    public class MyFilter implements Filter {
    
        /**
         * Default constructor. 
         */
        public MyFilter() {
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see Filter#destroy()
         */
        public void destroy() {
            // TODO Auto-generated method stub
        }
    
        /**
         * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
         */
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            
            String userID = request.getParameter("userid");
            if(userID == null || userID.trim().length() == 0)
            {
                response.getWriter().append("userid  is  error");
            }
            else
            {
                //本句添加在过滤器最下方,用于传递给下一个过滤器或目标页面,构成过滤链条
                chain.doFilter(request, response);
            }
        }
    
        /**
         * @see Filter#init(FilterConfig)
         */
        public void init(FilterConfig fConfig) throws ServletException {
            // TODO Auto-generated method stub
        }
    
    }
    MyFilter
    package com.hanqi;
    
    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;
    
    /**
     * Servlet Filter implementation class Test2
     */
    public class Test2 implements Filter {
    
        /**
         * Default constructor. 
         */
        public Test2() {
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see Filter#destroy()
         */
        public void destroy() {
            // TODO Auto-generated method stub
        }
    
        /**
         * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
         */
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            
            
            
            response.setCharacterEncoding("UTF-8");//输入的是UTF-8
            
            response.setContentType("text/html;charset=UTF-8");//返回的是UTF-8
                    
            
            
            chain.doFilter(request, response);
        }
    
        /**
         * @see Filter#init(FilterConfig)
         */
        public void init(FilterConfig fConfig) throws ServletException {
            // TODO Auto-generated method stub
        }
    
    }
    Test2
    package com.hanqi;
    
    import javax.servlet.ServletContextAttributeEvent;
    import javax.servlet.ServletContextAttributeListener;
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    import javax.servlet.ServletRequestAttributeEvent;
    import javax.servlet.ServletRequestAttributeListener;
    import javax.servlet.ServletRequestEvent;
    import javax.servlet.ServletRequestListener;
    import javax.servlet.http.HttpSessionActivationListener;
    import javax.servlet.http.HttpSessionAttributeListener;
    import javax.servlet.http.HttpSessionBindingEvent;
    import javax.servlet.http.HttpSessionBindingListener;
    import javax.servlet.http.HttpSessionEvent;
    import javax.servlet.http.HttpSessionListener;
    
    /**
     * Application Lifecycle Listener implementation class TextListener
     *
     */
    public class TextListener implements ServletContextListener, ServletContextAttributeListener, HttpSessionListener, HttpSessionAttributeListener, HttpSessionActivationListener, HttpSessionBindingListener, ServletRequestListener, ServletRequestAttributeListener {
    
        /**
         * Default constructor. 
         */
        public TextListener() {
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see ServletRequestListener#requestDestroyed(ServletRequestEvent)
         */
        public void requestDestroyed(ServletRequestEvent arg0)  { 
             // TODO Auto-generated method stub
        }
    
        /**
         * @see HttpSessionAttributeListener#attributeAdded(HttpSessionBindingEvent)
         */
        public void attributeAdded(HttpSessionBindingEvent arg0)  { 
             // TODO Auto-generated method stub
        }
    
        /**
         * @see ServletContextListener#contextInitialized(ServletContextEvent)
         */
        public void contextInitialized(ServletContextEvent arg0)  { 
             // TODO Auto-generated method stub
        }
    
        /**
         * @see HttpSessionActivationListener#sessionDidActivate(HttpSessionEvent)
         */
        public void sessionDidActivate(HttpSessionEvent arg0)  { 
             // TODO Auto-generated method stub
        }
    
        /**
         * @see HttpSessionBindingListener#valueBound(HttpSessionBindingEvent)
         */
        public void valueBound(HttpSessionBindingEvent arg0)  { 
             // TODO Auto-generated method stub
        }
    
        /**
         * @see ServletContextAttributeListener#attributeAdded(ServletContextAttributeEvent)
         */
        public void attributeAdded(ServletContextAttributeEvent arg0)  { 
             // TODO Auto-generated method stub
        }
    
        /**
         * @see ServletContextAttributeListener#attributeRemoved(ServletContextAttributeEvent)
         */
        public void attributeRemoved(ServletContextAttributeEvent arg0)  { 
             // TODO Auto-generated method stub
        }
    
        /**
         * @see HttpSessionListener#sessionDestroyed(HttpSessionEvent)
         */
        public void sessionDestroyed(HttpSessionEvent arg0)  { 
             // TODO Auto-generated method stub
        }
    
        /**
         * @see HttpSessionAttributeListener#attributeRemoved(HttpSessionBindingEvent)
         */
        public void attributeRemoved(HttpSessionBindingEvent arg0)  { 
             // TODO Auto-generated method stub
        }
    
        /**
         * @see ServletRequestAttributeListener#attributeAdded(ServletRequestAttributeEvent)
         */
        public void attributeAdded(ServletRequestAttributeEvent arg0)  { 
             // TODO Auto-generated method stub
        }
    
        /**
         * @see HttpSessionBindingListener#valueUnbound(HttpSessionBindingEvent)
         */
        public void valueUnbound(HttpSessionBindingEvent arg0)  { 
             // TODO Auto-generated method stub
        }
    
        /**
         * @see HttpSessionActivationListener#sessionWillPassivate(HttpSessionEvent)
         */
        public void sessionWillPassivate(HttpSessionEvent arg0)  { 
             // TODO Auto-generated method stub
        }
    
        /**
         * @see HttpSessionListener#sessionCreated(HttpSessionEvent)
         */
        public void sessionCreated(HttpSessionEvent arg0)  { 
             // TODO Auto-generated method stub
        }
    
        /**
         * @see HttpSessionAttributeListener#attributeReplaced(HttpSessionBindingEvent)
         */
        public void attributeReplaced(HttpSessionBindingEvent arg0)  { 
             // TODO Auto-generated method stub
        }
    
        /**
         * @see ServletContextAttributeListener#attributeReplaced(ServletContextAttributeEvent)
         */
        public void attributeReplaced(ServletContextAttributeEvent arg0)  { 
             // TODO Auto-generated method stub
        }
    
        /**
         * @see ServletRequestAttributeListener#attributeRemoved(ServletRequestAttributeEvent)
         */
        public void attributeRemoved(ServletRequestAttributeEvent arg0)  { 
             // TODO Auto-generated method stub
        }
    
        /**
         * @see ServletContextListener#contextDestroyed(ServletContextEvent)
         */
        public void contextDestroyed(ServletContextEvent arg0)  { 
             // TODO Auto-generated method stub
        }
    
        /**
         * @see ServletRequestAttributeListener#attributeReplaced(ServletRequestAttributeEvent)
         */
        public void attributeReplaced(ServletRequestAttributeEvent arg0)  { 
             // TODO Auto-generated method stub
        }
    
        /**
         * @see ServletRequestListener#requestInitialized(ServletRequestEvent)
         */
        public void requestInitialized(ServletRequestEvent arg0)  { 
             // TODO Auto-generated method stub
        }
        
    }
    TextListener

  • 相关阅读:
    JavaScript-Runoob-JS函数:JavaScript 函数调用
    JavaScript-Runoob-JS函数:JavaScript 函数参数
    JavaScript-Runoob-JS函数:JavaScript 函数定义
    JavaScript-Runoob:JavaScript 代码规范
    Serverless架构的前世今生
    【华为云技术分享】STM32L476移植华为LiteOS系列教程(二)---开发前准备
    8分钟为你详解React、Angular、Vue三大前端技术
    【华为云技术分享】云容器引擎 CCE权限管理实践
    【极客思考】设计模式:你确定你真的理解了单例模式吗?
    最终,我决定将代码迁出x86架构!
  • 原文地址:https://www.cnblogs.com/name-hanlin/p/5017605.html
Copyright © 2020-2023  润新知