Servlet 之 HttpServlet
package cn.jiemoxiaodi.http; import java.io.IOException; import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class MyHttpServlet extends GenericServlet { @Override
public void service(ServletRequest arg0, ServletResponse arg1)
throws ServletException, IOException {
// 如果我们确定请求的是用http协议,那么实际上ServletRequest 就是
// HttpServletRequest 所以进行强转 HttpServletRequest request = (HttpServletRequest) arg0; HttpServletResponse response = (HttpServletResponse) arg1; //当我们请求servlet的时候 ,如果他请求方式是get 那么他走一个doGet方法
//当我们请求servlet时候,如果他请求方式是post 那么他走一个doPost的方法 //获得 请求的 方式
String method= request.getMethod();
if(method.equalsIgnoreCase("get")){
doGet(request,response);
}else if(method.equalsIgnoreCase("post")){
doPost(request,response);
} } public void doPost(HttpServletRequest request, HttpServletResponse response) {
System.out.println("dopost");
} public void doGet(HttpServletRequest request, HttpServletResponse response) {
System.out.println("doget");
} }
package cn.jiemoxiaodi.http; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class DServlet extends MyHttpServlet { @Override
public void doPost(HttpServletRequest request, HttpServletResponse response) {
System.out.println("dopost----=--");
} @Override
public void doGet(HttpServletRequest request, HttpServletResponse response) {
System.out.println("doget----=--");
} }
1.关于Servlet的线程安全问题
因为一个servlet的实例在服务器中只存在一份. ==> 当有多个请求访问时,servlet线程将不安全.
如何规避线程安全问题呢? ===> 我们接受线程的信息时,将信息保存在方法的变量中,不要使用成员变量保存.
2.关于servlet中路径配置问题
<!--
路径配置分为两种情况
路径匹配
/DServlet ==> http://localhost:8080/Day10/DServlet
/ABC/DServlet ==> http://localhost:8080/Day10/ABC/DServlet
/ABC/BCD/DServlet ==> http://localhost:8080/Day10/ABC/BCD/DServlet
/ABC/* ==> http://localhost:8080/Day10/ABC/asdlkajsdlkjalsd
/ ==> 匹配所有路径 http://localhost:8080/Day10/2139009123j/asd2klnasd
匹配优先级: 路径匹配中 ,匹配的范围越广,优先级越低.
后缀名匹配
*.do ==> http://localhost:8080/Day10/213oiajsdoijoad.do
*.action ==> http://localhost:8080/Day10/lnasdljasdlkasljdasd.action
*.abc ==> http://localhost:8080/Day10/1039i09ixc0kasd.abc
这种路径匹配在现在学习阶段接触不到. 在学到Filter 和struts2 的时候就用到了.
!!!!!!!注意: 以上两种匹配方式不能同时使用
例如下面是绝对错误的:
/DServlet/*.do ==> 凡事路径配置中以"/"开头那么就说明含有路径匹配.这种时候再使用*.do 这种后缀名匹配绝对报错.
-->
3.(拓展)关于tomcat中web.xml的信息
1> defaultServlet 配置的路径是 "/" ==> 所有跟我的路径匹配不上的路径都会走default.default主要干两件事 1.找静态资源 2.找不到静态资源报错.2>session相关的配置 ==> 30 ==> 学session的时候就知道了
3>将所有MIME类型 都列举出来了.
4>welcome list file ==> 默认的欢迎资源.
4.我们知道servlet默认创建实例的时机是什么时候? ==> 第一次请求该servlet时创建. ==> 如果我们这个servlet创建需要的时间比较久.我们想让servlet随着服务器的启动而启动
那么我们可以这样做:
<servlet>
<servlet-name>AServlet</servlet-name>
<servlet-class>cn.itcast.servlet.AServlet</servlet-class>
<!-- 在启动服务器时加载的意思
配置了该配置,那么这个servlet就会在服务器启动时创建实例.
这个配置中填写的内容是整数 最小可以填0.
数字越小 启动优先级越高. 所以0 表示优先级最高.
如果两个数字大小相同,优先级按照配置顺序,谁先配置 谁先创建.
-->
<load-on-startup></load-on-startup>
</servlet>
Servlet 之 HttpServlet的更多相关文章
- 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 ...
- 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 ...
- 错误: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 ...
- 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 ...
- Servlet简介与Servlet和HttpServlet运行的流程
1.Servlet [1] Servlet简介 > Server + let > 意为:运行在服务器端的小程序. > Ser ...
- 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 ...
- 项目忽然出现 The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path 解决方法
0.环境: 新装win8.1系统,Tomcat配置无误. 1.错误: 项目中某一.jps页面忽然出现错误,鼠标点上去为:The superclass "javax.servlet.http. ...
- JavaWeb:报错信息The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
建立了一个Javaweb工程,并在eclipse中配置了Web容器Tomcat.新建的jsp页面,添加一个简单的Java类.可是,JSP页面顶端出现“红色”的报错信息:The superclass & ...
- eclipse中web工程新建jsp文件报错:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
web工程中新建jsp文件提示:The superclass "javax.servlet.http.HttpServlet" was not found on the Java ...
- java开发eclipse常见问题(一)The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
最近刚开始用Eclipse开发,刚开始都是按教程一步一步的新建web工程也没出现什么问题. 今天选了一个新的workspace,建了个web工程发现最简单的jsp页面都报错:The superclas ...
随机推荐
- Python-07-面向对象(进阶篇)
1.面向对象高级语法部分 1.1 静态方法 通过 @staticmethod 装饰器即可把其装饰的方法变为一个静态方法,什么是静态方法呢?其实不难理解,普通的方法,可以在实例化后直接调用,并且在方法里 ...
- IO(三)----序列流
SequenceInputStream 表示其他输入流的逻辑串联.它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取,依次类推,直到到达包含的最后一个输入流的 ...
- 回文自动机(BZOJ2565)
#include <cstdio> #include <cstring> #include <iostream> using namespace std; ][], ...
- ABP之依赖注入
写在开头 ABP开源项目最近有点小火,还开展了线下活动.本着学习DDD的心态与学习开源代码的的好奇,我也看了一遍ABP源码,在此将自己学习ABP的一些心得记录下来. 作为核心的IoC 作为一种解耦的方 ...
- Oracle之分页查询
select * from ( select a.*, rownum rn from (select * from table_name) a where rownum<= 40 ) where ...
- [转]数据恢复 文件恢复工具 DiskGenius v4.9.1 绿色专业版及单文件
必备神软!数据恢复及磁盘分区利器DiskGenius,目前最新版为v4.9.1,现在又有新思路的已注册专业版,已亲测可成功恢复4G以上的大文件,但不能虚拟磁盘格式转换!想用新版功能的有福了,推荐使用! ...
- AOJ DSL_2_C Range Search (kD Tree)
Range Search (kD Tree) The range search problem consists of a set of attributed records S to determi ...
- Android Studio中JNI程序的单步调试和日志打印
近日有个算法(检测碰撞)需要用C++实现,目的是IOS和ANDROID中共享同一段程序. 下面说说android调用这段程序过程中遇到的一些事情.(过程中网上搜索了一些相关文章,大部分说的是eclip ...
- The RAII Programming Idiom
https://www.hackcraft.net/raii/ https://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization
- UnknownHandler
未知处理器 从struts2.1 开始 ,struts2配置文件的DTD中增加了<unknown-handler-stack…/>和<unknown-handler-ref…/> ...