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. kerbose常用操作

    1.查看有那些用户认证 kadmin.local -q "list_principals" 2.用keytab文件进行认证 kinit -kt /root/keytab/hive. ...

  2. K-means clustering

    K-means算法是一种迭代算法,步骤如下: 1.随机初始化K个聚类中心u1,u2,...,uk 2.根据每个样本和各个聚类中心的距离给每个样本打上标签(例如,x(i)与u3的距离最小,则x(i)的标 ...

  3. redis 数据结构及应用场景

    1. String 常用命令: get.set.incr.decr.mget等 应用场景: String是最常用的数据类型,普通的key/value都可以归为此类,value其实不仅是String,也 ...

  4. Task.Run与Task.Factory.StartNew的区别

    Task是可能有延迟的工作单元,目的是生成一个结果值,或产生想要的效果.任务和线程的区别是:任务代表需要执行的作业,而线程代表做这个作业的工作者. 在.Net 4中,Task.Factory.Star ...

  5. jquery 第二章

    1.本章目标    css样式    选择器2.css样式    宽.高.边框.背景颜色.字体....... <html> <head> <style> div{ ...

  6. MySQL学习笔记:timediff、timestampdiff、datediff

    一.时间差函数:timestampdiff 语法:timestampdiff(interval, datetime1,datetime2) 结果:返回(时间2-时间1)的时间差,结果单位由interv ...

  7. Notes for "Python in a Nutshell"

    Introduction to Python Wrap C/C++ libraries into Python via Cython and CFFI. Python implementations ...

  8. matplotlib坐标轴设置续-【老鱼学matplotlib】

    本次会讲解如何修改坐标轴的位置. 要修改轴,就要先得到当前轴:plt.gca(),这个函数名挺怪的,其实是如下英文字母的首字母:get current axis,也就是得到当前的坐标轴. import ...

  9. UOJ#276. 【清华集训2016】汽水 二分答案 点分治

    原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ276.html 题解 首先,读入的时候就将所有的 $w_i$ 减掉 $k$ . 于是我们要求的就是平均值最 ...

  10. python面试题之什么是PEP8规范

    1 缩进.4个空格的缩进(编辑器都可以完成此功能),不使用Tap,更不能混合使用Tap和空格. 2 每行最大长度79,换行可以使用反斜杠,最好使用圆括号.换行点要在操作符的后边敲回车. 3 类和top ...