selvlet入门自己部署(sevlet接口实现)
1.为什么会出现sevlet?
需求:开发动态网页,让用户可以留言,其他人可以回复,用户可以交互的功能,普通的java技术不能完成
java服务器小程序:
a.由服务端来执行的
b.由java语言编写的
c.按照服务器规范开发的
d.功能强大,几乎可以完成所有的网站功能
e.是学习jsp的基础
其实就是java程序,该Java程序要遵循sevlet开发规范,
web服务器功能(通讯) 容器功能
快速入门案例;
使用接口的方式来开发servlet,同时显示时间
首先配置web.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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>
再在classes里面写java文件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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()); } }