servlet笔记03:继承servlet接口开发


开发servlet三种方式:

实现servlet接口

继承 Generic Servlet

继承HttpServlet

 

需求:通过继承servlet接口,开发一个servlet,要求可以显示hello,world.同时显示当前时间。

 

步骤:

       1:见Servlet笔记01

       2:开发servlet

package com.Servlet;

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

class TextServlet1 implements Servlet
{
	public void init(ServletConfig config) throws ServletException
	{
		//该函数用来初始化servlet,就是把该servlet装载到内存中,该函数只会被调用一次
		
	}
	
	
	//得到ServletConfig对象
	public ServletConfig getServletConfig()
	{
	
		return null;
	}
	
	//得到Servlet的配置信息
	public String getServletInfo()
	{
	
		return null;
	}
	
	//服务函数,业务逻辑代码写在这里
	public void service(ServletRequest request, ServletResponse response) throws ServletException,java.io.IOException
						
	{
		//在控制台输出
		System.out.println("hello,world"+new java.util.Date());
		//在浏览器输出
		response.getWriter().println("hello,world"+new java.util.Date());
	}
	
	//销毁该servlet
	public void destroy()
	{
	
	}


}

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>
  
  <!--根据servlet规范,需要将servlet部署到web.xml文件。该部署配置可以从examples下复制-->
  <servlet>
  	<!--servlet-name 给Servlet取个名字,改名字可以自己定义,默认使用该servlet的名字-->
      <servlet-name>TextServlet1</servlet-name>
      <!--servlet-class指定该servlet放在哪个包下面,格式是包.文件夹.文件名(不加扩展名)-->
      <servlet-class>com.Servlet.TextServlet1</servlet-class>
  </servlet>
	<servlet-mapping>
				<!--此处名字要与上边的一样-->
        <servlet-name>TextServlet1</servlet-name>
        <!--url-pattern这里就是将来访问该servlet的资源名部分,默认命名规范就是该servlet的名字-->        
<url-pattern>/jsp/chat/chat</url-pattern>
 	</servlet-mapping>
</web-app>


 

小技巧:

如果需要用javac编译一个带包的java文件,需要加参数

Javac –d . 文件名

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值