Servlet学习笔记(一)
来源:互联网 发布:构建自己的linux系统 编辑:程序博客网 时间:2024/05/17 01:49
/***@ author StormMaybin*@ date 2016-10-22*/
生命不息,奋斗不止!
Servlet简介
Servlet是sun公司提供的一门用于开发动态web资源的技术。
Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发一个Java程序向浏览器输出数据),需要完成以下2个步骤:
1、编写一个Java类,实现servlet接口。
2、把开发好的Java类部署到web服务器中。
Servlet运行过程以及生命周期
Servlet程序是由WEB服务器调用,web服务器收到客户端的Servlet访问请求后:
①Web服务器首先检查是否已经装载并创建了该Servlet的实例对象。如果是,则直接执行第④步,否则,执行第②步。
②装载并创建该Servlet的一个实例对象。
③调用Servlet实例对象的init()方法。
④创建一个用于封装HTTP请求消息的HttpServletRequest对象和一个代表HTTP响应消息的HttpServletResponse对象,然后调用Servlet的service()方法并将请求和响应对象作为参数传递进去。
⑤WEB应用程序被停止或重新启动之前,Servlet引擎将卸载Servlet,并在卸载之前调用Servlet的destroy()方法。
Servlet调用图
图像来自方立钧老师
Servlet接口API
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.
第一个Servlet小例子
开发环境:myeclipse
新建一个web项目,然后在src目录下新建一个ServletDemo.java文件
import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class ServletDemo extends HttpServlet { /** * Constructor of the object. */ public ServletDemo() { super(); } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the GET method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the POST method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); }}
接下来,Servlet映射配置
<!--由于客户端是通过URL地址访问web服务器中的资源,所以Servlet程序若想被外界访问,必须把servlet程序映射到一个URL地址上,这个工作在web.xml文件中使用<servlet>元素和<servlet-mapping>元素完成。<servlet>元素用于注册Servlet,它包含有两个主要的子元素:<servlet-name>和<servlet-class>,分别用于设置Servlet的注册名称和Servlet的完整类名。一个<servlet-mapping>元素用于映射一个已注册的Servlet的一个对外访问路径,它包含有两个子元素:<servlet-name>和<url-pattern>,分别用于指定Servlet的注册名称和Servlet的对外访问路径。--> <servlet> <servlet-name>ServletDemo</servlet-name> <servlet-class>ServletDemo</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletDemo</servlet-name> <url-pattern>/servlet/ServletDemo</url-pattern> </servlet-mapping>
部署发布到服务器上,完成
实际开发中,不用自己配置Servlet映射,利用myeclipse新建Servlet,自动生成!
目录结构
- src:存放java代码
- webRoot:项目的根目录
- META-INTF:存放一些meta information相关的文件的这么一个文件夹, 一般来说尽量不要自己手工放置文件到这个文件夹。
MEB-INT:
- classes:用于存放java字节码文件
- lib:用于存放该工程用到的库,例如servlet-api.jar等等
- web.xml:web工程的配置文件,完成用户请求的逻辑名称到真正的servlet类的映射
- Servlet学习笔记(一)---Servlet生命周期
- Servlet学习笔记(一)
- Servlet 学习笔记(一)
- servlet学习笔记(一)
- Servlet学习笔记(一)
- servlet学习笔记(一)
- Servlet学习笔记(一)
- Servlet学习笔记(一)
- Servlet学习笔记(一)
- servlet学习笔记(一)
- Servlet学习笔记(一)
- servlet学习笔记(一)
- Servlet 学习笔记一
- Servlet学习笔记(一)
- Servlet学习笔记《一》
- Servlet学习笔记(一)
- Servlet学习笔记一
- Servlet 学习笔记一
- 堆栈的初认识
- OkHttp的简单使用
- 嵌入式初接触
- Google Progressive Web App简称PWA
- HTML_08_bootstrap-初次见面
- Servlet学习笔记(一)
- 封装Dom4j库
- 欢迎使用CSDN-markdown编辑器
- 使用Dom4j生成XML文件
- 对象拷贝copy与mutableCopy
- hduoj2036
- 用HTML的嵌套div的方法画一个田字格
- HDU5929 Basic Data Structure(双端队列)
- 第一个C语言程序