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. TCP/IP(五)传输层之细说TCP的三次握手和四次挥手

    前言 这一篇我将介绍的是大家面试经常被会问到的,三次握手四次挥手的过程.以前我听到这个是什么意思呀?听的我一脸蒙逼,但是学习之后就原来就那么回事! 一.运输层概述 1.1.运输层简介 这一层的功能也挺 ...

  2. jdk1.8新特性 lambda表达式和Stream

    一.Lambda 1.lambda : 匿名函数 2.好处:减少打码的冗余,增强匿名函数的可读性 3.语法格式 语法格式一 : 无参数,无返回值 () -> System.out.println ...

  3. POJ 1556 The Doors(线段相交+最短路)

    题目: Description You are to find the length of the shortest path through a chamber containing obstruc ...

  4. html(jQuery)替换字符串(全部替换)

    var  str= "a<br/>b<br/>c<br/>"; var Newstr = str.replace("<br/&g ...

  5. Lua中的环境概念

    [前言] Lua将其所有的全局变量保存在一个常规的table中,这个table称为“环境”.这种组织结构的优点在于,其一,不需要再为全局变量创造一种新的数据结构,因此简化了Lua的内部实现:另一个优点 ...

  6. 【原创】大叔经验分享(27)linux服务器升级glibc故障恢复

    redhat6系统默认安装的glibc-2.12,有的软件依赖的是glibc-2.14,这时需要升级glibc,下载安装 http://ftp.gnu.org/gnu/glibc/glibc-2.14 ...

  7. lua table表判断是否为空

    官方手册里早已经给了答案,那就是靠lua内置的next函数 即如此用: a = {} if next(a) == nil then next其实就是pairs遍历table时用来取下一个内容的函数. ...

  8. C# 微信开发-----微信会员卡(二)

    主要说说如何使用微信的激活会员卡 如图: 点击激活会员卡时,要跳转到如下的图片: 要实现这个功能,首先我们在创建会员卡后就操作如下代码 #region 添加激活时的自定义字段 string custo ...

  9. HDU 5977 Garden of Eden

    题解: 路径统计比较容易想到点分治和dp dp的话是f[i][j]表示以i为根,取了i,颜色数状态为j的方案数 但是转移这里如果暴力转移就是$(2^k)^2$了 于是用FWT优化集合或 另外http: ...

  10. SQL反模式学习笔记7 多态关联

    目标:引用多个父表 反模式:使用多用途外键.这种设计也叫做多态关联,或者杂乱关联. 多态关联和EAV有着相似的特征:元数据对象的名字是存储在字符串中的. 在多态关联中,父表的名字是存储在Issue_T ...