servlet的注解配置

构建过程

项目结构如图


image.png

pom.xml依赖,导入servlet包



    4.0.0

    com.haijunyin
    servletdemo-xmlconfig
    1.0-SNAPSHOT

    war

    
        
            javax.servlet
            javax.servlet-api
            3.1.0
        
    


HelloServlet.java继承HttpServlet,添加注解@WebServlet及url替代xml文件中的映射

package com.haijunyin.servletdemo.xmlconfig.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/hello")
public class HelloServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("HelloServlet...doGet...");
        resp.getWriter().write("hello...");
    }
}

index.jsp请求页

<%@ page language="java" contentType="text/html; charset=utf-8"
         pageEncoding="utf-8"%>


    
    
        hello
    

运行结果

把应用加入tomcat启动


image.png

你可能感兴趣的:(servlet的注解配置)