java类javax.servlet.http.HttpServlet的实例源码

TestExpiresFilter.java 文件源码 项目:tomcat7 阅读 43 收藏 0 点赞 0 评论 0
@Test
public void testExcludedResponseStatusCode() throws Exception {
    HttpServlet servlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override
        protected void service(HttpServletRequest request,
                HttpServletResponse response) throws ServletException,
                IOException {
            response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
            response.addHeader("ETag", "W/\"1934-1269208821000\"");
            response.addDateHeader("Date", System.currentTimeMillis());
        }
    };

    validate(servlet, null, HttpServletResponse.SC_NOT_MODIFIED);
}
TestExpiresFilter.java 文件源码 项目:tomcat7 阅读 37 收藏 0 点赞 0 评论 0
@Test
public void testSkipBecauseExpiresIsDefined() throws Exception {
    HttpServlet servlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override
        protected void service(HttpServletRequest request,
                HttpServletResponse response) throws ServletException,
                IOException {
            response.setContentType("text/xml; charset=utf-8");
            response.addDateHeader("Expires", System.currentTimeMillis());
            response.getWriter().print("Hello world");
        }
    };

    validate(servlet, null);
}
TestExpiresFilter.java 文件源码 项目:apache-tomcat-7.0.73-with-comment 阅读 34 收藏 0 点赞 0 评论 0
@Test
public void testExcludedResponseStatusCode() throws Exception {
    HttpServlet servlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override
        protected void service(HttpServletRequest request,
                HttpServletResponse response) throws ServletException,
                IOException {
            response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
            response.addHeader("ETag", "W/\"1934-1269208821000\"");
            response.addDateHeader("Date", System.currentTimeMillis());
        }
    };

    validate(servlet, null, HttpServletResponse.SC_NOT_MODIFIED);
}
TestExpiresFilter.java 文件源码 项目:apache-tomcat-7.0.73-with-comment 阅读 39 收藏 0 点赞 0 评论 0
@Test
public void testSkipBecauseExpiresIsDefined() throws Exception {
    HttpServlet servlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override
        protected void service(HttpServletRequest request,
                HttpServletResponse response) throws ServletException,
                IOException {
            response.setContentType("text/xml; charset=utf-8");
            response.addDateHeader("Expires", System.currentTimeMillis());
            response.getWriter().print("Hello world");
        }
    };

    validate(servlet, null);
}
Expand.java 文件源码 项目:epay 阅读 33 收藏 0 点赞 0 评论 0
@Override
public void init(HttpServlet servlet) throws Exception {
    TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
    ServletContext servletContext = servlet.getServletContext();
    String configFileName = servletContext.getInitParameter("configFileName");
    Properties properties = loadConfig(configFileName);
    MybatisManager.init(properties.getProperty("config_dir"), "mybatis-config.xml", new MariadbLog());
    HttpUtil.init("UTF-8", new HttpclientLog());
    ThreadmsgLog threadmsgLog = new ThreadmsgLog();
    AsyncThreadManager.init(100, 10, 3, 0, threadmsgLog);
    AsyncThreadManager.start();
    MsgManager.init(true, threadmsgLog);
    CommonConfig.init(properties);
    HOpCode.init();

    MsgOpCode.init();
    HttpManager.addFilter(new TokenHttpFilter());
    HttpManager.addHttpListener(new AppService());
    HttpManager.addHttpListener(new OrderRecordService());
    HttpManager.addHttpListener(new PayService());
    HttpManager.addHttpListener(new LoginService());
    MsgManager.addMsgListener(new NotifyService(10));
}
HttpServer2.java 文件源码 项目:hadoop-oss 阅读 38 收藏 0 点赞 0 评论 0
/**
 * Add an internal servlet in the server, specifying whether or not to
 * protect with Kerberos authentication.
 * Note: This method is to be used for adding servlets that facilitate
 * internal communication and not for user facing functionality. For
 +   * servlets added using this method, filters (except internal Kerberos
 * filters) are not enabled.
 *
 * @param name The name of the servlet (can be passed as null)
 * @param pathSpec The path spec for the servlet
 * @param clazz The servlet class
 * @param requireAuth Require Kerberos authenticate to access servlet
 */
