一个简单的servlet的demo
javaweb 的应用我们需要参考javaee api
查找servlet接口
javax.servlet
Interface Servlet
- All Known Subinterfaces:
- HttpJspPage, JspPage
- All Known Implementing Classes:
- FacesServlet, GenericServlet, HttpServlet
-
public interface Servlet
Implemented by: FacesServlet, GenericServlet, JspPage
定义所有 servlet 都必须实现的方法。
servlet 是运行在 Web 服务器中的小型 Java 程序。servlet 通常通过 HTTP(超文本传输协议)接收和响应来自 Web 客户端的请求。
要实现此接口,可以编写一个扩展 javax.servlet.GenericServlet 的一般 servlet,或者编写一个扩展 javax.servlet.http.HttpServlet 的 HTTP servlet。
此接口定义了初始化 servlet 的方法、为请求提供服务的方法和从服务器移除 servlet 的方法。这些方法称为生命周期方法,它们是按以下顺序调用的:
- 构造 servlet,然后使用
init方法将其初始化。 - 处理来自客户端的对
service方法的所有调用。 - 从服务中取出 servlet,然后使用
destroy方法销毁它,最后进行垃圾回收并终止它。
除了生命周期方法之外,此接口还提供了 getServletConfig 方法和 getServletInfo 方法,servlet 可使用前一种方法获得任何启动信息,而后一种方法允许 servlet 返回有关其自身的基本信息,比如作者、版本和版权。
See also
javax.servlet.GenericServlet, javax.servlet.http.HttpServlet
Defines methods that all servlets must implement.
A servlet is a small Java program that runs within a Web server. Servlets receive and respond to requests from Web clients, usually across HTTP, the HyperText Transfer Protocol.
To implement this interface, you can write a generic servlet that extends javax.servlet.GenericServlet or an HTTP servlet that extends javax.servlet.http.HttpServlet.
This interface defines methods to initialize a servlet, to service requests, and to remove a servlet from the server. These are known as life-cycle methods and are called in the following sequence:
- The servlet is constructed, then initialized with the
initmethod. - Any calls from clients to the
servicemethod are handled. - The servlet is taken out of service, then destroyed with the
destroymethod, then garbage collected and finalized.
In addition to the life-cycle methods, this interface provides the getServletConfig method, which the servlet can use to get any startup information, and the getServletInfo method, which allows the servlet to return basic information about itself, such as author, version, and copyright.
由此我们看出 要想实现动态的网页必须集成servlet接口 或者 继承javax.servlet.GenericServlet or an HTTP servlet that extends javax.servlet.http.HttpServlet.
我们创建一个 servlet的demo
先添加一个 servlet类 集成接口Servlet
package cn.jiemoxiaodi.servlet; import java.io.IOException;
import java.io.OutputStream; import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; public class AServlet implements Servlet{ public void destroy() {
// TODO Auto-generated method stub
System.out.println("destory");
} public void init(ServletConfig arg0) throws ServletException {
// TODO Auto-generated method stub
System.out.println("init");
} public void service(ServletRequest arg0, ServletResponse arg1)
throws ServletException, IOException {
// TODO Auto-generated method stub
OutputStream ops= arg1.getOutputStream();
ops.write("hellow".getBytes()); System.out.println("hello--service");
} public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return null;
} public String getServletInfo() {
// TODO Auto-generated method stub
return null;
} }
我们创建完类型后我们要告诉我们的服务器,有这个动态网页资源啦 在web.xml 中做如下配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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">
<display-name></display-name> <!-- 配置servlet到项目中 -->
<servlet>
<!-- 可以随便填只需要注意不要和其他servlet的名字重复即可(建议使用简单类名) -->
<servlet-name>AServlet</servlet-name>
<!-- 配置servlet的完整类名 -->
<servlet-class>cn.jiemoxiaodi.servlet.AServlet</servlet-class>
</servlet> <!-- 配置上面这个servlet使用那个路径能被访问 -->
<servlet-mapping>
<!-- 此处不能随便写了,必须与上面的servlet对应,表示在为那个servlet配置路径 -->
<servlet-name>AServlet</servlet-name>
<!-- 配置访问这个servlet的路径 (相对路径) /==>http://localhost:8080/day10_servlet/ http://localhost:8080/day10_servlet/AServlet -->
<url-pattern>/AServlet</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
到此就可以了。
http://localhost:8080/项目名字/AServlet

