JavaEE新特性

2017/04/03 JavaEE

JavaEE 6.0新特性

tomcat7 以上版本支持

WebServlet注解

@WebServlet(urlPatterns = "/myServlet", loadOnStartup = 1, initParams = {@WebInitParam(name = "key", value = "val")})
public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }
}

Filter注解

// DispatcherType.ASYNC,拦截异步Servlet使用
@WebFilter(urlPatterns = "/*",dispatcherTypes = {DispatcherType.REQUEST,DispatcherType.ASYNC},asyncSupported = true)
public class MyFilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {}
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    	chain.doFilter(request, response);
    }
    public void destroy() {}
}

Listener注解

@WebListener
public class MyListener implements ServletRequestListener {
    public void requestDestroyed(ServletRequestEvent sre) {}
    public void requestInitialized(ServletRequestEvent sre) {}
}

异步处理

// asyncSupported = true表明支持异步处理,所经过的filter也需要声明asyncSupported = true
@WebServlet(urlPatterns = "/myServlet", loadOnStartup = 1, initParams = {@WebInitParam(name = "key", value = "val")},asyncSupported = true)
public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        final AsyncContext asyncContext = req.startAsync(req,resp);
        // IE有缓存,超过缓存大小才会输出到浏览器界面
        // for (int i = 0; i < 512; i++) {
        //     try {
        //         resp.getWriter().write(" ");
        //     } catch (Exception e) {
        //         e.printStackTrace();
        //     }
        // }
        asyncContext.start(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    try {
                        Thread.sleep(500);
                        resp.getWriter().write(i+" ");
                        resp.getWriter().flush();// flush才会输出到客户端,否则超出缓存大小后输出
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                asyncContext.complete();// 异步处理结束
            }
        });
    }
}

文件上传

<form action="<c:url value="/fileUpload3"/>" method="post" enctype="multipart/form-data">
    <input type="text" name="username"/></br>
    <input type="file" name="file"/></br>
    <input type="submit"/>
</form>
@WebServlet(urlPatterns = "/fileUpload3")
@MultipartConfig //开启文件上传支持
public class FileUploadServlet3 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        System.out.println(username);
        Part part = req.getPart("file");
        System.out.println(part.getContentType());
        System.out.println(part.getName());
        System.out.println(part.getSize());
        // part.getSubmittedFileName(); // 3.1.0版本中提供
        part.write("/upload/" + part.getSubmittedFileName());
    }
}


以上概念总结于传智播客JavaWeb课程

Search

    Post Directory