public void addInternalServlet(String name, String pathSpec,
    Class<? extends HttpServlet> clazz, boolean requireAuth) {
  ServletHolder holder = new ServletHolder(clazz);
  if (name != null) {
    holder.setName(name);
  }
  webAppContext.addServlet(holder, pathSpec);

  if(requireAuth && UserGroupInformation.isSecurityEnabled()) {
     LOG.info("Adding Kerberos (SPNEGO) filter to " + name);
     ServletHandler handler = webAppContext.getServletHandler();
     FilterMapping fmap = new FilterMapping();
     fmap.setPathSpec(pathSpec);
     fmap.setFilterName(SPNEGO_FILTER);
     fmap.setDispatches(Handler.ALL);
     handler.addFilterMapping(fmap);
  }
}
Expand.java 文件源码 项目:startpoint 阅读 30 收藏 0 点赞 0 评论 0
@Override
public void init(HttpServlet servlet) throws Exception {
    TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
    ServletContext servletContext = servlet.getServletContext();
    String configFileName = servletContext.getInitParameter("configFileName");
    Properties properties = loadConfig(configFileName);
    MybatisManager.init(properties.getProperty("config_dir"), "mybatis-config.xml", new MariadbLog());
    KeyLockManager.init(new UCenterKeyLockType().getkeyLockType(), 120000, 100, new KeylockLog());
    HOpCodeUCenter.init();
    CommonConfigUCenter.init();
    CommonConfigUCenter.UCENTER_URL = properties.getProperty("uCenterUrl");
    HttpManager.addFilter(new TokenHttpFilter());
    HttpManager.addHttpListener(new UserService());
    HttpManager.addHttpListener(new UserGroupService());
    HttpManager.addHttpListener(new TokenService());
}
TestExpiresFilter.java 文件源码 项目:tomcat7 阅读 32 收藏 0 点赞 0 评论 0
/**
 * Test that a resource with empty content is also processed
 */
@Test
public void testEmptyContent() throws Exception {
    HttpServlet servlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override
        protected void service(HttpServletRequest request,
                HttpServletResponse response) throws ServletException,
                IOException {
            response.setContentType("text/plain");
            // no content is written in the response
        }
    };

    validate(servlet, Integer.valueOf(7 * 60));
}
TestExpiresFilter.java 文件源码 项目:tomcat7 阅读 40 收藏 0 点赞 0 评论 0
@Test
public void testSkipBecauseCacheControlMaxAgeIsDefined() throws Exception {
    HttpServlet servlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override
        protected void service(HttpServletRequest request,
                HttpServletResponse response) throws ServletException,
                IOException {
            response.setContentType("text/xml; charset=utf-8");
            response.addHeader("Cache-Control", "private, max-age=232");
            response.getWriter().print("Hello world");
        }
    };

    validate(servlet, Integer.valueOf(232));
}
TestExpiresFilter.java 文件源码 项目:tomcat7 阅读 40 收藏 0 点赞 0 评论 0
@Test
public void testUseContentTypeExpiresConfiguration() throws Exception {
    HttpServlet servlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override
        protected void service(HttpServletRequest request,
                HttpServletResponse response) throws ServletException,
                IOException {
            response.setContentType("text/xml; charset=utf-8");
            response.getWriter().print("Hello world");
        }
    };

    validate(servlet, Integer.valueOf(3 * 60));
}
TestExpiresFilter.java 文件源码 项目:tomcat7 阅读 35 收藏 0 点赞 0 评论 0
@Test
public void testUseContentTypeWithoutCharsetExpiresConfiguration()
        throws Exception {
    HttpServlet servlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override
        protected void service(HttpServletRequest request,
                HttpServletResponse response) throws ServletException,
                IOException {
            response.setContentType("text/xml; charset=iso-8859-1");
            response.getWriter().print("Hello world");
        }
    };

    validate(servlet, Integer.valueOf(5 * 60));
}
TestExpiresFilter.java 文件源码 项目:tomcat7 阅读 36 收藏 0 点赞 0 评论 0
@Test
public void testUseDefaultConfiguration1() throws Exception {
    HttpServlet servlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override
        protected void service(HttpServletRequest request,
                HttpServletResponse response) throws ServletException,
                IOException {
            response.setContentType("image/jpeg");
            response.getWriter().print("Hello world");
        }
    };

    validate(servlet, Integer.valueOf(1 * 60));
}
TestExpiresFilter.java 文件源码 项目:tomcat7 阅读 39 收藏 0 点赞 0 评论 0
@Test
public void testUseDefaultConfiguration2() throws Exception {
    HttpServlet servlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override
        protected void service(HttpServletRequest request,
                HttpServletResponse response) throws ServletException,
                IOException {
            response.setContentType("image/jpeg");
            response.addHeader("Cache-Control", "private");

            response.getWriter().print("Hello world");
        }
    };

    validate(servlet, Integer.valueOf(1 * 60));
}
TestExpiresFilter.java 文件源码 项目:tomcat7 阅读 31 收藏 0 点赞 0 评论 0
@Test
public void testUseMajorTypeExpiresConfiguration() throws Exception {
    HttpServlet servlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override
        protected void service(HttpServletRequest request,
                HttpServletResponse response) throws ServletException,
                IOException {
            response.setContentType("text/json; charset=iso-8859-1");
            response.getWriter().print("Hello world");
        }
    };

    validate(servlet, Integer.valueOf(7 * 60));
}
TestMimeHeaders.java 文件源码 项目:tomcat7 阅读 31 收藏 0 点赞 0 评论 0
private void setupHeadersTest(Tomcat tomcat) {
    Context ctx = tomcat.addContext("", getTemporaryDirectory()
            .getAbsolutePath());
    Tomcat.addServlet(ctx, "servlet", new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override
        public void service(ServletRequest req, ServletResponse res)
                throws ServletException, IOException {
            res.setContentType("text/plain; charset=ISO-8859-1");
            res.getWriter().write("OK");
        }
    });
    ctx.addServletMapping("/", "servlet");

    alv = new HeaderCountLogValve();
    tomcat.getHost().getPipeline().addValve(alv);
}
WebXMLTest.java 文件源码 项目:parabuild-ci 阅读 31 收藏 0 点赞 0 评论 0
public void testContextParameters() throws Exception {
    WebXMLString wxs = new WebXMLString();
    wxs.addServlet( "/SimpleServlet", SimpleGetServlet.class );
    wxs.addContextParam( "icecream", "vanilla" );
    wxs.addContextParam( "cone", "waffle" );
    wxs.addContextParam( "topping", "" );

    ServletRunner sr = new ServletRunner( toInputStream( wxs.asText() ) );
    ServletUnitClient client = sr.newClient();
    assertEquals( "Context parameter 'icecream'", "vanilla", sr.getContextParameter( "icecream" ) );
    InvocationContext ic = client.newInvocation( "http://localhost/SimpleServlet" );

    javax.servlet.ServletContext sc = ((HttpServlet) ic.getServlet()).getServletContext();
    assertNotNull( "ServletContext should not be null", sc );
    assertEquals( "ServletContext.getInitParameter()", "vanilla", sc.getInitParameter( "icecream" ) );
    assertEquals( "init parameter: cone", "waffle", sc.getInitParameter( "cone" ) );
    assertEquals( "init parameter: topping", "", sc.getInitParameter( "topping" ) );
    assertNull( "ServletContext.getInitParameter() should be null", sc.getInitParameter( "shoesize" ) );

}
TestExpiresFilter.java 文件源码 项目:apache-tomcat-7.0.73-with-comment 阅读 29 收藏 0 点赞 0 评论 0
/**
 * Test that a resource with empty content is also processed
 */
