创建一个接口:

package my.web;

public interface SpringWeb {
void config();
}

实现类:

package my;

import my.web.SpringWeb;

public class SpringInit implements SpringWeb {
@Override
public void config() {
System.out.println("大家好");
}
}
import my.web.SpringWeb;

public class SpringWeblnitializer implements SpringWeb {
@Override
public void config() {
System.out.println("你好,哈皮!");
}
}

创建:MyWebConfig 等同于web.xml

package my.web;

import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.HandlesTypes;
import java.util.Set; // 等同于web.xml文件
@HandlesTypes({SpringWeb.class})
public class MyWebConfig implements ServletContainerInitializer {
@Override
public void onStartup(Set<Class<?>> set, ServletContext servletContext) throws ServletException {
System.out.println("hello wrold"); for (Class<?> aclass : set) {
SpringWeb o = null;
try{
o = (SpringWeb) aclass.newInstance();
o.config();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
} }
}

创建一个servlet 继承于 HttpServlet

package my.web;

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; @WebServlet("/aaa")
public class MyServlet extends HttpServlet { @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
PrintWriter writer = resp.getWriter();
writer.write("。。。。。");
}
}

  

在src下创建 META-INF 包

其次在创建一个 services 的包  添加一个filed的文件

//加上这一句 目的项目在初始化自动找到web.xml文件
my.web.MyWebConfig

结果:

源码地址:https://github.com/nongzihong/servlet_new

使用 java替换web项目的web.xml的更多相关文章

  1. Visual Studio中用于ASP.NET Web项目的Web服务器

    当您在 Visual Studio 中开发 Web 项目时,需要 Web 服务器才能测试或运行它们. 利用 Visual Studio,您可以使用不同的 Web 服务器进行测试,包括 IIS Expr ...

  2. maven web项目的web.xml报错The markup in the document following the root element must be well-formed.

    maven项目里面的web.xml开头约束是这样的 <?xml version="1.0" encoding="UTF-8"?> <web-a ...

  3. Idea创建Maven Web项目的web.xml版本问题

    问题描述:创建Maven Web项目时,选择MavenWebapp模板时,自动生成的web.xml文件版本为1.4,如图所示 如何才能修改为常用的4.0版本的xml文件呢? 这个文件是从Maven仓库 ...

  4. 一个电商项目的Web服务化改造

    一个电商项目的Web服务化改造 项目,早期是随便瞎做的,没啥架构,连基本的设计也没. 有需求,实现需求,再反复修改. 大致就是这么做的. 最近,项目要重新架构,和某boss协商的结果是,采用阿里开源的 ...

  5. Eclipse 启动Tomcat后web项目的classes的子文件夹中没有calss文件

    Eclipse 启动Tomcat后web项目的classes的子文件夹中没有calss文件. 经网上查得以下方法可解决 把properties属性里的java compiler-->buildi ...

  6. 如何修改MyEclipse项目的web context-root

    修改一个MyEclipse项目的名称很容易,右键点项目->rename就行了. 但此时项目的web context-root 还没有改变,需要右键点项目->properties->M ...

  7. maven项目的pom.xml文件详解

    <project xmlns="http://maven.apache.org/POM/4.0.0 " 2 xmlns:xsi="http://www.w3.org ...

  8. 对于maven创建spark项目的pom.xml配置文件(图文详解)

    不多说,直接上干货! http://mvnrepository.com/ 这里,怎么创建,见 Spark编程环境搭建(基于Intellij IDEA的Ultimate版本)(包含Java和Scala版 ...

  9. springMVC+Mybatis的maven-web项目的pom.xml文件内容

    pom.xml文件内容 <!-- 第一行是XML头,指定了该xml文档的版本和编码方式 --> <project xmlns="http://maven.apache.or ...

随机推荐

  1. MySQL总结(4)

    insert 数据的插入 INSERT插入数据

  2. 函数进阶装B操作

    三元表达式 条件成立时的返回值 if 条件 else 条件不成立时的返回值 x = 10 y = 20 print(f"x if x > y else y: {x if x > ...

  3. 单元测试 - tox 使用

    1. 问题一 $ tox -e pep8 -- testdemo.server pep8 installed: alembic==,amqp==,appdirs==,Babel==,beautiful ...

  4. ftp建立虚拟用户实现文件上传和下载

    环境 centos7 1.开启vsftpd服务 2.检查vsftpd服务是否开启 3.添加虚拟用户口令文件 vi etc/vsftpd/vuser.txt 4.生成虚拟用户口令认证文件 如果没有db_ ...

  5. windows设置通过NFS连接到Linux共享数据

    win7下增加了很多有用的功能,只是默认没有开启而已,今天简述下一个WIN7下的NFS功能,通过这个功能,可以让win7共享Linux下面的磁盘分区或者目录数据,这个功能原理只能通过samba或者ft ...

  6. Linux下网络设置

    1.临时IP配置 # ifconfig eth0   192.168.110.118    netmask 255.255.255.0   gateway 192.168.110.2    up # ...

  7. 【writeup】is_numeric函数矛盾运用

    最近在BugkuCTF平台刷题,遇到‘矛盾’http://120.24.86.145:8002/get/index1.php,感觉蛮有意思的,记录下思路 目标代码如下: $num=$_GET['num ...

  8. Ubuntu16.04下caffe CPU版的图片训练和测试

    一 数据准备 二.转换为lmdb格式 1.首先,在examples下面创建一个myfile的文件夹,来用存放配置文件和脚本文件.然后编写一个脚本create_filelist.sh,用来生成train ...

  9. VM10 不能安装VMware tools的解决方法

    当安装VMware tools,提示"正在进行简易安装时,无法手动启动VMware TOOLS安装",把CD-ROM设置成自动检测就可以了.

  10. Java 接口和内部类

    接口的方法默认死public,接口的属性默认为public static final. 为什么要接口,而不是使用抽象类.一个对象只能继承一个类,却可以实现多个接口. clone: Object的浅拷贝 ...