1. HttpServlet:针对Http协议定义的一个Servlet基类,唯一的功能就是强制类型转换ServletRequest转换成HttpServletRequest,ServletResponse转换成HttpServletResponse,HttpServlet继承自GenericServlet,而GenericServlet实现了Servlet接口和ServletConfig接口。也就是说编写Servlet类不需要直接实现Servlet类,去继承HttpServlet就行。
  2. 好处:
    • 不会产生多余无用的代码
    • 不用在强制类型转换了(为了获取请求的方式是get还是post需要用到需要用request.getMethod(),其中request必须是HttpServletRequest类型的,HttpServlet在service(ServletRequest req, ServletResponse res)已经强制类型转换好了)
    • 在service(HttpServletRequest req, HttpServletResponse resp)重载方法中获取请求方式,get请求会调用doget方法,post请求会执行dopost方法。所以我们可以再这2个方法中定义我们自己的业务逻辑

3 . Http中的方法:

HttpServlet()

doGet(HttpServletRequest, HttpServletResponse)

getLastModified(HttpServletRequest)

doHead(HttpServletRequest, HttpServletResponse)

doPost(HttpServletRequest, HttpServletResponse)

doPut(HttpServletRequest, HttpServletResponse)

doDelete(HttpServletRequest, HttpServletResponse)

getAllDeclaredMethods(Class< ? >)

doOptions(HttpServletRequest, HttpServletResponse)

doTrace(HttpServletRequest, HttpServletResponse)

service(HttpServletRequest, HttpServletResponse)

maybeSetLastModified(HttpServletResponse, long)

service(ServletRequest, ServletResponse)

改进后的实例:

HttpServlet

/**
* 针对Http协议定义的一个Servlet基类
*唯一的功能就是强制类型转换ServletRequest转换成HttpServletRequest,ServletResponse转换成
*HttpServletResponse
*/
public class MyHttpServlet extends MyGenericServlet { @Override
public void service(ServletRequest arg0, ServletResponse arg1)
throws ServletException, IOException {
if(arg0 instanceof HttpServletRequest){
HttpServletRequest httpServletRequest = (HttpServletRequest) arg0;
if(arg1 instanceof HttpServletResponse){
HttpServletResponse httpServletResponse = (HttpServletResponse) arg1;
service(httpServletRequest, httpServletResponse);
}
}
} public void service(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException {
//1.获取请求方式
String method = arg0.getMethod(); //2.根据请求的方式再调用对应的处理方法
if("GET".equalsIgnoreCase(method)){
doGet(arg0, arg1);
}else if("POST".equalsIgnoreCase(method)){
doPost(arg0, arg1);
}
} public void doPost(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException{ } public void doGet(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException{ }
}

Servlet:

public class LoginServlet3 extends MyHttpServlet {

    @Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取请求的方式
String method = request.getMethod();
System.out.println("请求方式:" + method); //1.获取请求参数username,password(获取的是表单信息)
String username = request.getParameter("username");
String password = request.getParameter("password"); //2.获取当前web应用的初始化参数user,password。
String initUser = getServletContext().getInitParameter("user");
String initPassword = getServletContext().getInitParameter("password"); PrintWriter out = response.getWriter();
//3.比对
if(initUser.equals(username) && initPassword.equals(password)){
//4.打印响应字符串
out.println("Hello: " + username);
}else{
out.println("Sorry: " + username);
}
} @Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取请求的方式
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
String method = httpServletRequest.getMethod();
System.out.println("请求方式:" + method); //1.获取请求参数username,password(获取的是表单信息)
String username = request.getParameter("username");
String password = request.getParameter("password"); //2.获取当前web应用的初始化参数user,password。
String initUser = getServletContext().getInitParameter("user");
String initPassword = getServletContext().getInitParameter("password"); PrintWriter out = response.getWriter();
//3.比对
if(initUser.equals(username) && initPassword.equals(password)){
//4.打印响应字符串
out.println("Hello: " + username);
}else{
out.println("Sorry: " + username);
}
}
}

页面:

<body>

    <form action="loginServlet" method="get">
user:<input type="text" name="user"/>
<br>
password:<input type="password" name="password"/>
<br>
interesting:
<input type="checkbox" name="interesting" value="read"/>read
<input type="checkbox" name="interesting" value="walk"/>walk
<input type="checkbox" name="interesting" value="swing"/>swing
<input type="checkbox" name="interesting" value="shopping"/>shopping
<input type="checkbox" name="interesting" value="tv"/>TV
<br>
<input type="submit" value="Submit"/>
</form>
</body>

Servlet(9)—HttpServlet和改进Servlet实例的更多相关文章

  1. Servlet(8)—GenericServlet和改进Servlet实例

    概念genericservlet是个抽象的父类,必须给出子类才能实例化它.它给出了设计servlet的一些骨架,定义了servlet生命周期,还有一些得到名字.配置.初始化参数的方法,其设计的是和应用 ...