@Test
public void testEmptyContent() throws Exception {
    HttpServlet servlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override
        protected void service(HttpServletRequest request,
                HttpServletResponse response) throws ServletException,
                IOException {
            response.setContentType("text/plain");
            // no content is written in the response
        }
    };

    validate(servlet, Integer.valueOf(7 * 60));
}
TestExpiresFilter.java 文件源码 项目:apache-tomcat-7.0.73-with-comment 阅读 31 收藏 0 点赞 0 评论 0
@Test
public void testSkipBecauseCacheControlMaxAgeIsDefined() throws Exception {
    HttpServlet servlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override
        protected void service(HttpServletRequest request,
                HttpServletResponse response) throws ServletException,
                IOException {
            response.setContentType("text/xml; charset=utf-8");
            response.addHeader("Cache-Control", "private, max-age=232");
            response.getWriter().print("Hello world");
        }
    };

    validate(servlet, Integer.valueOf(232));
}
TestExpiresFilter.java 文件源码 项目:apache-tomcat-7.0.73-with-comment 阅读 37 收藏 0 点赞 0 评论 0
@Test
public void testUseContentTypeExpiresConfiguration() throws Exception {
    HttpServlet servlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override
        protected void service(HttpServletRequest request,
                HttpServletResponse response) throws ServletException,
                IOException {
            response.setContentType("text/xml; charset=utf-8");
            response.getWriter().print("Hello world");
        }
    };

    validate(servlet, Integer.valueOf(3 * 60));
}
TestExpiresFilter.java 文件源码 项目:apache-tomcat-7.0.73-with-comment 阅读 31 收藏 0 点赞 0 评论 0
@Test
public void testUseContentTypeWithoutCharsetExpiresConfiguration()
        throws Exception {
    HttpServlet servlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override
        protected void service(HttpServletRequest request,
                HttpServletResponse response) throws ServletException,
                IOException {
            response.setContentType("text/xml; charset=iso-8859-1");
            response.getWriter().print("Hello world");
        }
    };

    validate(servlet, Integer.valueOf(5 * 60));
}
TestExpiresFilter.java 文件源码 项目:apache-tomcat-7.0.73-with-comment 阅读 33 收藏 0 点赞 0 评论 0
@Test
public void testUseDefaultConfiguration1() throws Exception {
    HttpServlet servlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override
        protected void service(HttpServletRequest request,
                HttpServletResponse response) throws ServletException,
                IOException {
            response.setContentType("image/jpeg");
            response.getWriter().print("Hello world");
        }
    };

    validate(servlet, Integer.valueOf(1 * 60));
}
TestExpiresFilter.java 文件源码 项目:apache-tomcat-7.0.73-with-comment 阅读 36 收藏 0 点赞 0 评论 0
@Test
public void testUseDefaultConfiguration2() throws Exception {
    HttpServlet servlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override
        protected void service(HttpServletRequest request,
                HttpServletResponse response) throws ServletException,
                IOException {
            response.setContentType("image/jpeg");
            response.addHeader("Cache-Control", "private");

            response.getWriter().print("Hello world");
        }
    };

    validate(servlet, Integer.valueOf(1 * 60));
}
TestExpiresFilter.java 文件源码 项目:apache-tomcat-7.0.73-with-comment 阅读 40 收藏 0 点赞 0 评论 0
@Test
public void testUseMajorTypeExpiresConfiguration() throws Exception {
    HttpServlet servlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override
        protected void service(HttpServletRequest request,
                HttpServletResponse response) throws ServletException,
                IOException {
            response.setContentType("text/json; charset=iso-8859-1");
            response.getWriter().print("Hello world");
        }
    };

    validate(servlet, Integer.valueOf(7 * 60));
}
TestMimeHeaders.java 文件源码 项目:apache-tomcat-7.0.73-with-comment 阅读 42 收藏 0 点赞 0 评论 0
private void setupHeadersTest(Tomcat tomcat) {
    Context ctx = tomcat.addContext("", getTemporaryDirectory()
            .getAbsolutePath());
    Tomcat.addServlet(ctx, "servlet", new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override
        public void service(ServletRequest req, ServletResponse res)
                throws ServletException, IOException {
            res.setContentType("text/plain; charset=ISO-8859-1");
            res.getWriter().write("OK");
        }
    });
    ctx.addServletMapping("/", "servlet");

    alv = new HeaderCountLogValve();
    tomcat.getHost().getPipeline().addValve(alv);
}
HttpServer.java 文件源码 项目:hadoop 阅读 53 收藏 0 点赞 0 评论 0
/**
 * Add an internal servlet in the server, specifying whether or not to
 * protect with Kerberos authentication. 
 * Note: This method is to be used for adding servlets that facilitate
 * internal communication and not for user facing functionality. For
 +   * servlets added using this method, filters (except internal Kerberos
 * filters) are not enabled. 
 * 
 * @param name The name of the servlet (can be passed as null)
 * @param pathSpec The path spec for the servlet
 * @param clazz The servlet class
 * @param requireAuth Require Kerberos authenticate to access servlet
 */
