eclipse搭建java web项目

时间:2024-03-10 17:44:44

准备条件

  1. eclipse
  2. jdk1.8 (配置jdk环境)
  3. apache-tomcat-8.5.97(记住安装位置)

 

点击完成

开始创建javaweb项目

 

 

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

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class test extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取 PrintWriter 对象
    	   response.setCharacterEncoding("UTF-8");
    	   response.setContentType("text/plain; charset=UTF-8");
        PrintWriter pw = response.getWriter();
        // 输出 Hello World
        pw.print("输出 Hello World");
    }
}

 目录结构如下:

 

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    
    <servlet>
        <servlet-name>fly</servlet-name>
        <servlet-class>fly.test</servlet-class>
    </servlet>


    <servlet-mapping>
        <servlet-name>fly</servlet-name>
        <url-pattern>/hi</url-pattern>
    </servlet-mapping>
</web-app>

   

 

 

 

 

 会出现此界面表示成功