栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

创建一个简单的Servlet程序以及相关问题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

创建一个简单的Servlet程序以及相关问题

1.输出 "hello world"

1. 新建一个maven项目

 

2. 在pom.xml文件中配置servlet依赖, 新建的项目中的java与resources目录为自己手动添加

 3. 测试代码

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

public class Servlet01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        PrintWriter writer = resp.getWriter();  // 响应流
        writer.print("Hello World!");
        
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

4. 在Web.xml中添加下列代码注册servlet



  hello
  com.zx.servlet.Servlet01


  
    hello
    /h
  

5.配置Tomcat

 

 

 6. 测试

这个是进入了默认的index.jsp文件

 这里进入Servlet01,输出hello world

 注: 创建maven项目时没有生成src目录没有web.xml文件,可能是由于idea还没有加载出来的原因。重启idea或者自己创建如下

 

 在这两个地方添加srcmainwebapp

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/992564.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号