public void addInternalServlet(String name, String pathSpec, 
    Class<? extends HttpServlet> clazz, boolean requireAuth) {
  ServletHolder holder = new ServletHolder(clazz);
  if (name != null) {
    holder.setName(name);
  }
  webAppContext.addServlet(holder, pathSpec);

  if(requireAuth && UserGroupInformation.isSecurityEnabled()) {
     LOG.info("Adding Kerberos (SPNEGO) filter to " + name);
     ServletHandler handler = webAppContext.getServletHandler();
     FilterMapping fmap = new FilterMapping();
     fmap.setPathSpec(pathSpec);
     fmap.setFilterName(SPNEGO_FILTER);
     fmap.setDispatches(Handler.ALL);
     handler.addFilterMapping(fmap);
  }
}
HttpServer2.java 文件源码 项目:hadoop 阅读 40 收藏 0 点赞 0 评论 0
/**
 * Add an internal servlet in the server, specifying whether or not to
 * protect with Kerberos authentication.
 * Note: This method is to be used for adding servlets that facilitate
 * internal communication and not for user facing functionality. For
 +   * servlets added using this method, filters (except internal Kerberos
 * filters) are not enabled.
 *
 * @param name The name of the servlet (can be passed as null)
 * @param pathSpec The path spec for the servlet
 * @param clazz The servlet class
 * @param requireAuth Require Kerberos authenticate to access servlet
 */
