servlet Servlet例子
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实例:
在F:\Tomcat 7.0\webapps\helloServlet\WEB-INF\classes下新建FirstServlet.java内容如下:
package flying607;
import java.io.*;
import javax.servlet.*;
public class FirstServlet extends GenericServlet{
public void service(ServletRequest req, ServletResponse res) throws ServletException,IOException{
OutputStream out = res.getOutputStream();
out.write("Hello,Servlet!Good-Bye!".getBytes());
}
}
将需要的包(F:\Tomcat 7.0\lib\servlet-api.jar)set classpath了,然后javac -d . FirstServlet了。
在F:\Tomcat 7.0\webapps\helloServlet\WEB-INF下新建web.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<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_3_0.xsd"
version="3.0">
<servlet-mapping>
<servlet-name>flying607</servlet-name>
<url-pattern>/flying</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>flying607</servlet-name>
<servlet-class>
flying607.FirstServlet
</servlet-class>
</servlet>
</web-app>
访问helloServlet/flying即可。
servlet Servlet例子的更多相关文章
- Servlet+JSP例子
前面两节已经学习了什么是Servlet,Servlet接口函数是哪些.怎么运行.Servlet生命周期是什么? 以及Servlet中的模式匹配URL,web.xml配置和HttpServlet.怎么 ...
- servlet简单例子1
servlet简单例子1 分类: servlet jsp xml2012-04-18 21:54 3646人阅读 评论(3) 收藏 举报 servletloginjspaction浏览器 LoginS ...
- org.apache.cxf.transport.servlet.CXFServlet cannot be cast to javax.servlet.Servlet
java.lang.ClassCastException: org.apache.cxf.transport.servlet.CXFServlet cannot be cast to javax.se ...
- sae Servlet class XXXX is not a javax.servlet.Servlet
以前都是使用myeclipse开发web工程上传sae后没有问题,但是使用javaee导出war包上传sae 无法访问 Servlet class XXXX is not a javax.servl ...
- cannot be cast to javax.servlet.Servlet
在第一次开发Maven项目时,maven环境和仓库以及eclipse都和讲师讲解的一样,可是却遇到下面这个问题: java.lang.ClassCastException: servlet.UserS ...
- java.lang.ClassCastException: cn.itcase.serviceImpl.servicestudentImpl cannot be cast to javax.servlet.Servlet
java.lang.ClassCastException: cn.itcase.serviceImpl.servicestudentImpl cannot be cast to javax.servl ...
- 异常:Servlet class X is not a javax.servlet.Servlet
使用Maven命令 mvn archetype:create 创建了一个简单的web项目.导入Eclipse运行时,报这样的异常信息: Servlet class X is not a javax.s ...
- Servlet Servlet是Java平台上的CGI技术
Servlet Servlet是Java平台上的CGI技术.Servlet在服务器端运行,动态地生成Web页面.与传统的CGI和许多其它类似CGI的技术相比,Java Servlet具有更高的效率并更 ...
- Servlet - Servlet相关
1. 概念 Servlet是指任何实现了Servlet接口的类, Servlet运行于支持Java的应用服务器中, Servlet可以响应任何类型的请求, 但大多数情况下, Servlet只用来扩展基 ...
- "xxx cannot be cast to jakarta.servlet.Servlet "报错解决方式
在做jsp的上机时候同学出现了一个500错误:com.kailong.servlet.ComputeBill cannot be cast to jaka.servlet.Servlet 然后因为我用 ...
随机推荐
- C#的new操作符到底做了什么
使用new操作符来创建对象,其背后到底发生了什么? 有一个父类Animal,Dog派生于Animal. class Program { static void Main(string[] args) ...
- Python中函数的参数传递与可变长参数
转自旭东的博客原文 Python中函数的参数传递与可变长参数 Python中传递参数有以下几种类型: (1)像C++一样的默认缺省函数 (2)根据参数名传参数 (3)可变长度参数 示例如下: (1)默 ...
- 单独配置secondarynamenode
一.配置说明 这是在我之前yarn框架上通过加入节点,改动相关的配置文件,使得secondarynamenode独立出来的,所以这里前期的一系列琐碎配置请參考我之前的博客: http://blog.c ...
- Eclipse默认快捷键指南
Eclipse 是一个开放源代码的.基于Java的可扩展开发平台,包含一个框架和一组服务,用于通过插件组件构建开发环境,附带了一个标准的插件集(包括Java开发工具Java Development K ...
- JAVA常见算法题(三十五)
判断一个整数能被几个9整除. public static void main(String[] args) { f(729); f(730); } public static void f(int n ...
- Eclipse中运行Tomcat遇到的内存溢出错误
使用Eclipse(版本Indigo 3.7)调试Java项目的时候,遇到了下面的错误: Exception in thread "main" Java.lang.OutOfMem ...
- android:Activity四种启动模式简单介绍
Activity启动模式 能够依据实际的需求为Activity设置相应的启动模式,从而能够避免创建大量反复的Activity等问题 Activity有四种载入模式 1.standard(默认启动模式, ...
- C/C++中内联(inline)函数的优点和缺点
优点 缺点 1. 内联函数代码被放入符号表中,在使用时进行替换,和宏展开一样,效率很高: 1. 不允许过多的代码,代码过多的话会造成大的内存消耗,最好在5行以内: 2. 编绎器在调用一个内联函数,首先 ...
- Linux C Socket编程原理及简单实例
部分转自:http://goodcandle.cnblogs.com/archive/2005/12/10/294652.aspx 1. 什么是TCP/IP.UDP? 2. Socket在哪里 ...
- Insert Interval leetcode java
题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...