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
init
method. - Any calls from clients to the
service
method are handled. - The servlet is taken out of service, then destroyed with the
destroy
method, 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 然后因为我用 ...
随机推荐
- Unity中关于Device Filter的选择问题
引言 目前工作的Unity版本是5.4.1f,发布Android版本.apk的时候,对包体的大小有些疑问,就上网查了下资料,发现Build Settings——Player Settings——Oth ...
- 使用 kubeadm 搭建 kubernetes1.10 集群
PS:所有节点安装之前记得先把镜像准备好,否者将无法启动,也不报错. $ cat /etc/hosts192.168.11.1 master192.168.11.2 node 禁用防火墙: $ sys ...
- poj 1007 Quoit Design(分治)
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- AndEngine中文文档下载地址
AndEngine doc downloadhere 下载地址:http://pan.baidu.com/s/1bnjcL0V 文档是由github仓库AndEngine的代码生成. 本doc中包括 ...
- 使用DBCA工具创建自己的数据库
ylbtech-Oracle:使用DBCA工具创建自己的数据库 DBCA创建数据库 默认安装的Oracle数据库一般不能满足实际应用的需求,例如数据库名称.数据库块的大小等都需要修改,那么我们应该自 ...
- C#创建一个Windows Service
Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的.所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Window ...
- scala 学习笔记十三 特质(转载)
转载地址:https://blog.csdn.net/dwb1015/article/details/51761510 1,介绍 Scala和java一样不允许类从多个超类继承:从多个超类继承可能会导 ...
- ListView.MouseDoubleClick
private void ruleListView_MouseDoubleClick(object sender, MouseButtonEventArgs e) { ListViewItem ite ...
- TextBox_TextChanged
private void TextBox_TextChanged(object sender, TextChangedEventArgs e) { TextBox textBox = sender a ...
- Ubuntu16.04下Neo4j图数据库官网安装部署步骤(图文详解)(博主推荐)
不多说,直接上干货! 说在前面的话 首先,查看下你的操作系统的版本. root@zhouls-virtual-machine:~# cat /etc/issue Ubuntu LTS \n \l r ...