public void addInternalServlet(String name, String pathSpec,
    Class<? extends HttpServlet> clazz, boolean requireAuth) {
  ServletHolder holder = new ServletHolder(clazz);
  if (name != null) {
    holder.setName(name);
  }
  webAppContext.addServlet(holder, pathSpec);

  if(requireAuth && UserGroupInformation.isSecurityEnabled()) {
     LOG.info("Adding Kerberos (SPNEGO) filter to " + name);
     ServletHandler handler = webAppContext.getServletHandler();
     FilterMapping fmap = new FilterMapping();
     fmap.setPathSpec(pathSpec);
     fmap.setFilterName(SPNEGO_FILTER);
     fmap.setDispatches(Handler.ALL);
     handler.addFilterMapping(fmap);
  }
}
SimpleSessionFilterTest.java 文件源码 项目:simple-session 阅读 30 收藏 0 点赞 0 评论 0
@Test
public void test_filter_wrapper() throws IOException, ServletException {
    HttpServlet servlet = new HttpServlet() {
        @Override
        public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
            assertThat(req).isInstanceOf(SimpleSessionRequest.class);
            super.service(req, res);
        }
    };
    mockFilterChain = new MockFilterChain(servlet, filter);

    filter.doFilter(mockRequest, mockResponse, mockFilterChain);

    assertThat(mockRequest.getAttribute(SimpleSessionFilter.class.getName().concat(".VISITED")))
            .isEqualTo(Boolean.TRUE);
}
TestRs.java 文件源码 项目:reactive-components 阅读 45 收藏 0 点赞 0 评论 0
private Server createServer() {
    Server server = new Server();
    SelectChannelConnector connector = new SelectChannelConnector();
    connector.setPort(8384);
    server.addConnector(connector);

    ServletHandler handler = new ServletHandler();
    Servlet servlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
            String path = req.getServletPath().substring(1);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
            resp.getWriter().append(path);
        }

    };
    handler.addServletWithMapping(new ServletHolder(servlet), "/");
    server.setHandler(handler);
    return server;
}
BaseServletAction.java 文件源码 项目:PCloud_Server_v3 阅读 29 收藏 0 点赞 0 评论 0
/**
 * 请求委托
 * @param request
 * @param response
 * @param httpServlet
 */
public void entrust(HttpServletRequest request, HttpServletResponse response, HttpServlet httpServlet){
    method = request.getMethod();
    this.request = request;
    response.setCharacterEncoding(encode);

    //设置POST请求的字符编码
    if(method.equals("POST")){
        try {
            request.setCharacterEncoding(encode);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    //设置Content-Type
    response.setHeader("Content-Type", "text/html;charset=" + encode);

    execute(request, response, httpServlet);
}
HttpServer.java 文件源码 项目:ditb 阅读 39 收藏 0 点赞 0 评论 0
/**
 * Add an internal servlet in the server, specifying whether or not to
 * protect with Kerberos authentication.
 * Note: This method is to be used for adding servlets that facilitate
 * internal communication and not for user facing functionality. For
 +   * servlets added using this method, filters (except internal Kerberos
 * filters) are not enabled.
 *
 * @param name The name of the servlet (can be passed as null)
 * @param pathSpec The path spec for the servlet
 * @param clazz The servlet class
 * @param requireAuth Require Kerberos authenticate to access servlet
 */
public void addInternalServlet(String name, String pathSpec,
    Class<? extends HttpServlet> clazz, boolean requireAuth) {
  ServletHolder holder = new ServletHolder(clazz);
  if (name != null) {
    holder.setName(name);
  }
  webAppContext.addServlet(holder, pathSpec);

  if(requireAuth && UserGroupInformation.isSecurityEnabled()) {
     LOG.info("Adding Kerberos (SPNEGO) filter to " + name);
     ServletHandler handler = webAppContext.getServletHandler();
     FilterMapping fmap = new FilterMapping();
     fmap.setPathSpec(pathSpec);
     fmap.setFilterName(SPNEGO_FILTER);
     fmap.setDispatches(Handler.ALL);
     handler.addFilterMapping(fmap);
  }
}


问题


面经


文章

微信
公众号

扫码关注公众号