服务器和Servlet
Web服务器
- 监听一个TCP端口
- 转发请求,回收响应
- 本身没有业务逻辑,连接操作系统和应用程序代码
系统Web服务模型
系统Web服务模型.pngServlet
- 一种规范:约束了Java服务器与业务类的通信方式
- 一个接口:javax.servlet.Servlet
- 一种Java类:实现了Servlet接口的应用程序类
Servlet.png
Tomcat
- Java原生,运行在JVM上
- 多种并发模型,高性能
- 支持嵌入应用程序
集成Tomcat
1. 添加tomcat依赖
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
// https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-core
compile group: 'org.apache.tomcat.embed', name: 'tomcat-embed-core', version: '8.5.23'
}
2. 创建TomcatServer类
package com.istimeless.web.server;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;
/**
* @author lijiayin
*/
public class TomcatServer {
private Tomcat tomcat;
private String[] args;
public TomcatServer(String[] args){
this.args = args;
}
public void startServer() throws LifecycleException {
tomcat = new Tomcat();
tomcat.setPort(6699);
tomcat.start();
Thread awaitThread = new Thread("tomcat_await_thread") {
@Override
public void run() {
TomcatServer.this.tomcat.getServer().await();
}
};
awaitThread.setDaemon(false);
awaitThread.start();
}
}
3. 修改MiniApplication类
package com.istimeless.starter;
import com.istimeless.web.server.TomcatServer;
import org.apache.catalina.LifecycleException;
/**
* @author lijiayin
*/
public class MiniApplication {
public static void run(Class<?> cls, String[] args){
System.out.println("Hello Mini-Spring!");
TomcatServer tomcatServer = new TomcatServer(args);
try {
tomcatServer.startServer();
} catch (LifecycleException e) {
e.printStackTrace();
}
}
}
4. 运行并测试
运行结果
使用浏览器访问http://localhost:6699时返回404 测试结果.png
5. 创建TestServlet类,只需要实现service方法
package com.istimeless.web.servlet;
import javax.servlet.*;
import java.io.IOException;
/**
* @author lijiayin
*/
public class TestServlet implements Servlet {
@Override
public void init(ServletConfig config) throws ServletException {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
res.getWriter().println("test");
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
6. 修改TomcatServer类
package com.istimeless.web.server;
import com.istimeless.web.servlet.TestServlet;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
/**
* @author lijiayin
*/
public class TomcatServer {
private Tomcat tomcat;
private String[] args;
public TomcatServer(String[] args){
this.args = args;
}
public void startServer() throws LifecycleException {
tomcat = new Tomcat();
tomcat.setPort(6699);
tomcat.start();
//注入servlet
Context context = new StandardContext();
context.setPath("");
context.addLifecycleListener(new Tomcat.FixContextListener());
//自己定义的servlet
TestServlet servlet = new TestServlet();
Tomcat.addServlet(context, "testServlet", servlet)
.setAsyncSupported(true);
//名称和上面的一致
context.addServletMappingDecoded("/test.json", "testServlet");
//context添加到host中
tomcat.getHost().addChild(context);
Thread awaitThread = new Thread("tomcat_await_thread") {
@Override
public void run() {
TomcatServer.this.tomcat.getServer().await();
}
};
awaitThread.setDaemon(false);
awaitThread.start();
}
}
7. 运行并测试
运行结果
使用浏览器访问http://localhost:6699/test.json 测试结果.png
网友评论