Servlet三种开发方式之servlet接口

Servlet技术是在java ee 出现之前就存在了,在开发动态网页中,得到广泛的应用,直到现在java ee项目中也是非常重要的,同时jsp也是在servlet的基础上发展起来的。因此,掌握好servlet太重要了。

Servlet是用java编写的服务器程序,它的特点:

  Ⅰ.他是由服务器端(tomcat)调用和执行

  Ⅱ.他是用java语言编写的

  Ⅲ.他是按照Servlet规范开发的

  Ⅳ.功能强大,可以完成几乎所有的网站功能  

  Ⅴ.是学习jsp基础

实现servlet接口

 这个是最早期使用的一种方式,用起来比较麻烦,那为什么要学servlet接口呢?因为它学它来了解servlet生命周期非常好。实现servlet接口的步骤:

  Ⅰ.建立一个web应用MyServletWeb

  Ⅱ.MyServletWeb下建立WEB-INF/web.xml,可以在ROOT项目里拷贝

  Ⅲ.在MyServletWeb下建立classes目录(我们的Servlet就要在该项目开发),l建立lib目录

  Ⅳ.开发MyFirstServlet.java,实现servlet接口

  Ⅴ.根据Servlet规范,我们还需要部署Servlet,在web.xml文件里部署

  补充:如果使用javac去编译一个java文件,则需要带命令参数 javac -d . MyFristServlet.java 文件

MyFristServlet文件

package com.beekc.www;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.lang.*;

public class MyFristServlet implements Servlet
{
	//该函数用于初始化servlet,就是把该servlet装载到内存中
	//只会被调用一次
	public void init(ServletConfig config) throws ServletException
	{
		
	}
	
	//得到ServletConfig对象
	public ServletConfig getServletConfig()
	{
		return null;
	}
	
	//该函数是服务函数,我们的业务逻辑代号就是写在这里
	//该函数每次都会被调用
	public void service(ServletRequest req,ServletResponse res) throws ServletException,IOException
	{
		res.getWriter().println("Hello world " + new java.util.Date());
		
	}
	
	//该函数是得到servlet配置信息
	public String getServletInfo()
	{
		return null;
	}
	
	//销毁该servlet,从内存中清除
	//只会被调用一次
	public void destroy()
	{
		
	}
	
}

web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!--
 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://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  version="4.0"
  metadata-complete="true">

	<!--根据serlvet规范,需要将Servlet部署到web.xml文件,该部署可以从examples下拷贝-->
    <servlet>
	  <!--servlet-name是将来访问该servlet的资源名,该名字可以自己定义:默认就使用该servlet的名字-->
      <servlet-name>MyFristServlet</servlet-name>
	  <!--servlet-class要指明Servlet放在哪个包下-->
      <servlet-class>com.beekc.www.MyFristServlet</servlet-class>
    </servlet>
	<!--Servlet的映射-->
	<servlet-mapping>
		<!--这个servlet-name要和上面servlet-name名字一样-->
        <servlet-name>MyFristServlet</servlet-name>
		<!--来访问Servlet的资源名部分-->
        <url-pattern>/MyFristServlet</url-pattern>
    </servlet-mapping>

</web-app>

  Ⅵ.测试结果

 

猜你喜欢

转载自www.cnblogs.com/beekc/p/12588148.html