第一个servet(用注解),不用web.xml

环境: idea 

1.新建模块

2.在蓝色src下新建一个包com.test

3.在包下新建servlet

4.写代码

package com.test;

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;
import java.io.PrintWriter;

/**
 * Created by buer on 2018/4/9.
 */
@WebServlet(name = "Servlet2",urlPatterns = "/onee")

public class Servlet2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

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

        PrintWriter  out = response.getWriter();
        out.print("2333333333");
        String name = request.getParameter("name1");
        out.print(name);
    }
}

 

 

 

5.打开网址:http://localhost:8080/onee?name1=11

 

 

 

第一个servet(用注解),不用web.xml_第1张图片

你可能感兴趣的:(JavaWeb基础)