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. IDEA 中使用MyBatis-generator 自动生成MyBatis代码

    0.在Intellij IDEA创建maven项目 1. 在maven项目的pom.xml 添加mybatis-generator-maven-plugin 插件 <build> < ...

  2. Web从入门到放弃<2>

    <添加debug-toolbar> django现在1.11是必须这么做: pip install django-debug-toolbar 设置1: INSTALLED_APPS = [ ...

  3. python的一些基本概念

    1.为什么python被称为胶水语言?他是新一代的系统脚本参考博客:https://www.cnblogs.com/ningskyer/articles/5264172.html 2.python百度 ...

  4. 关于PHP上传文件时配置 php.ini 中的 upload_tmp_dir

    在<PHP 5.3 入门经典>9.6.3 的试一试中(P235),给出了一个上传文件的例子,这里的文件格式为jpeg图片(image/jpeg).如果之前未配置 php.ini 中的 up ...

  5. Linux extmail的邮件服务器搭建

    注:本文来源于<extmail搭建> 一.背景介绍 ExtMail Solution 是一个基于优秀开源软件的电子邮件系统解决方案,核心部件包括了Postfix.Amavisd-new.C ...

  6. js编译原理(你不知道的javascript)

    虽然通常将js归类为"动态"或"解释执行"语言,但其实也可把它看成是一门编译语言.只不过这个所谓的编译与传统的编译语言不同,它不是提前编译的,编译结果也不能在分 ...

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

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

  8. 开启Golang编程第一章

    Go is an open source programming language that makes it easy to build simple,reliable, and effcient ...

  9. BsonJavaScript

    BsonJavaScript主要应用于mongodb驱动中 1.进行数据分组 MongoClient client = new MongoClient(host.ConnectionString); ...

  10. 模块度Q

    模块度:也称模块化度量值,是目前常用的一种衡量网络社区结构强度的方法. 常用语衡量一个社区划分结果的优劣:一个理想化的社区划分应该对应着社区内部节点间相似度尽可能的高,同时社区外部节点间的相异度尽可能 ...