servlet是动态网页的一种技术,用java语言实现。在tomcat中,servlet的创建是有tomcat实现。
servlet接口中有5个函数:
其他信息:init、service、destroy是生命周期函数。init destroy是调用一次。而service是调用多次。
servlet其实就是应用中的一个小程序,一个应用部署中可以由多个servlet。根据url的应用信息可以找到对应的web应用。servlet的路径需要在web的文件中进行指定。多个url可以指定一个name。或者使用通配符。
url路径匹配:
<servlet>
<name></name>
<servlet-class></servlet-class>
</servlet>
<servlet-mapping>
<name></name>
<url-pattern></url-pattern>
</servlet-mapping>
复制代码
*********注意servlet的url路径前要有/表示相对地址。如果没有/部署会报错。
主要实现是管理容器传给servlet一些信息。初始化参数与servletContext,serverlet标签在配置文件中的写法如下图:name是唯一标识,class代表真是的servlet类。里面包含一些initparameter配置信息。
继承了servlet与configservlet接口。并且实现log的方法,这个方法主要方便我们编写servlet接口的函数。
基于http协议继承了servlet类。将service()函数分散到http中的各种方法中 相关函数介绍:
servletContext对应一个应用,包含一个servlet容器的相关信息。这个类是很重要的,也称为一个数据域(一个应用的servlet可以相互传递信息)
System.out.println(context1.getContextPath());// return /web3 。uri对象的应用地址 复制代码
ServletContext context2=context1.getContext("/web2");
System.out.println(context2.getContextPath());//抛出异常,null指针
ServletContext context3=context1.getContext("/web3");
System.out.println(context3.getContextPath());// /web3 没有任何用处。
复制代码
System.out.println(context1.getMimeType("abc.text")); //text/plain
System.out.println(context1.getMimeType("abc.jpg")); //image/jpeg
复制代码
For example, for a web application containing:
/welcome.html
/catalog/index.html
/catalog/products.html
/catalog/offers/books.html
/catalog/offers/music.html
/customer/login.jsp
/WEB-INF/web.xml
/WEB-INF/classes/com.acme.OrderServlet.class
/WEB-INF/lib/catalog.jar!/META-INF/resources/catalog/moreOffers/books.html
getResourcePaths("/") would return {"/welcome.html", "/catalog/", "/customer/", "/WEB-INF/"}, and getResourcePaths("/catalog/") would return {"/catalog/index.html", "/catalog/products.html", "/catalog/offers/", "/catalog/moreOffers/"}.
复制代码
System.out.println(context1.getResource("a.txt"));
//print:file:/D:/java_temp/plug1/out/artifacts/web3_war_exploded/a.txt
复制代码
<context-param>
<param-name>name1</param-name>
<param-value>value1</param-value>
</context-param>
复制代码
ServletRegistration.Dynamic addServlet(String servletName, String className)
ServletRegistration.Dynamic addServlet(String servletName, Servlet servlet)
ServletRegistration.Dynamic addServlet(String servletName, Class<? extends Servlet> servletClass)把servlet增加到context里面,相当于注册。
T createServlet(Class clazz) throws ServletException创建servlet的类 18.
ServletRegistration getServletRegistration(String servletName)
Map<String,? extends ServletRegistration> getServletRegistrations()返回注册的servlet。
FilterRegistration.Dynamic addFilter(String filterName, String className)
FilterRegistration.Dynamic addFilter(String filterName,Filter filter)
FilterRegistration.Dynamic addFilter(String filterName, Class<? extends Filter> filterClass)
T createFilter(Class clazz)
FilterRegistration getFilterRegistration(String filterName)
Map<String,? extends FilterRegistration> getFilterRegistrations() 同上servlet 20.其他的都是关于listener、session、classloader、declareRoles
web的工程如下图,在hello.html增加超链接的网页,其中第一个不能进行相对地址到servlet程序:相对地址为http://localhost:8080/bservlet。相对地址一般采用三张方式:
<a href="/bservlet" target="_blank">bservlet</a> <a href="bservlet" target="_blank">bservlet</a> <a href="/web3/bservlet" target="_blank">bservlet</a> <a href="http://localhost:8080/web3/bservlet" target="_blank">bservlet</a> 复制代码