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. 考虑实现一个不抛异常的swap

    Effective C++:参考自harttle land 类的swap实现与STL容器是一致的:提供swap成员函数, 并特化std::swap来调用那个成员函数. class Widget { p ...

  2. opencv图片坐标和数组坐标

    图片坐标和数组坐标是相反的,坐标原点位于左上角 import numpy as np import cv2 height, width = 150, 200 img = np.zeros((heigh ...

  3. 重排DL

    题解: https://www.luogu.org/problemnew/show/T51442 从这题上还是学到不少东西.. 以前并没有写过ex-bsgs 正好拿这个复习中国剩余定理和bsgs了(我 ...

  4. Hadoop Avro支持多输入AvroMultipleInputs

    Avro 提供了1.x版本的AvroMultipleInputs,但是不支持2.x API版本,因此修改对应代码,增加对hadoop 2.x API版本的的支持 代码放在https://github. ...

  5. 通俗理解webService及.net中的使用方法

    什么是WebService? WebService两个关键字:HTTP.接口 WebService就是一个接口,与普通接口的区别就是:普通接口只能本地调用:WebService可以远程调用. WebS ...

  6. NOIP2016提高组Day1T2 天天爱跑步 树链剖分 LCA 倍增 差分

    原文链接https://www.cnblogs.com/zhouzhendong/p/9275606.html 题目传送门 - 洛谷P1600 题目传送门 - LOJ#2359 题目传送门 - Vij ...

  7. VS2013 FFmpeg开发环境配置

    1.下载ffmpeg包(dll.include.lib)   https://ffmpeg.zeranoe.com/builds/         有3个版本:Static.Shared和Dev St ...

  8. oracle中查询条件包含null时

    不能使用=null或者!=null 应该使用is null和is not null

  9. Scala-Unit6-final/type关键字、样例类&样例对象

    一.关键字 1.final关键字 用final修饰的类:不能被继承 用final修饰的方法:不能被重写 注意:(1)在Scala中变量不需要用final修饰,因为val与var已经限制了变量是否可变 ...

  10. python基础篇_002_基础数据类型

    Python基础数据类型 1.int # int 用于计算 num = 3 # int 与其他数据类型转换 int_to_str = str(num) # 数字加引号 print(int_to_str ...