servlet3.0的学习(一)

注解的使用
开发工具:NetBeans6.8
目前j2ee6的开发工具只能是NetBeans,应用服务器为glassfish和jboss6.0
在介绍之前先说一下NetBeans常用的快捷键!
ctrl+/ : 注释掉 (选中多行,可以多行注释)
ctrl+\ : 代码提示
ctrl+e : 删除行
当然也可以通过工具-选项-快捷键映射来修改!

前言:servlet3.0加强了注解支持,注解的引入,使部署描述符web.xml文件成为可选的。在这完全不用配置web.xml!

TestServlet.java
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name="mytest",
        urlPatterns={"/test"},
        initParams={ @WebInitParam(name="message", value="my servlet"),@WebInitParam(name="message2", value="my servlet2")}
)
public class TestServlet extends HttpServlet {
    private String listenerMessage = null;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        listenerMessage = (String)config.getServletContext().getAttribute("listenerMessage");
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse res)
            throws IOException, ServletException {

        PrintWriter writer = res.getWriter();
        writer.write("Hello, " + getInitParameter("message") + ", ");
        writer.write( getInitParameter("message2") + ", ");
        writer.write(req.getAttribute("filterMessage") + ", ");
        writer.write(listenerMessage + ".\n");
    }
}


说明:
WebServlet:指定servlet,必须要继承HttpServlet
urlPatterns:指定servlet的路径
initParams:指定初始化参数,可以多个
WebInitParam:初始化参数,一个参数一个WebInitParam

TestFilter.java
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.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;


@WebFilter(urlPatterns={"/*"}, initParams={ @WebInitParam(name="mesg", value="my filter") })
public class TestFilter implements Filter {
    String mesg = null;

    public void init(FilterConfig filterConfig) throws ServletException {
        mesg = filterConfig.getInitParameter("mesg");
    }   

    public void doFilter(ServletRequest req, ServletResponse res,
            FilterChain chain) throws IOException, ServletException {

        req.setAttribute("filterMessage", mesg);
        chain.doFilter(req, res);
    }

    public void destroy() {
    }
}

说明:
WebFilter:指定拦截器,必须实现Filter
urlPatterns:指定拦截器的范围

TestServletContextListener.java
import javax.servlet.ServletContext;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletContextEvent;


@javax.servlet.annotation.WebListener
public class TestServletContextListener implements ServletContextListener {
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext context = sce.getServletContext();
        context.setAttribute("listenerMessage", "my listener");
    }   

    public void contextDestroyed(ServletContextEvent sce) {
    }
}

说明:
javax.servlet.annotation.WebListener:指定监听器,必须实现ServletContextListener

你可能感兴趣的:(java,应用服务器,servlet,Netbeans,Glassfish)