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 ...
随机推荐
- 十分钟轻松让你认识ASP.NET MVC6
这篇文章说明下如何在普通编辑器下面开发mvc6应用程序. 上篇文章: 十分钟轻松让你认识ASP.NET 5(MVC6) 首先安装mvc6的nuget包: 可以看到在project.json文件中添加了 ...
- React基础知识
学习文档(按优先级排列)http://reactjs.cn/react/docs/tutorial-zh-CN.htmlhttp://www.cnblogs.com/Mrs-cc/p/4969755. ...
- .Net 闭包理解
.Net 闭包理解 这个东西初看是比较难懂,但是一旦理解之后就很容易了,做笔记以加深印象.且看这题 example.1 class Program { static void Main(string[ ...
- 【跟着子迟品 underscore】JavaScript 数组展开以及重要的内部方法 flatten
Why underscore (觉得这一段眼熟的童鞋可以直接跳到正文了...) 最近开始看 underscore.js 源码,并将 underscore.js 源码解读 放在了我的 2016 计划中. ...
- VPN使用指南|稳定的VPN|
之前一直用GoagentFQ,但是有时不太稳定,网速也不是很快,然后就试用了云梯VPN,感觉还不错,网速挺快,也比较稳定.http://opticalvpn.com/?r=72ec52481ab8d2 ...
- 【C#】C#容易忽视的错误
1.string 拼接站内存,前提是字符串比较多的时候string 字符串类型拼接占内存,解决方法就是用 StringBuilder和String.Format2.不知道内置的验证数据类型的方法. ; ...
- jeecg小吐槽
1.online开发出来的表单,字段中设置的默认值,新建表单的时候不会出来,要保存后才会在后台补加进去!(为时已晚吧) 2.online开发出来的表单,主表中的附表,在online配置界面指定了可以为 ...
- 公司VPN信息
公司VPN证书信息(请妥善留档本邮件) 管理 管理员 <admin@pansoft.com> 2016/12/12 11:00 收件人: huhuan@pansoft.com × ...
- java 对象入门
对象的五大特征 (1)所有东西都是对象.可将对象想象成一种新型变量;它保存着数据,但可要求对自身进行操作.理论上讲, 可从要解决的问题身上提出所有概念性的组件,然后再程序中将其表达为一个对象. (2) ...
- 清除文件夹下的SVN信息
Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE/SOFTWARE/Classes/Folder/shell/清除SVN信息] @=&q ...