背景
上世纪90年代,随着Internet和浏览器的飞速发展,基于浏览器的B/S模式随之火爆发展起来。最初,用户使用浏览器向WEB服务器发送的请求都是请求静态的资源,比如html、css等。 但是可以想象:根据用户请求的不同动态的处理并返回资源是理所当然必须的要求。
servlet的定义
- Servlet is a technology which is used to create a web application. servlet是一项用来创建web application的技术。
- Servlet is an API that provides many interfaces and classes including documentation. servlet是一个提供很多接口和类api及其相关文档。
- Servlet is an interface that must be implemented for creating any Servlet.servlet是一个接口,创建任何servlet都要实现的接口。
- Servlet is a class that extends the capabilities of the servers and responds to the incoming requests. It can respond to any requests. servlet是一个实现了服务器各种能力的类,对请求做出响应。它可以对任何请求做出响应。
- Servlet is a web component that is deployed on the server to create a dynamic web page.servlet是一个web组件,部署到一个web server上(如tomcat,jetty),用来产生一个动态web页面。
servlet的历史
web Container
web容器也叫servlet容器,负责servlet的生命周期,映射url请求到相应的servlet。
A web container (also known as a servlet container;[1] and compare "webcontainer"[2]) is the component of a web server that interacts with Java servlets. A web container is responsible for managing the lifecycle of servlets, mapping a URL to a particular servlet and ensuring that the URL requester has the correct access-rights.A web container handles requests to servlets, JavaServer Pages (JSP) files, and other types of files that include server-side code. The Web container creates servlet instances, loads and unloads servlets, creates and manages request and response objects, and performs other servlet-management tasks.A web container implements the web component contract of the Java EE architecture. This architecture specifies a runtime environment for additional web components, including security, concurrency, lifecycle management, transaction, deployment, and other services.
常见的web容器如下:
在web容器中,web应用服务器的结构如下:
1.普通servlet实现页面访问
1.1 实例1:使用web.xml实现一个http服务
实现一个简单的servlet
- package com.howtodoinjava.servlets;
-
- 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 MyFirstServlet extends HttpServlet {
-
- private static final long serialVersionUID = -1915463532411657451L;
-
- @Override
- protected void doGet(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException
- {
- response.setContentType("text/html;charset=UTF-8");
- PrintWriter out = response.getWriter();
- try {
- // Write some content
- out.println("<html>");
- out.println("<head>");
- out.println("<title>MyFirstServlet</title>");
- out.println("</head>");
- out.println("<body>");
- out.println("<h2>Servlet MyFirstServlet at " + request.getContextPath() + "</h2>");
- out.println("</body>");
- out.println("</html>");
- } finally {
- out.close();
- }
- }
-
- @Override
- protected void doPost(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- //Do some other work
- }
-
- @Override
- public String getServletInfo() {
- return "MyFirstServlet";
- }
- }
web.xml配置servlet
/MyFirstServlet MyFirstServlet com.howtodoinjava.servlets.MyFirstServlet MyFirstServlet /MyFirstServlet
1.2 编程方式实现一个http服务请求
(编辑:ASP站长网)
|