  2. app开发历程---1,servlet 返回JSON作为android 接口实例

    最近公司领导要做app,虽然以前自己是做app的测试的,但是好多东西都不是很明白,这里记录自己这段日子的历程. 1.搭建服务器端,以前做测试的时候,他们用的是Apache+mysql+php,而自己上 ...

  3. 基于Servlet的MVC模式用户登录实例

    关于MVC模式的简单解释 M Model,模型层,例如登录实例中,用于处理登录操作的类: V View,视图层,用于展示以及与用户交互.使用html.js.css.jsp.jQuery等前端技术实现: ...

  4. 超全面的JavaWeb笔记day09<Servlet&GenericServlet&HttpServlet&ServletContext>

    1.Servlet概述 2.Servlet接口 3.GenericServlet 4.HttpServlet 5.Servlet细节 6.ServletContext(重要) Servlet概述 生命 ...

  5. 1.1(学习笔记)Servlet简介及一个简单的实例

    一.Servlet简介 Servlet是使用Java语言编写的服务器端程序,可以生产动态的Web界面. 主要运行在服务器端,Servlet可以方便的处理客户端传来的HTTP请求,并返回一个响应. 二. ...

  6. 最简单的Servlet继承HttpServlet查询数据库登录验证

    <%-- Created by IntelliJ IDEA. User: yunqing Date: 2017-12-06 Time: 9:11 To change this template ...

  7. Servlet基础(三) Servlet的多线程同步问题

    Servlet基础(三) Servlet的多线程同步问题 Servlet/JSP技术和ASP.PHP等相比,由于其多线程运行而具有很高的执行效率. 由于Servlet/JSP默认是以多线程模式执行的, ...

  8. JSP/SERVLET入门教程--Servlet 使用入门

    现在的JSP书籍有的是直接讲述JSP的使用,然后再讲解SERVERLET的使用;也有书籍是先讲述SERVERLET的使用,然后讲解JSP使用.个人认为第二种相对好一些,至于原因大家可以在学习体会到!所 ...

  9. Servlet 3特性:异步Servlet

    解异步Servlet之前,让我们试着理解为什么需要它.假设我们有一个Servlet需要很多的时间来处理,类似下面的内容: LongRunningServlet.java package com.jou ...

随机推荐

  1. 去除ArrayList集合中的重复自定义对象元素

    要求去除ArrayList集合中重复的Student的对象(什么叫重复,所有属性值都相同叫做重复). 思路: 1.创建一个新集合 2.遍历旧集合中的每一个元素,去新集合中找这个元素,如果这个元素不存在 ...

  2. 选择结构switch

    1.选择结构switch switch 条件语句也是一种很常用的选择语句,它和if条件语句不同,它只能针对某个表达式的值作出判断,从而决定程序执行哪一段代码.例如,在程序中使用数字1~7来表示星期一到 ...

  3. Introduction to boundary integral equations in BEM

    Boundary element method (BEM) is an effective tool compared to finite element method (FEM) for resol ...

  4. XXE (XML External Entity Injection) 外部实体注入漏洞案例分析

    ENTITY 实体 在一个甚至多个XML文档中频繁使用某一条数据,我们可以预先定义一个这条数据的“别名”,即一个ENTITY,然后在这些文档中需要该数据的地方调用它. XML定义了两种类型的ENTIT ...

  5. sentinel-dashboard安装、运行(ubuntu)

    下载页面https://github.com/alibaba/Sentinel/releases wget -P /opt/downloads https://github.com/alibaba/S ...

  6. 查出了a表,然后对a表进行自查询,a表的别名t1,t2如同两张表,因为t1,t2查询的条件不一样,真的如同两张表,关联两张表,可以将两行或者多行数据合并成一行,不必使用wm_concat()函数。为了将t2表的数据全部查出来使用了右连接。

    with a as( select nsr.zgswj_dm, count(distinct nsr.djxh) cnt, 1 z from hx_fp.fp_ly fp, hx_dj.dj_nsrx ...

  7. ContentProvider工作原理

    --摘自<android插件化开发指南> 1.系统管理类app,比如手机助手,有机会频繁使用ContentProvider 2.通讯录或者短信数据,是以ContentProvider的形式 ...

  8. HDU3339 In Action 【最短路】+【01背包】

    <题目链接> 题目大意: 给出一个0-n组成的图,1-n的点上分布着值为pow的电站,给出图的m条边以及距离,从0出发到n个点中的x个点的行走距离和最小(因为是每炸一个点派出一辆坦克),且 ...

  9. PostgreSQL 在Ubuntu下如何修改postgres默认密码

    Step1: 切换用户为postgres sudo su postgres Step2: 用postgres连接postgreSQL psql -U postgres Step3: 修改postgre ...

  10. UML图快速入门

    UML(Unified Modeling Language)统一建模语言的概念已经出现了近20年,虽然并不是所有的概念都非常有实践意义,但常见的用例图.类图.序列图和状态图却实实在在非常有效,是项目中 ...