重新学习Servlet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package javax.servlet; import java.io.IOException; public interface Servlet { public void init(ServletConfig config) throws ServletException; public ServletConfig getServletConfig(); public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException; public String getServletInfo(); public void destroy(); } |
1 | public abstract class GenericServlet implements Servlet, ServletConfig, java.io.Serializable |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | package com.xh.test.api; import javax.servlet. * ; import java.io.IOException; / * * * Created by root on 17 - 11 - 12. * / public class MyGenericServlet extends GenericServlet { public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { System.out.println( ">>>service " + req + " , " + res); String id = req.getParameter( "id" ); System.out.println( ">>>id " + id ); res.getWriter().write( "hello i am MyGenericServlet" ); res.flushBuffer(); } @Override public void init(ServletConfig config) throws ServletException { System.out.println( ">>>init_p :" + config); super .init(config); } @Override public void init() throws ServletException { System.out.println( ">>>init" ); super .init(); } @Override public void destroy() { System.out.println( ">>>destroy" ); super .destroy(); } public MyGenericServlet() { System.out.println( ">>>MyGenericServlet" ); } } |
访问:http://localhost:8080/testservlet/1?id=100
浏览器输出:hello i am MyGenericServlet
控制台输出:
>>>MyGenericServlet
>>>init_p :org.apache.catalina.core.StandardWrapperFacade@4a1957bf
>>>init
>>>service org.apache.catalina.connector.RequestFacade@e4f73de , org.apache.catalina.connector.ResponseFacade@297fd904
>>>id 100
PS:容器启动的时候没有初始化,第一次访问才初始化。init(ServletConfig config)的参数由容器传入。ServletConfig包括当前Servlet的名字,服务器监听的地址等
配置文件:
web.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <!DOCTYPE web - app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web - app> <display - name>Archetype Created Web Application< / display - name> <servlet> <servlet - name>MyGenericServlet< / servlet - name> <servlet - class >com.xh.test.api.MyGenericServlet< / servlet - class > < / servlet> <servlet - mapping> <servlet - name>MyGenericServlet< / servlet - name> <url - pattern> / 1 < / url - pattern> < / servlet - mapping> < / web - app> |
pom.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | <project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" > <modelVersion> 4.0 . 0 < / modelVersion> <groupId>com.xh.test.servlet< / groupId> <artifactId>testservlet< / artifactId> <packaging>war< / packaging> <version> 1.0 - SNAPSHOT< / version> <name>testservlet Maven Webapp< / name> <url>http: / / maven.apache.org< / url> <dependencies> <dependency> <groupId>junit< / groupId> <artifactId>junit< / artifactId> <version> 3.8 . 1 < / version> <scope>test< / scope> < / dependency> <dependency> <groupId>javax.servlet< / groupId> <artifactId>javax.servlet - api< / artifactId> <version> 3.1 . 0 < / version> <scope>provided< / scope> < / dependency> < / dependencies> <build> <plugins> <! - - 本地环境使用 - - > <plugin> <groupId>org.apache.tomcat.maven< / groupId> <artifactId>tomcat7 - maven - plugin< / artifactId> <version> 2.1 < / version> <configuration> <hostName>localhost< / hostName> <! - - Default: localhost - - > <port> 8080 < / port> <! - - 启动端口 Default: 8080 - - > <path> / ${project.artifactId}< / path> <! - - 访问应用路径 Default: / ${project.artifactId} - - > <uriEncoding>UTF - 8 < / uriEncoding> <! - - uri编码 Default: ISO - 8859 - 1 - - > < / configuration> < / plugin> < / plugins> <finalName>testservlet< / finalName> < / build> < / project> |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 【非技术】说说2024年我都干了些啥