一个简单的servlet的demo的更多相关文章
- Servlet复习1: 一个简单的Servlet的使用
Servlet学习 1. Servlet与JSP的关系 2. Servlet的声明周期 3. 一个简单的Servlet的使用方法 什么是Servlet? 什么又是JSP? 继承了javax.servl ...
- 一个简单的webservice的demo(下)winform异步调用webservice
绕了一大圈,又开始接触winform的项目来了,虽然很小吧.写一个winform的异步调用webservice的demo,还是简单的. 一个简单的Webservice的demo,简单模拟服务 一个简单 ...
- 一个简单的Webservice的demo(中)_前端页面调用
首先新建项目,这里有两种调用方式,为了能方便理解,新建页面WebserviceTest如下图: 先引用写好的服务,这里用上次写好的服务.见上次写的一个简单的Webservice的demo,简单模拟服务 ...
- 在cmd下编译一个简单的servlet时出现程序包javax.servlet不存在
由于servlet和JSP不是Java平台JavaSE(标准版)的一部分,而是Java EE(企业版)的一部分,因此,必须告知编译器servlet的位置. 解决“软件包 javax.servlet不存 ...
- 开发部署一个简单的Servlet
Servlet是一个执行在服务器端的Java Class文件,载入前必须先将Servlet程序代码编译成.class文件,然后将此class文件放在servlet Engline路径下.Servlet ...
- Tomcat剖析(二):一个简单的Servlet服务器
Tomcat剖析(二):一个简单的Servlet服务器 1. Tomcat剖析(一):一个简单的Web服务器 2. Tomcat剖析(二):一个简单的Servlet服务器 3. Tomcat剖析(三) ...
- 一个简单的servlet容器
[0]README 0.1)本文部分文字转自 “深入剖析Tomcat”,旨在学习 一个简单的servlet容器 的基础知识: 0.2)for complete source code, pleas ...
- 一个简单的Webservice的demo,简单模拟服务
前段时间一直在学习WCF,匆匆忙忙的把<WCF全面解析>和<WCF服务编程>看了一遍,好多东西都不是很懂,又听了一下WCF分布式开发的网络教程,算是马马虎虎的明白点了.回顾了一 ...
- 一个简单的Servlet容器实现
上篇写了一个简单的Java web服务器实现,只能处理一些静态资源的请求,本篇文章实现的Servlet容器基于前面的服务器做了个小改造,增加了Servlet请求的处理. 程序执行步骤 创建一个Serv ...
随机推荐
- 平行四边形导航,背景颜色渐变动画(不支持IE6/7/8)
body{ font-size: 14px; } ul ,li{ margin:0px; padding:0px; list-style: none; } .box{ width: 1000px; h ...
- 51Nod-1136 欧拉函数
51Nod: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1136 1136 欧拉函数 基准时间限制:1 秒 空间限制: ...
- 85 megacli-查看raid信息
文章本身我不做过多修改了,在这里我就把自己在安装时候碰到的难点跟大家提下.1.何处下载?首先,根据文章中的路径已经下载不到相应的文件了,在此我们就自己到http://www.lsi.com的网站上去搜 ...
- 动画的使用—Drawable Animation
Drawable Animation可以称为帧动画,因为它是通过每次播放一帧Drawable资源实现的. Drawable Animation算不上真正意义上的动画,因为它的内部实现是通过定时发送消息 ...
- 【USACO 2.3】Controlling Companies (递推)
题意:A公司对B公司有控制权的条件是满足下面条件之一:A=B,A对B的股份超过50%,A控制的公司对B的股份之和超过50%. 分析:我把控制关系分个等级:第一级是直接的股份超过50%,第二级是至少需要 ...
- 在eclipse中部署jsp项目
昨天在做实验的时候发现图片的路径不对,怎么改都不对,后来想到在浏览器中输入localhost:8080是tomcat服务器的路径,没找到资源是不是就是项没有部署到tomcat中,去tomcat的web ...
- 大二在CSDN的博客整理
001我为什么想写博客 控制台版2048 version_1.0总结 022 囚徒困境中的均衡-----从一篇经典论文说起 021 模拟退火算法学习(一)-----求解最短连通路径 020 小记一次网 ...
- WEKA使用
参考 http://bbs.middleware123.com/thread-24052-1-1.html 使用Weka进行数据挖掘 http://quweiprotoss.blog.163.com ...
- SDL 显示解码后的yuv12数据
在上篇h264解码为yuv12后http://jhlong12345.blog.163.com/blog/static/1230631292015725115058709/ ,需要显示出来,使用sdl ...
- 使用Nominatim进行openstreetmap地址搜索/解析
Nominatim(来自拉丁语,意思是“名称”)是一个可以按名称和地址来搜索OSM中的数据,并生成OSM点的合成地址的工具(反向地理编码).可用在http://nominatim.openstreet ...