Servlet规范
一、Servlet规范介绍:
-
servlet规范来自于JAVAEE规范中的一种
-
作用:
1)在Servlet规范中,指定动态资源文件开发步骤。2)在Servlet规范中,指定Http服务器调用动态资源文件规则。
3)在Servlet规范中,指定Http服务器管理动态资源文件实例对象规则。
二、Servlet接口实现类 :
1.Servlet接口来自于Servlet规范下一个接口,这个接口存在Http服务器
提供jar包(servlet-api.jar–>javax.servlet–>resources–>Servlet)
2.Tomcat服务器下lib文件有一个servlet-api.jar存放Servlet接口(javax.servlet.Servlet接口)
3.Servlet规范中任务,Http服务器能调用的【动态资源文件】必须是一个Servlet接口实现类。
三、Servlet接口实现类开发步骤:
第一步:创建一个Java类继承与HttpServlet父类,使之成为一个Servlet接口实现类。
第二步:重写HttpServlet父类两个方法。doGet或doPost。
public class OneServlet extends HttpServlet{
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("调用doGet()");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("调用doPost()");
}
}
解疑:实现Servlet开发,为什么要继承HttpServlet呢?
Servlet接口的源码:
package javax.servlet;
import java.io.IOException;
public interface Servlet {
void init(ServletConfig var1) throws ServletException;
ServletConfig getServletConfig();
String getServletInfo();
void destroy();
//实际应用的方法只有service(),上面4个的方法都用不上
void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;
}
由以上代码得知:Servlet的方法中用不上的方法太多,所以要需要抽象类来实现接口中的部分方法。
抽象类作用:降低接口实现类对实现过程的难度,将接口中不需要的抽象方法交给抽象类进行完成,这样接口实现类只需要对接口需要方法进行完成。
extends extends implements
oneServlet--------->(abstract)HttpServlet------->(abstract)GenericServlet--------->servlet接口
init、
destroy、
getServletInfo、
getServletConfig
GenericServlet抽象方法内此类完成了init、destroy、getServletInfo、getServletConfig方法。而HttpServle继承了GenericServlet,因此我们只需要面向HttpServlet编程。
由oneServlet的继承关系得知,oneServlet继承抽象类HttpServlet,HttpServlet继承了抽象类GenericServlet,GenericServlet实现了Servlet接口。
第三步:将Servlet接口实现类信息【注册】到Tomcat服务器
【网站】—>【web】—>【WEB-INF】—>web.xml
<!--将Servlet接口实现类类路径地址交给Tomcat-->
<servlet>
<servlet-name>OneServlet</servlet-name> <!--声明一个变量存储servlet接口实现类类路径-->
<servlet-class>com.abc.controller.OneServlet</servlet-class><!--声明servlet接口实现类类路径-->
</servlet>
<!--为了降低用户访问Servlet接口实现类难度,需要设置简短请求别名-->
<servlet-mapping>
<servlet-name>OneServlet</servlet-name>
<url-pattern>/one</url-pattern> <!--设置简短请求别名,别名在书写时必须以"/"为开头-->
</servlet-mapping>
浏览器向Tomcat索要OneServlet时地址为:
http://localhost:8080/myWeb/one
四、Servlet对象生命周期:
1.网站中所有的Servlet接口实现类的实例对象,只能由Http服务器负责创建,开发人员不能手动创建Servlet接口实现类的实例对象。
2.在默认的情况下,Http服务器接收到对于当前Servlet接口实现类第一次请求时自动创建这个Servlet接口实现类的实例对象,在手动配置情况下,要求Http服务器在启动时自动创建某个Servlet接口实现类的实例对象。
手动创建Servlet接口实现类在.xml文件的配置:
<servlet>
<servlet-name>mm</servlet-name> <!--声明一个变量存储servlet接口实现类类路径-->
<servlet-class>com.bjpowernode.controller.OneServlet</servlet-class>
<load-on-startup>30<load-on-startup><!--填写一个大于0的整数即可-->
</servlet>
3.在Http服务器运行期间,一个Servlet接口实现类只能被创建出一个实例对象
4.在Http服务器关闭时刻,自动将网站中所有的Servlet对象进行销毁
总结:正常情况下,Http服务器默认在第一个用户请求时创建Servlet对象,配置<load-on-startup>
后,则在服务器启动的时候就创建了Servlet对象。
五、HttpServletRequest接口
一、介绍:
1、HttpServletRequest接口来自于Servlet规范中,在Tomcat中存在servlet-api.jar
2、HttpServletRequest接口实现类由Http服务器负责提供
3、HttpServletRequest接口负责在doGet/doPost方法运行时读取Http请求协议包中信息
4、开发人员习惯于将HttpServletRequest接口修饰的对象称为【请求对象】
二、作用:
1、可以读取Http请求协议包中【请求行】信息
2、可以读取保存在Http请求协议包中【请求头】或则【请求体】中请求参数信息
3、可以代替浏览器向Http服务器申请资源文件调用
public class OneServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.通过请求对象,读取【请求行】中【url】信息
String url = request.getRequestURL().toString();
//2.通过请求对象,读取【请求行】中【method】信息
String method = request.getMethod();
//3.通过请求对象,读取【请求行】中uri信息
/*
* URI:资源文件精准定位地址,在请求行并没有URI这个属性。
* 实际上URL中截取一个字符串,这个字符串格式"/网站名/资源文件名"
* URI用于让Http服务器对被访问的资源文件进行定位
*/
String uri = request.getRequestURI();// substring
System.out.println("URL "+url);
System.out.println("method "+method);
System.out.println("URI "+uri);
}
}
public class TwoServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.通过请求对象获得【请求头】中【所有请求参数名】
Enumeration paramNames =request.getParameterNames(); //将所有请求参数名称保存到一个枚举对象进行返回
while(paramNames.hasMoreElements()){
String paramName = (String)paramNames.nextElement();
//2.通过请求对象读取指定的请求参数的值
String value = request.getParameter(paramName);
System.out.println("请求参数名 "+paramName+" 请求参数值 "+value);
}
}
}
public class ThreeServlet extends HttpServlet {
/*
* 问题:
* 以GET方式发送中文参数内容时,得到正常结果
* 以POST方式发送中文参数内容,得到【乱码】
* 原因:
* 浏览器以GET方式发送请求,请求参数保存在【请求头】,在Http请求协议包到达Http服务器之后,
* 请求头二进制内容由Tomcat负责解码,Tomcat9.0默认使用【utf-8】字符集,可以解释一切国家文字
* 浏览器以POST方式发送请求,请求参数保存在【请求体】,在Http请求协议包到达Http服务器之后,
* 请求体二进制内容由当前请求对象(request)负责解码。request默认使用[ISO-8859-1]字符集,一个东欧语系字符集
* 因此请求体参数内容是中文,将无法解码只能得到乱码。
* 解决方案:
* 在Post请求方式下,在读取请求体内容之前,应该通知请求对象使用utf-8字符集对请求体内容进行一次重新解码
* request.setCharacterEncoding("utf-8");
*
*
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//通知请求对象,使用utf-8字符集对请求体二进制内容进行一次重写解码
request.setCharacterEncoding("utf-8");
//通过请求对象,读取【请求体】参数信息
String value = request.getParameter("userName");
System.out.println("从请求体得到参数值 "+value);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//通过请求对象,读取【请求头】参数信息
String userName = request.getParameter("userName");
System.out.println("从请求头得到参数值 "+userName);
}
}
六、HttpServletResponse接口:
一、介绍:
1、HttpServletResponse接口来自于Servlet规范中,在Tomcat中存在servlet-api.jar
2、HttpServletResponse接口实现类由Http服务器负责提供
3、HttpServletResponse接口负责将doGet/doPost方法执行结果写入到【响应体】交给浏览器
4、程序员习惯于将HttpServletResponse接口修饰的对象称为【响应对象】
二、主要功能:
1、将执行结果以二进制形式写入到【响应体】
2、设置响应头中[content-type]属性值,从而控制浏览器使用对应编译器将响应体二进制数据编译为【文字,图片,视频,命令】
3、设置响应头中【location】属性,将一个请求地址赋值给location从而控制浏览器向指定服务器发送请求
public class OneServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String result ="Hello JavaWeb";
//1.通过响应对象,向Tomcat索要输出流对象
PrintWriter out = response.getWriter();
//2.通过输出流,将执行结果以二进制形式写入到响应体
out.write(result);
}
}
通常浏览器在接收到响应包之后,根据【响应头中content-type】
属性的值,来采用对应【编译器】对【响应体中二进制内容】进行编译处理
在默认的情况下,content-type属性的值“text” content-type="text"此时浏览器将会采用【文本编译器】对响应体二进制数据进行解析。
解决方案:
在得到输出流之前,通过响应对象对响应头中content-type属性进行
一次重新赋值用于指定浏览器采用正确编译器response.setContentType("text/html;charset=utf-8");
public class ThreeServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String result1="Java<br/>Mysql<br/>HTML<br/>"; //既有文字信息又有HTML标签命令
String result2="张三<br/>爱丽丝<br/>海绵宝宝";
//设置响应头content-type
response.setContentType("text/html;charset=utf-8");
//向Tomcat索要输出流
PrintWriter out = response.getWriter();
//通过输出流将结果写入到响应体
out.print(result1);
out.print(result2);
}
}
public class FourServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String result ="http://www.baidu.com?userName=张三";
//通过响应对象,将地址赋值给响应头中location属性
response.sendRedirect(result);//[响应头 location="http://www.baidu.com"]
}
/*
* 浏览器在接收到响应包之后,如果
* 发现响应头中存在location属性
* 自动通过地址栏向location指定网站发送请求
* sendRedirect方法远程控制浏览器请求行为【请求地址,请求方式,请求参数】
*/
}
七、请求对象和响应对象生命周期:
1.在Http服务器接收到浏览器发送的【Http请求协议包】之后,自动为当前的【Http请求协议包】生成一个【请求对象】和一个【响应对象】。
2.在Http服务器调用doGet/doPost方法时,负责将【请求对象】和【响应对象】作为实参传递到方法,确保doGet/doPost正确执行。
3.在Http服务器准备推送Http响应协议包之前,负责将本次请求关联的【请求对象】和【响应对象】销毁。
总结:【请求对象】和【响应对象】生命周期贯穿一次请求的处理过程中,一个请求需要多久,两个对象就存在多久。