码迷,mamicode.com
首页 > 其他好文 > 详细

Servlet快速入门

时间:2019-10-15 09:57:59      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:nts   tin   rri   public   抽象   规则   gets   ppi   mapping   

念:Servlet就是一接口,定了Java浏览访问到(tomcat识别)规则
使用:
  1.个实现Servlet接口的
  2.实现接口中的抽象方法
  3.web-->WEB-INF-->web.xml配置Servlet
  4.行服件Tomcat
Servlet代码:
  
 1 //1.定义一个实现Servlet接口的类
 2 public class ServletTest implements Servlet {
 3 /*
 4     2.实现接口中的抽象方法
 5  */
 6     @Override
 7     public void init(ServletConfig servletConfig) throws ServletException {
 8 
 9     }
10 
11     @Override
12     public ServletConfig getServletConfig() {
13         return null;
14     }
15 
16     @Override
17     public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
18         System.out.println("servlet成功启动");
19     }
20 
21     @Override
22     public String getServletInfo() {
23         return null;
24     }
25 
26     @Override
27     public void destroy() {
28 
29     }
30 }

web.xml(配置文件代码):

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
 5          version="4.0">
 6 <!--配置Servlet-->
 7     <servlet>
 8         <servlet-name>servlettest01</servlet-name>
 9         <servlet-class>cn.aikang.Servlet.ServletTest</servlet-class>
10     </servlet>
11     <servlet-mapping>
12         <servlet-name>servlettest01</servlet-name>
13         <url-pattern>/servlettest01</url-pattern>
14     </servlet-mapping>
15 </web-app>

 

 执行原理:
  服务器收到客户端的请求后查找客户端的web.xml
  查找web.xml的<url-pattern>标签后查找到对应的class
  Tomcat会创建该类的对象
  执行class的方法

Servlet快速入门

标签:nts   tin   rri   public   抽象   规则   gets   ppi   mapping   

原文地址:https://www.cnblogs.com/aikang525/p/11675318.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!