`
hai0378
  • 浏览: 517933 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Java Web 胡言乱语 之-- Servlet

 
阅读更多

Servlet一共分为三种:

1,简单Servlet,是作为一种程序所必须的开发结果保存下来的

2,过滤Servlet : javax.Servlet.Filter 接口完成公共协议的操作,

3,监听Servlet

 

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   version="2.5">

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>

  <filter>
    <filter-name>simple</filter-name>
	<filter-class>org.lxh.filterdemo.SimpleFilter</filter-class>
    <init-param>
	  <param-name>ref</param-name>
	  <param-value>helllworld</param-value>
	</init-param>
  </filter>
  <filter-mapping>
     <filter-name>simple</filter-name>
	 <url-pattern>/*</url-pattern>
  </filter-mapping>
 </web-app>

 

2,

<url-pattern>/*</url-pattern>

  表示的是一个过滤器的过滤路径,现在尽然是/*,就表示是项目全路径。

2.1 实现过滤器

在Servlet中,如果要定义个过滤器,则直接让一个类实现 javax.servlet.Filter接口即可,此接口定义了三个操作方法:

 public void init(FilterConfig filterCofig) throws ServletException

 public void doFilter(ServletRequest request ,ServletResponse response,FilterChain chain) throws   

                        IOException ,ServletException  

 public void destory()

 

FilterChain 接口的主要作用是将用户的请求向下传递给其他的过滤器或者是Servlet:

 

 

过滤器实际上是执行两次的 过滤器本身也可以对多个路径执行过滤

 

过滤器在开发中最频繁的两种操作:编码验证,登陆验证

 

过滤器Filter在 web.xml 中的配置应该先配置过滤器,然后再配置简单Servlet(重点记忆 )

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   version="2.5">

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>

  <!-- Simple Filter -->
  <filter>
    <filter-name>simple</filter-name>
	<filter-class>org.lxh.filterdemo.SimpleFilter</filter-class>
    <init-param>
	  <param-name>ref</param-name>
	  <param-value>helllworld</param-value>
	</init-param>
  </filter>
  <!--Encoding Filter -->
   <filter>
    <filter-name>encoding</filter-name>
	<filter-class>org.lxh.filterdemo.EncodingFilter</filter-class>
    <init-param>
	  <param-name>charset</param-name>
	  <param-value>GBK</param-value>
	</init-param>
   </filter>
   <!-- LoginFilter -->
   <filter>
    <filter-name>login</filter-name>
	<filter-class>org.lxh.filterdemo.LoginFilter</filter-class>
   </filter>

  <filter-mapping>
     <filter-name>login</filter-name>
	 <url-pattern>/*<url-pattern>
  </filter-mapping>
  <filter-mapping>
     <filter-name>encoding</filter-name>
	 <url-pattern>/*</url-pattern>
  </filter-mapping>
  <filter-mapping>
     <filter-name>simple</filter-name>
	 <url-pattern>/*</url-pattern>
  </filter-mapping>
 </web-app>
 

 

<--------------------------------------------------------------------------------------------------------------------------->

 

Servlet之监听器

1,对Application进行监听:Application是ServletContext接口的对象,表示是整个上下文的环境,如果要想实现对

 application监听则可以使用如下两个接口:

  1.1  ServletContextListener:是对整个上下文环境的监控

  1.2  ServletContextAttributeListener: 对属性的监听

 

用途:网站在线人数监听

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics