selvlet入门自己部署(sevlet接口实现)

本文介绍了Servlet的出现背景,即为了解决开发动态网页的需求,如留言和互动功能,普通Java技术无法满足的问题。详细讲解了Servlet的基本概念,包括其定义、特点及如何遵循Servlet开发规范进行编程。并通过一个快速入门案例,展示了如何在web.xml中配置Servlet,以及在Java文件中实现Servlet接口,最终在浏览器中显示当前时间。

1.为什么会出现sevlet?

需求:开发动态网页,让用户可以留言,其他人可以回复,用户可以交互的功能,普通的java技术不能完成

java服务器小程序:

a.由服务端来执行的

b.由java语言编写的

c.按照服务器规范开发的

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

e.是学习jsp的基础

其实就是java程序,该Java程序要遵循sevlet开发规范,

 web服务器功能(通讯) 容器功能

 

 

快速入门案例;

 使用接口的方式来开发servlet,同时显示时间

 首先配置web.xml

<?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://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_3_1.xsd"
  version="3.1"
  metadata-complete="true">

<servlet>
     <servlet-name>ServletTest</servlet-name>
     <servlet-class>com.wangzhi.servlet.Myfirst</servlet-class>
 </servlet>

 <servlet-mapping>
       <!-- 映射Servlet -->
       <servlet-name>ServletTest</servlet-name>
       <!--<servlet-name>与上面<Servlet>标签的<servlet-name>元素相对应,不可以随便起名  -->
       <url-pattern>/ServletTest</url-pattern>
       <!-- 上面一句话用于映射访问URL -->
   </servlet-mapping>
</web-app>
View Code

再在classes里面写java文件

package com.wangzhi.servlet;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class Myfirst implements Servlet {
    // 销毁函数,内存中清除,调用一次
    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

    @Override
    public ServletConfig getServletConfig() {
        // TODO Auto-generated method stub
        return null;
    }

    // 获取对象
    @Override
    public String getServletInfo() {
        // TODO Auto-generated method stub
        return null;
    }

    // 每次调用一次,servlet装载内存
    @Override
    public void init(ServletConfig arg0) throws ServletException {
        // TODO Auto-generated method stub

    }

    @Override
    public void service(ServletRequest req, ServletResponse res)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
System.out.println("hello,world!"+new java.util.Date());
res.getWriter().println("hello,world"+new java.util.Date());

    }

}
View Code

转载于:https://www.cnblogs.com/helloworld2019/p/10948988.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值