开发者

Java EE Filter and Listener

If I implement a Filter that before the request is processed it prints to the browser the date/time like this:

@WebServlet(name = "TimedServlet",
urlPatterns =
{
    "/timed_servlet"
}, initParams =
{
    @WebInitParam(name = "prod", value = "true")
})
class TimedServlet extends HttpServlet
{
    @WebFilter(filterName = "Time_F",
    urlPatterns =
    {
        "/timed_servlet"
    })
    private class Time_F implements Filter
    {
        private FilterConfig fc;

        public void init(FilterConfig filterConfig) throws ServletException
        {
            fc = filterConfig;
        }

        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
        {
            Calendar cal = Calendar.getInstance();
            SimpleDateFormat sd_f = new SimpleDateFormat();

            response.getWriter().println(sd_f.format(cal));

            chain.doFilter(request, response);
        }

        public void destroy()
        {
            fc = null;
        }
    }

    protected void doRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        response.setContentType("text/html;charset=UTF-8");

        try (PrintWriter out = response.getWriter())
        {

            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ---</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("PROD");
            out.println("</body>");
            out.println("</html>");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        doRequest(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
           开发者_JS百科 throws ServletException, IOException
    {
        doRequest(request, response);
    }

    public String getServletInfo()
    {
        return "...";
    }
}

nothing is showed in the browser but only the servlet output!

And If I want to process a ServletRequestListener how can I access a response object so I can show some info to the user?

Thanks.


You need to invoke FilterChain#doFilter() to let the request go to the servlet/JSP. Otherwise you're plain blocking the initial request.

chain.doFilter(request, response);

As to your second question, no there the ServletRequestListener is not designed for.


Unrelated to the concrete problem, please keep in mind that a Filter is not necessarily the right place to write something to the response. This way the Servlet/JSP has no chance anymore to control/change the response as per business requirements and/or in case of exceptions. You'll risk ending up with IllegalStateExceptions in the server logs. If you want to print the date in all pages, you have to do it in the JSP side. To achieve this, store the date as a request attribute instead:

String timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
request.setAttribute("timestamp", timestamp);
chain.doFilter(request, response);

and then display it in JSP as follows

${timestamp}

(you can also just store the new Date() and use JSTL <fmt:formatDate> to format the date)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