实现servlet的三种方式
1、实现Servlet接口。
2、继承GenericServlet。
3、继承HttpServlet。
第三种方式是现在比较常用的。
第一种:
[java] view plain copy
- package com.tsinghua;
- import javax.servlet.*;
- import java.io.*;
- public class Hello implements Servlet{
- /**
- * 销毁servlet实例,释放内存
- * 3种情况:
- * 1.reload 该servlet(所在的webapps)
- * 2.关闭tomcat
- * 3.关机
- */
- @Override
- public void destroy() {
- // TODO Auto-generated method stub
- System.out.println("destroy it");
- }
- @Override
- public ServletConfig getServletConfig() {
- // TODO Auto-generated method stub
- return null;
- }
- @Override
- public String getServletInfo() {
- // TODO Auto-generated method stub
- return "";
- }
- /**
- * 初始化该servlet,类似于构造函数。
- * 该函数只会被调用一次。当用户第一次访问该servlet时被调用。
- */
- @Override
- public void init(ServletConfig arg0) throws ServletException {
- // TODO Auto-generated method stub
- System.out.println("init it");
- }
- /**
- * 这个函数用于处理业务逻辑
- * 程序员应该把业务逻辑代码写在这里
- * 这个函数会被调用多次,当用户每访问该servlet时都会被调用。
- * req:用来获得客户端的信息
- * res:用来对客户端返回信息
- */
- @Override
- public void service(ServletRequest req, ServletResponse res)
- throws ServletException, IOException {
- // TODO Auto-generated method stub
- System.out.println("service it");
- //从res中得到PrintWriter
- PrintWriter pw=res.getWriter();
- pw.println("hello,world");
- }
- }
第二种:
[java] view plain copy
- package com.tsinghua;
- import javax.servlet.GenericServlet;
- import javax.servlet.ServletException;
- import javax.servlet.ServletRequest;
- import javax.servlet.*;
- import java.io.*;
- public class HelloGen extends GenericServlet{
- /**
- * 重写service方法即可。
- */
- @Override
- public void service(ServletRequest req, ServletResponse res)
- throws ServletException, IOException {
- // TODO Auto-generated method stub
- //返回HelloWorld!generic
- PrintWriter pw=res.getWriter();
- pw.println("HelloWorld!generic");
- }
- }
第三种:
[java] view plain copy
- package com.tsinghua;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.*;
- public class HelloHttp extends HttpServlet{
- /**
- * 处理get请求
- * req:客户端的请求信息
- * resp:客户端的返回信息
- */
- public void doGet(HttpServletRequest req, HttpServletResponse resp)
- {
- //业务逻辑在这里写。
- try {
- PrintWriter pw=resp.getWriter();
- pw.println("Hello,Http");
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- /**
- * 处理post请求
- * req:客户端的请求信息
- * resp:客户端的返回信息
- */
- public void doPost(HttpServletRequest req, HttpServletResponse resp)
- {
- //post和get请求一样
- this.doGet(req, resp);
- }
- }
servlet的配置:
[html] view plain copy
- <?xml version="1.0" ?>
- <!--
- 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>
- <!-- servlet的配置 -->
- <servlet>
- <servlet-name>hello</servlet-name><!-- servlet的名字,任意的 -->
- <servlet-class>com.tsinghua.Hello</servlet-class><!-- 指明servlet的包名+类名 -->
- </servlet>
- <!-- 映射关系配置 -->
- <servlet-mapping>
- <servlet-name>hello</servlet-name><!-- servlet的名字,和上面一样 -->
- <url-pattern>/hello</url-pattern><!-- 这是在浏览器中输入的访问该servlet的url -->
- </servlet-mapping>
- <!-- servlet的配置 -->
- <servlet>
- <servlet-name>helloGen</servlet-name><!-- servlet的名字,任意的 -->
- <servlet-class>com.tsinghua.HelloGen</servlet-class><!-- 指明servlet的包名+类名 -->
- </servlet>
- <!-- 映射关系配置 -->
- <servlet-mapping>
- <servlet-name>helloGen</servlet-name><!-- servlet的名字,和上面一样 -->
- <url-pattern>/helloGen</url-pattern><!-- 这是在浏览器中输入的访问该servlet的url -->
- </servlet-mapping>
- <!-- servlet的配置 -->
- <servlet>
- <servlet-name>helloHttp</servlet-name><!-- servlet的名字,任意的 -->
- <servlet-class>com.tsinghua.HelloHttp</servlet-class><!-- 指明servlet的包名+类名 -->
- </servlet>
- <!-- 映射关系配置 -->
- <servlet-mapping>
- <servlet-name>helloHttp</servlet-name><!-- servlet的名字,和上面一样 -->
- <url-pattern>/helloHttp</url-pattern><!-- 这是在浏览器中输入的访问该servlet的url -->
- </servlet-mapping>
- </web-app>