在Java Web的世界里,Tomcat 等Web Server被称作servlet容器。这一称谓的由来就是因为,Tomcat 实际上运行JSP 页面和Servlet。servlet实际上包含了诸多的业务逻辑,后来经过分层思想的演化,业务逻辑会被独立成dao,service等层,但在servlet中还是会调用service层的逻辑。这时候servlet实际上是页面和逻辑层的桥梁,差不多是MVC中的控制器(C)的意思。

正是因为这种原因,structs2这个很好的MVC框架才诞生出来,这时候通过Action,ActionSupport,等接口屏蔽了servlet的doGet,doPost等方法。为什么需要屏蔽这些方法呢?我们分别看看doGet和doPost里面做了些什么。

public void doGet(ServletRequest rep, ServletResponse req)throws ServletException, IOException {System.out.println("doGet");req.setContentType("text/html");PrintWriter out = req.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();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {System.out.println("doPost");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();
}

是的,您没有看错,确实在拼html标签。动态的部分来自业务逻辑代码返回的,而静态部分需要在这里拼接字符串,效率很低,而且很容易出错。

下面我们来看看实现servlet的两种方式:实现Servlet接口和继承httpServlet

import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class MyServlet2 implements Servlet{public MyServlet2(){System.out.println("MyServlet2");}public void destroy() {// TODO Auto-generated method stubSystem.out.println("destroy");}public ServletConfig getServletConfig() {// TODO Auto-generated method stubSystem.out.println("ServletConfig");return null;}public String getServletInfo() {System.out.println("getServletInfo");// TODO Auto-generated method stubreturn null;}public void init(ServletConfig arg0) throws ServletException {// TODO Auto-generated method stubSystem.out.println("init");}public void service(ServletRequest rep, ServletResponse req)throws ServletException, IOException {// TODO Auto-generated method stubdoGet(rep,req);System.out.println("service");}public void doGet(ServletRequest rep, ServletResponse req)throws ServletException, IOException {System.out.println("doGet");req.setContentType("text/html");PrintWriter out = req.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();}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {System.out.println("doPost");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();}}
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 MyServlet extends HttpServlet {/*** Constructor of the object.*/public MyServlet() {super();System.out.println("constructor.");}/*** Destruction of the servlet. <br>*/public void destroy() {System.out.println("destroy");super.destroy(); // Just puts "destroy" string in log// Put your code here}/*** 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 {System.out.println("doGet");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 {System.out.println("doPost");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();}/*** Initialization of the servlet. <br>** @throws ServletException if an error occurs*/public void init() throws ServletException {// Put your code hereSystem.out.println("init");}}

这两个servlet算是最简单的两个servlet了,因为只包含doGet和doPost方法,实际开发过程中,肯定比这个复杂,但是大量地拼接字符串不是好的做法。

我们实际使用structs的Action,实现Action接口或者继承ActionSupport

package com.wicresoft.action;import com.opensymphony.xwork2.Action;public class IndexAction2 implements Action {public String execute() throws Exception {System.out.println("IndexAction2 executed.");// TODO Auto-generated method stubreturn SUCCESS;}
}
package com.wicresoft.action;import com.opensymphony.xwork2.ActionSupport;public class IndexAction3 extends ActionSupport{@Overridepublic String execute() throws Exception {System.out.println("IndexAction3 executed.");// TODO Auto-generated method stub//return super.execute();return "success";}public String UserAdd(){return SUCCESS;}public String Add(){return SUCCESS;}}

值得注意的是,这两种方式和实现servlet的两种方式遥相呼应。

Servlet生命周期分为三个阶段:
1,初始化阶段  调用init()方法
2,响应客户请求阶段  调用service()方法
3,终止阶段  调用destroy()方法

Servlet被装载后,Servlet容器创建一个Servlet实例并且调用Servlet的init()方法进行初始化。在Servlet的整个生命周期内,init()方法只被调用一次。

在servlet的实例被创建后,如果有请求过来,servlet容器会开新的线程来处理请求,而不再创建新的servlet的实例,也是因此,init()方法只会被调用一次!

Servlet - Java Web Core Component相关推荐

  1. Servlet (Java Web 学习笔记)

    Servlet 1.Servlet 技术 a)什么是 Servlet b)手动实现 Servlet 程序 c)url 地址到 Servlet 程序的访问 d)Servlet 的生命周期 e)GET 和 ...

  2. Java Web开发小结

    Web漏洞 Web常见的漏洞原理和攻击手法 JAVA Web开发基础知识 Java常见开发框架 JAVA WEB应用目录 JSP页面 Servlet Tomcat容器.JSP和Servlet Java ...

  3. java web知识总结——【华清远见】

    servlet Java web主要用的是sun公司制定的一种用于web服务器的功能的组件-servlet. 做Javaweb项目需要创建javaweb工程,和以往创建普通的Java项目不同,这个需要 ...

  4. Java Web 简明教程

    点此查看 所有教程.项目.源码导航 1. 前言 本教程用于介绍Java Web开发入门的方方面面,包括开发环境.工具.网页.Java.数据库等. 本教程写于2016年底,一些内容相对比较陈旧了,新版的 ...

  5. Java Web学习笔记 3 深入Servlet技术

    第3章 深入Servlet技术 请求-响应模式就是典型的Web应用程序访问过程,Java Web应用程序中,处理请求并发送响应的过程是由一种叫做Servlet的程序来完成的. 请求request,响应 ...

  6. 使用Java Servlet,JSP标签和Stormpath快速构建Java Web App

    建筑物身份管理,包括身份验证和授权? 尝试Stormpath! 我们的REST API和强大的Java SDK支持可以消除您的安全风险,并且可以在几分钟内实现. 注册 ,再也不会建立auth了! 我们 ...

  7. Java Web学生成绩管理系统(JSP+Servlet+JDBC+Dao)

    学完java web后,期末期间用所学知识写了一个简单的学生管理系统,现在有空整理分享下. 注意:本文章仅供参考和学习,源码和数据库设计在文章的底部,点击展开然后往下翻就可以找到,其实数据库就六张表, ...

  8. 初学 Java Web 开发,请远离各种框架,从 Servlet 开发

    写在前面: 本文是转自:http://www.oschina.net/question/12_52027  的文章,如果要求删除,第一时间联系我立即删除! Web框架是开发者在使用某种语言编写Web应 ...

  9. 初学Java Web开发,请远离各种框架,从Servlet开发

    [转载自红薯,原帖地址]http://www.oschina.net/question/12_52027 OSCHINA 软件库有一个分类--Web框架,该分类中包含多种编程语言的将近500个项目. ...

最新文章

  1. 函数调用的方法一共有 4 种,call,apply,bind
  2. 程序猿的英语之ielts indicator speaking test
  3. 深入理解Fabric环境搭建的详细过程
  4. 构建闭环式的研发运维体系----云效EDAS DevOps
  5. Spring-Security 自定义Filter完成验证码校验
  6. Canvas createImageData
  7. python选择法_新手小白如何学习Python 选对方法很重要(附教程)
  8. telnet直接登录POP3
  9. 如何解决手机电话本CSV格式和VCF格式的转换
  10. springboot 整合mybatis plus
  11. matlab实现证件照换底+美肤的功能
  12. SQL SERVER(32)Transact-SQL概述
  13. i5 12500H性能怎么样 相当于什么水平
  14. swt 做界面时部分要点
  15. 计算机专业水平不足,计算机专业教学存在的问题及完善对策
  16. windows系统DOS窗口
  17. Reac-18 portal传送门
  18. More Effective C++35个改善编程与设计的有效方法笔记
  19. 【process.popen】
  20. Centos /Linux环境下利用Docker 安装mysql5.7镜像(含离线安装),启动mysql镜像并初始化数据库

热门文章

  1. Android下拉列表怎么做?(小白速成7)
  2. Word 插入图片后不显示或显示不完整怎么办
  3. PPT配色的实用小技巧分享
  4. MySQL灵魂五十问
  5. css复选框表单,用CSS来美化表单——复选按钮篇
  6. JAVA通过auth_code获取支付宝账户信息
  7. 第二篇:Haploview做单倍型教程2--分析教程
  8. 每日一犬 · 波尔多犬
  9. 会议室预约系统 会议预约 会议预约触摸屏 会议预约管理系统
  10. CSS盒子模型学习-02