HttpServlet的原理

 HttpServlet抽象类中的(部分)方法

 HttpServlet extends GenericServlet{
void service(ServletRequest request,ServletResponse responce){
*强转两个参数为http协议相关的类型。
*调用本类的servic(HttpServletRequest, HttpServletResponse)方法
} void service(HttpServletRequest,HttpServletResponse)-->参数已经是Http协议相关的,使用起来就更加方便。
*它会通过request得到当前请求的请求方式,例如:GET或POST
*根据请求方式在调用doGet()或doPost()方法 void doGet(){...} -->重写
void doPost(){...} -->重写 }

原理图:

源码:

MyHttpServlet.java

 package one.servlet;

 import java.io.IOException;

 import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class MyHttpServlet extends HttpServlet {
//**公有的请求方法
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//我们只需重写该方法即可
System.out.println("doPost()...");
}
}

如果在浏览器的地址栏中输入http://localhost:8080/XJS_Servlet1/MyHttpServlet 然后请求,会出现下图:

解决方法:在WebRoot文件下创建一个login.html

 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 路径要求:“/”开头+/项目名+/servlet路径(web.xml中url-pattern) -->
<form action="/XJS_Servlet1/MyHttpServlet" method="post">
<input type="submit" value="提交"/>
</form>
</body>
</html>

然后中浏览器地址栏中输入http://localhost:8080/XJS_Servlet1/login.html  再点击提交按钮,以post的方式请求服务器

请求一次,执行一次doPost()方法,结果如下:

doPost()...
doPost()...

自己直接创建一个servlet:直接继承了HttpServlet,还重写doGet()和doPost()方法

然后Next

然后就会自动为我们再web.xml文件中配置该FServlet的路径

HttpServlet的更多相关文章

  1. The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path解决方案

    0.环境: win7系统,Tomcat9配置无误. 1.错误: 项目中某一.jps页面忽然出现错误,鼠标点上去为:The superclass "javax.servlet.http.Htt ...

  2. The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path。问题

    JSP页面顶端出现“红色”的报错信息:The superclass "javax.servlet.http.HttpServlet" was not found on the Ja ...

  3. 错误:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path

    我们在利用Eclipse进行Java web开发时,可能会出现这样的错误:The superclass javax.servlet.http.HttpServlet was not found on ...

  4. eclipse中 报出The type javax.servlet.http.HttpServlet cannot be resolved. It is indirect错误

    在Myeclispe部署项目后 报错 The type javax.servlet.http.HttpServlet cannot be resolved. It is indirect错误 如果在M ...

  5. Servlet简介与Servlet和HttpServlet运行的流程

    1.Servlet      [1] Servlet简介         > Server + let         > 意为:运行在服务器端的小程序.         > Ser ...

  6. Servlet/JSP-03 HttpServlet

    一. GenericServlet GenericServlet本身是一个抽象类,并且实现了Servlet和ServletConfig接口 其在内部定义了一个私有的ServletConfig类型的变量 ...

  7. ubuntu下eclipse遇到The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path

    JSP页面顶端出现“红色”的报错信息:The superclass "javax.servlet.http.HttpServlet" was not found on the Ja ...

  8. servlet、genericservlet、httpservlet之间的区别

    转自:http://blog.csdn.net/rat9912345/article/details/5161789 当编写一个servlet时,必须直接或间接实现servlet接口,最可能实现的方法 ...

  9. HttpServlet 详解(注!仿)

    Servlet的框架是由两个Java包组成:javax.servlet和javax.servlet.http. 在javax.servlet包中定义了所有的Servlet类都必须实现或扩展的的通用接口 ...

  10. Java Servlet(六):HttpServlet实现原理(jdk7+tomcat7+eclipse)

    本篇记录了HttpServlet的实现过程,主要讲述了如何依赖Servlet,GenericServlet实现的原理. HttpServlet实现过程:1.是一个Servlet,继承自GenericS ...

随机推荐

  1. spring cloud 微服务调用--ribbon和feign调用

    这里介绍ribbon和feign调用两种通信服务调用方式,同时介绍如何引入第三方服务调用.案例包括了ribbon负载均衡和hystrix熔断--服务降级的处理,以及feign声明式服务调用.例子包括s ...

  2. WebService - [Debug] undefined element declaration 's:schema'

    错误: [ERROR] undefined element declaration 's:schema' line 44 of http://www.webxml.com.cn/WebServices ...

  3. 关于 min_25 筛的入门以及复杂度证明

    min_25 筛是由 min_25 大佬使用后普遍推广的一种新型算法,这个算法能在 \(O({n^{3\over 4}\over log~ n})\) 的复杂度内解决所有的积性函数前缀和求解问题(个人 ...

  4. windows下安装setuptools与pip

    1.下载 setuptools与pip 下载地址如下: https://pypi.Python.org/pypi/setuptools https://pypi.Python.org/pypi/pip ...

  5. Python-Django-Ajax

    什么是Ajax: 通过js语言跟后台进行交互的一个东西 -特点:异步,局部刷新 ajax往后台提交数据 $.ajax({ url:'请求的地址', type:'get/post', data:{key ...

  6. JS高级

    一.函数高级 1.函数回调 函数回调的本质:在一个函数中,满足特定条件下,调用另一个函数 // 回调的函数 function callback(data) {} // 逻辑函数 function fu ...

  7. java的官网下载(如有不懂,可以去我发的视频网站,那里面有详细过程)

    https://www.oracle.com/technetwork/java/javase/downloads/java-archive-javase9-3934878.html java 9的下载 ...

  8. TCP报文首段格式

    参考:https://www.bilibili.com/video/av9876107?from=search&seid=5217429010533979778 TCP(Transmissio ...

  9. Redis 可视化工具:Web Redis Manager

    以前用的:RedisDesktopManager (0.9.3以后需要订阅) https://github.com/uglide/RedisDesktopManager/releases 现在需要订阅 ...

  10. java实验-3

    实验内容 1.XP基础 2.XP核心实践 3.相关工具 实验内容 一.在IDEA中使用工具(Code->Reformate Code)把下面代码重新格式化,再研究一下Code菜单,找出一项让自己 ...