Servlet复习1: 一个简单的Servlet的使用
Servlet学习
1. Servlet与JSP的关系
2. Servlet的声明周期
3. 一个简单的Servlet的使用方法
什么是Servlet? 什么又是JSP?
继承了javax.servlet的一些类的程序 , 已经具有了Servlet的特征
但是Servlet的可视性不是太好, JSP可以弥补这一点 , jsp是Java Server Pagers , 是一种使用java作为脚本的语言, 在web服务器中可以转化为Servlet , 在传统的HTML网页中使用<% java语言 %> 就可以嵌入java代码了
Servlet是服务器端的小程序 , 可以用来通过多种方法扩充一个Web服务器.
PS:这句话告诉我们:
1. Servlet 是一个java写的程序
2. Servlet是运行在服务器端的 , 而不是Client
3. 写Servlet的最终目的是为了让服务器帮我们做事情
Servlet与JSP的关系:
相同点: JSP最终会转化为一个Servlet
不同点:
1 Servlet是持久的 , 仅需要Web服务器加载一次
2 Servlet是可扩展的 , 因为java是面向对象的
3 JSP是为了解决Servlet中的编程困难为开发的技术 , jsp是脚本语言 , Servlet是java语言
4 Servlet必须在编译之后才可以进行
5 第一次运行时 , JSP需要转化为Servlet然后在编译 , 才可以运行 . 而Servlet直接就编译了 .(所谓的第一次就是web容器中没有存在一模一样的文件)
6 servel写业务逻辑十分强大 . jsp写视图层效果很好
Servlet生命周期:
Servlet是javax.Servlet.httpServlet的子类.
1 装载Servlet
2 调用构造函数 实例化一个Serlet对象 , 这个对象的名字有<servelt-name/>指定
3 调用init()方法
4 服务Service()
5卸载destoory()
Demo:
目录结构:
Web.xml
----------------------------Test1.java----------------------------------------------------------
package com.controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Test1
*测试声明周期
*/
@WebServlet("/Test1")
public class Test1 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Test1() {
super();
System.out.println("----------------construction----------------");
// TODO Auto-generated constructor stub
}
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
System.out.println("---------init------------");
System.out.println(this.getClass());
}
/**
* @see Servlet#destroy()
*/
public void destroy() {
super.destroy();
System.out.println("----------------destroy-----------");
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
*/
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("------service---------------");
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
System.out.println("---------------默认调用doget()------------");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter out = response.getWriter();
out.println("-------------------书写内容-------------------------") ;
out.flush();
out.close();
}
}
index.jsp
效果图:
PS: 请注意地址栏的变化
这个时候发现没有调用doget()方法
原因是: 于是这样写了一下:
发现还是没有>>>>>>>>>>>>>>>这时你发现你写的这个类上的Service方法是这样写的:
对! 就是覆盖了了父类的方法了 ,如此一来,Servlet的Service()只会运行Syso了
为此应该改成:
这是这个类成了这样的:
package com.controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Test1
*测试声明周期
*/
@WebServlet("/Test1")
public class Test1 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Test1() {
super();
System.out.println("----------------construction----------------");
// TODO Auto-generated constructor stub
}
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
System.out.println("---------init------------");
System.out.println(this.getClass());
}
/**
* @see Servlet#destroy()
*/
public void destroy() {
super.destroy();
System.out.println("----------------destroy-----------");
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
*/
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
super.service(request, response);
System.out.println("------service---------------");
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
System.out.println("---------------doget()------------");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter out = response.getWriter();
out.println("-------------------content-------------------------") ;
out.flush();
out.close();
}
}
---------------------------------------------------
效果是:
在启动之后
显示
后台输出信息为:
点击连接之后:
控制台输出信息是
Servlet复习1: 一个简单的Servlet的使用的更多相关文章
- 在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 ...
- 一个简单的servlet的demo
javaweb 的应用我们需要参考javaee api 查找servlet接口 javax.servletInterface Servlet All Known Subinterfaces: Ht ...
- 一个简单的Servlet容器实现
上篇写了一个简单的Java web服务器实现,只能处理一些静态资源的请求,本篇文章实现的Servlet容器基于前面的服务器做了个小改造,增加了Servlet请求的处理. 程序执行步骤 创建一个Serv ...
- 通过Jetty搭建一个简单的Servlet运行环境
最近在做一些简单的Servlet开发的时候,感觉每次调试的时候都要发布到tomcat上很麻烦,把程序共享给同事也很麻烦,需要帮他设置本地的tomcat环境. 在网上找了找其他的Servlet运行环境, ...
- how tomcat works 札记(两)----------一个简单的servlet集装箱
app1 (看着眼前这章建议读者,看how tomcat works 札记(一个)----------一个简单的webserver http://blog.csdn.net/dlf123321/art ...
- how tomcat works 读书笔记(二)----------一个简单的servlet容器
app1 (建议读者在看本章之前,先看how tomcat works 读书笔记(一)----------一个简单的web服务器 http://blog.csdn.net/dlf123321/arti ...
随机推荐
- hdu4992 Primitive Roots(所有原根)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4992 题意:给出n,输出n的所有原根. 思路:求出n的一个原根x,那么对于所以的i,i<phi( ...
- GZFramwork快速开发框架演练之会员系统(二)添加字典模块
开始前请先阅读 GZFramwork快速开发框架之窗体设计说明 第一步:准备模块图片 图片为2张大小分别为16x16和32x32,放在\Debug\images目录下 因为会员管理模块并不多 ...
- GBrowse配置相关资料
GBrowse配置相关资料(形状.颜色.配置.gff3) http://gmod.org/wiki/Glyphs_and_Glyph_Optionshttp://gmod.org/wiki/GBrow ...
- XML详解:第二部分 XML Schema
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- [SAP ABAP开发技术总结]IDoc
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- 常用的STL查找算法
常用的STL查找算法 <effective STL>中有句忠告,尽量用算法替代手写循环:查找少不了循环遍历,在这里总结下常用的STL查找算法: 查找有三种,即点线面: 点就是查找目标为单个 ...
- IO设计模式:Reactor和Proactor对比
IO设计模式:Reactor和Proactor对比 平时接触的开源产品如Redis.ACE,事件模型都使用的Reactor模式:而同样做事件处理的Proactor,由于操作系统的原因,相关的开源产品也 ...
- Maven中<dependencies>节点和<dependencyManagement>节点的区别 转
以前一直没有在意,今天建立maven工程的时候在<dependencyManagement>节点下加入了junit依赖,结果在dependency Graph中没有发现junit的依赖关系 ...
- Android 使用finalBitmap实现缓存读取
public class NewsApplication extends Application{ private FinalBitmap finalBitmap=null; public Final ...
- iOS企业版打包(转载)
转自 http://www.cnblogs.com/shenlaiyaoshi/p/5472474.html 神来钥匙-陈诗友 iOS 企业版 打包 使用 iOS 企业版的证书发布应用可以跳过 A ...