Tomcat配置和servlet创建(eclipse)、重定向和转发

一、Tomcat下载和安装

1、首先去官网下载Tomcat的zip包;
链接:http://tomcat.apache.org/

2、通过server,添加对应版本的Tomcat,选则custom(找到下载的包的路径)

二、Servlet访问路径配置

servlet:

  1. 路径问题:

    a.通过插件@WebServlet("/servlet")

    b.通过web.xml里面配置

    如果两个都配置了,两个路径都可以访问




  
    wtx
    com.wtx.servlet.FirstSevlet
    
    4
  
  
  
    wtx
    /first
  

servlet访问静态资源

web.xml里面找url-patten没有缺省的-->全局的web.xml里面找缺省的("/")-->default-->去工程根目录找-->index.html

插件设置Servlet访问url

/**
 * Servlet implementation class WtxServlet
 */
方法一: @WebServlet("/wtxServlet")
方法二:@WebServlet(name = "www", urlPatterns = {"/OneServlet", "/One"},loadOnStartup = -1) //loadOnStartup != -1加上服务器启动就会init


public class WtxServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see Servlet#init(ServletConfig)
     */
    public void init(ServletConfig config) throws ServletException {
        // TODO Auto-generated method stub
    }

    /**
     * @see Servlet#destroy()
     */
    public void destroy() {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
     */
    //当每次请求的时候就会调用此方法
    //一但你写了service就不会再去找doget 或dopost
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        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);
    }

}

三、设置响应头和重定向(发送两次请求,地址栏会变)

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        //设置响应行 设置状态码
        response.setStatus(200);
        
        //添加响应头
        
        response.addHeader("name", "test");
        
        response.addIntHeader("age", 10);
        response.addDateHeader("my-Date", new Date().getTime());
        
        response.addHeader("name", "charles");
        
        
        //修改响应头
        response.setHeader("name", "charles_w");
        response.setIntHeader("age", 100);
        response.setDateHeader("my-Date", new Date().getTime());
        
        //设置重定向
        //访问两次-第一次是ResponseServlet,第二次是LocationServlet
        response.setStatus(302);
        //response.setHeader("location", "/26-Servlet/LocationServlet");
        //不用写location
//      response.sendRedirect("/26-Servlet/LocationServlet");
        //定时刷新重定向  隔3秒钟才去转发
        //response.setHeader("refresh","3;url=http://www.baidu.com");
        
        
        //设置缓存区当中使用的编码 UTF-8
        //response.setCharacterEncoding("UTF-8");
        
        //在响应当中主动告诉浏览使用什么样的编码来接收
        //response.setHeader("Content-Type", "text/html;charset=UTF-8");
        //可以使用简写 放在设置响应体之前来设置编码
        response.setContentType("text/html;charset=UTF-8");
        
        //设置响应体 
        //写字符
        response.getWriter().write("响应数据");
        //如果字符当中有html浏览器会帮你解析
        response.getWriter().write("

666

"); //写中文 response.getWriter().write("我是666"); }

四、ServletContext 全局的对象

//设置内容
this.getServletContext().setAttribute("checkCode", "设置的内容");

//获取设置的内容
this.getServletContext().getAttribute("checkCode");

五、请求转发(发送一次请求,地址栏不会变)

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println(request);
    // 请求转发
    //1.获取转发器
    //Servlet2 是另一个Servlet
    RequestDispatcher disp = request.getRequestDispatcher("/Servlet2");
    //域  能够存储数据 
    request.setAttribute("key", "value666");
    //2.转发
    disp.forward(request, response);
        
    }

你可能感兴趣的:(Tomcat配置和servlet创建(eclipse)、重定向和转发)