Servlet(9)—HttpServlet和改进Servlet实例

2024-05-02 19:38

本文主要是介绍Servlet(9)—HttpServlet和改进Servlet实例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  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 {@Overridepublic 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 {@Overridepublic 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);}}@Overridepublic 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实例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/955018

相关文章

MySQL多实例管理如何在一台主机上运行多个mysql

《MySQL多实例管理如何在一台主机上运行多个mysql》文章详解了在Linux主机上通过二进制方式安装MySQL多实例的步骤,涵盖端口配置、数据目录准备、初始化与启动流程,以及排错方法,适用于构建读... 目录一、什么是mysql多实例二、二进制方式安装MySQL1.获取二进制代码包2.安装基础依赖3.清

SpringBoot 异常处理/自定义格式校验的问题实例详解

《SpringBoot异常处理/自定义格式校验的问题实例详解》文章探讨SpringBoot中自定义注解校验问题,区分参数级与类级约束触发的异常类型,建议通过@RestControllerAdvice... 目录1. 问题简要描述2. 异常触发1) 参数级别约束2) 类级别约束3. 异常处理1) 字段级别约束

Apache Ignite缓存基本操作实例详解

《ApacheIgnite缓存基本操作实例详解》文章介绍了ApacheIgnite中IgniteCache的基本操作,涵盖缓存获取、动态创建、销毁、原子及条件更新、异步执行,强调线程池注意事项,避免... 目录一、获取缓存实例(Getting an Instance of a Cache)示例代码:二、动态

JSONArray在Java中的应用操作实例

《JSONArray在Java中的应用操作实例》JSONArray是org.json库用于处理JSON数组的类,可将Java对象(Map/List)转换为JSON格式,提供增删改查等操作,适用于前后端... 目录1. jsONArray定义与功能1.1 JSONArray概念阐释1.1.1 什么是JSONA

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串

java向微信服务号发送消息的完整步骤实例

《java向微信服务号发送消息的完整步骤实例》:本文主要介绍java向微信服务号发送消息的相关资料,包括申请测试号获取appID/appsecret、关注公众号获取openID、配置消息模板及代码... 目录步骤1. 申请测试系统2. 公众号账号信息3. 关注测试号二维码4. 消息模板接口5. Java测试

MySQL数据库的内嵌函数和联合查询实例代码

《MySQL数据库的内嵌函数和联合查询实例代码》联合查询是一种将多个查询结果组合在一起的方法,通常使用UNION、UNIONALL、INTERSECT和EXCEPT关键字,下面:本文主要介绍MyS... 目录一.数据库的内嵌函数1.1聚合函数COUNT([DISTINCT] expr)SUM([DISTIN

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析

《Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析》InstantiationAwareBeanPostProcessor是Spring... 目录一、什么是InstantiationAwareBeanPostProcessor?二、核心方法解

java String.join()方法实例详解

《javaString.join()方法实例详解》String.join()是Java提供的一个实用方法,用于将多个字符串按照指定的分隔符连接成一个字符串,这一方法是Java8中引入的,极大地简化了... 目录bVARxMJava String.join() 方法详解1. 方法定义2. 基本用法2.1 拼接