-
20.3 通过Server 修改tomcat默认路径
-
20.4 URL和URI 要区分好
-
20.5 Servlet三大域对象 和 JSP四大域对象
-
21. 补充2
-
- 21.1 display-name和welcome-file-list标签
-
21.2 session监听器 的不同
-
21.3 attributeReplaced为什么开始就要调用
-
21.4 8个监听器类型
-
21.5 return;的用法
-
21.6 servlet中的 / 路径不能随便配置!!
-
21.7 请求转发和重定向的路径问题
===========================================================================
通过实现Servlet接口来实现Servlet程序。
**Servlet的生命周期:
1.执行Servlet构造器方法。
2.执行init初始化方法。
3.执行service方法。
4.执行destroy销毁方法。**
**第1,2步:是在第一次访问时创建调用。
第3步:是每次访问时都会调用,(例如刷新客户端)。
第4步:web工程停止的时候调用。**
package com.test01;
import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
public class HelloServlet implements Servlet{
public HelloServlet() {
System.out.println(“1. 构造器方法”);
}
@Override
public void destroy() {
// TODO Auto-generated method stub
System.out.println(“4.destory方法”);
}
@Override
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}
@Override
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
System.out.println(“2. 初始化方法”);
}
//service方法是专门用来处理请求和响应的。
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
System.out.println(“3. service方法”);
//ServletRequest方法中没有getMethod()方法但是它的子接口HttpServletRequest中有。
HttpServletRequest httpServletRequset = (HttpServletRequest)req;
//获取到客户端请求的方式:
String method = httpServletRequset.getMethod();
if(“GET”.equals(method)) {
doGet();
}else if(“POST”.equals(method)) {
doPost();
}
}
//该方法用来执行获取get请求后的操作
public void doGet() {
System.out.println(“对于接受到GET请求后,该方法用来执行get方法后的请求操作。”);
}
//该方法用来执行post请求后的操作
public void doPost() {
System.out.println(“对于接受到POST请求后,该方法用来执行POST方法后的请求操作。”);
}
}
==================================================================================
GET和POST分发请求步骤:
//service方法是专门用来处理请求和响应的。
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
System.out.println(“3. service方法”);
//ServletRequest方法中没有getMethod()方法但是它的子接口HttpServletRequest中有。
HttpServletRequest httpServletRequset = (HttpServletRequest)req;
//获取到客户端请求的方式:
String method = httpServletRequset.getMethod();
if(“GET”.equals(method)) {
doGet();
}else if(“POST”.equals(method)) {
doPost();
}
}
=============================================================================
<?xml version="1.0" encoding="UTF-8"?>WebTest02
HelloServlet
com.test01.HelloServlet
HelloServlet
/hello
`
===========================================================================================
除了实现Servlet接口,还能通过继承HttpServlet实现Servlet程序。
-
1.编写一个类继承HttpServlet类。
-
2.根据业务需要重写doGet或doPost方法。
-
3.到web.xml中的配置Servlet程序的访问地址。
平时创建servlet程序,我们直接可以在IDEA或eclise中直接new一个servlet程序,定义好各个配置信息即可,注意的是3.0版本后是注解版本,2.5之前是xml版本别混淆。
5. Servlet 接口, GenericServlet 类, HttpServlet 类之间的关系
=================================================================================================================
注意的是: HttpServlet类中有service()方法,并且里面会有判断例如是get还是post,从而执行doGet或doPost方法,这样我们只需要继承HttpServlet,重写doGet和doPost即可。
==============================================================================
6.1 ServletConfig类和ServletContext类区别
**ServletConfig和ServletContext(容器)是相反的.
ServletConfig都是内测私用的,定义在那个servlet中,那个servlet就能获取上面参数值;
而ServletContext容器则是共享的,设置在webapp主目录下的!所有的servlet都可以调用。**
ServletConfig类是Servlet的配置信息类。
**它有三大作用:
可以获取Servlet程序的别名,servlet-name的值。
获取初始化参数init-param。
获取ServletContext对象。**
该类一般都在init()方法中:
package com.test01;
import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
public class HelloServlet implements Servlet{
public HelloServlet() {
System.out.println(“1. 构造器方法”);
}
@Override
public void destroy() {
System.out.println(“4.destory方法”);
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void init(ServletConfig config) throws ServletException {
System.out.println(“2. 初始化方法”);
//1.获取servlet-name别名值:
System.out.println(“别名为:”+config.getServletName());
//2.获取初始化参数init-name:
System.out.println(“初始化参数username的值是:”+config.getInitParameter(“username”));
System.out.println(“初始化参数url的值:”+config.getInitParameter(“url”));
//3.获取ServletContext对象
System.out.println(config.getServletContext());
}
//service方法是专门用来处理请求和响应的。
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
System.out.println(“3. service方法”);
//ServletRequest方法中没有getMethod()方法但是它的子接口HttpServletRequest中有。
HttpServletRequest httpServletRequset = (HttpServletRequest)req;
//获取到客户端请求的方式:
String method = httpServletRequset.getMethod();
if(“GET”.equals(method)) {
doGet();
}else if(“POST”.equals(method)) {
doPost();
}
}
//该方法用来执行获取get请求后的操作
public void doGet() {
System.out.println(“对于接受到GET请求后,该方法用来执行get方法后的请求操作。”);
}
//该方法用来执行post请求后的操作
public void doPost() {
System.out.println(“对于接受到POST请求后,该方法用来执行POST方法后的请求操作。”);
}
}
ServletConfig对象可以自己定义一个,不一定非要使用init方法中的形参。
ServletConfig con = getServletConfig();
String str = con.getInitParameter(“[对应init-param的别名]”);
上面对应web.xml的init-param标签如下:
<?xml version="1.0" encoding="UTF-8"?>WebTest02
HelloServlet
com.test01.HelloServlet
username
root
url
jdbc:mysql://localhost:3306/test
HelloServlet
/hello
HelloServlet02
com.test01.HelloServlet02
HelloServlet02
/hello2
总结起来就是:Servlet程序 和ServletConfig对象都是由Tomcat创建的,我们负责使用而已。
Servlet程序默认是第一次访问的时候创建,ServletConfig是每个Servlet程序创建时,就创建一个对应的ServletConfig对象。
在别的方法中通过getServletConfig()也可以获得当前Servlet程序的ServletConfig对象。
package com.test01;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloServlet02 extends HttpServlet{
@Override
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
super.init(config); //
System.out.println(“重写了init方法,主要注意一定要继承一下父类的init方法,传递config参数!!!”
- “不然下面的getServletConfig()方法就会报错!”);
//为什么这样,看源码就懂了。
}
/*
- doGet()方法是在get请求的时候调用。
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println(“HelloServlet2 的doGet方法”);
//在别的方法中通过getServletConfig()也可以获得当前Servlet程序的ServletConfig对象。
ServletConfig sc = getServletConfig();
System.out.println(sc);
}
/*
- doPost()方法是在Post请求的时候调用。
*/
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println(“HelloServlet2的doPost方法”);
}
}
需要注意,一个Servlet程序只有一个ServletConfig对象,他们是一一对应的关系,不要混淆。
================================================================================
7.1 ServletConfig类和ServletContext类区别
这里我在强调一下两者区别:
**ServletConfig和ServletContext(容器)是相反的.
ServletConfig都是内测私用的,定义在那个servlet中,那个servlet就能获取上面参数值;
而ServletContext容器则是共享的,设置在webapp主目录下的!所有的servlet都可以调用。**
ServletContext是一个接口,他表示Servlet上下文对象。
一个web工程,只有一个ServletContext对象实例(单例设计)。
**ServletContext对象是一个域对象。
(什么是域对象,是像map一样,可以存储数据的对象,叫做域对象。这里的域指的是存取数据的操作范围。)**
前面也说了ServletContext就相当于一个共享操作,所有的servlet程序都可以使用,里面的属性值。
context-param配置如下:(它是配置在webapp标签中!)
username
context
同样也是使用getServletContext()
方法调用:
ServletContext servletContext = getServletContext();
String initParameter = servletContext.getInitParameter(“jdbc.driver”);
System.out.println(initParameter);
//ServletContext servletContext2 = getServletConfig().getServletContext();
//这个方法和上面没啥不同,看源码就知道了。
**1.获取web.xml中配置的上下文参数context-param。
2.获取当前工程路径,格式:/工程路径。
3.获取工程部署后在服务器硬盘上的绝对路径。
4.像map一样可以存储数据。**
前三步的操作:
package com.test01;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ContextServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.获取web.xml中配置的上下文参数context-param。
ServletContext context = getServletConfig().getServletContext();
String name = context.getInitParameter(“username”);
System.out.println(“context-param参数username的参数值为:”+ name);
System.out.println(“context-param参数password的参数值为:”+context.getInitParameter(“password”));
//2.获取当前工程路径,格式:/工程路径。
System.out.println(“获取当前工程路径:”+context.getContextPath());
//3.获取工程部署后在服务器硬盘上的绝对路径。
/*
-
这里的参数"/",斜杠被服务器解析地址为:http://ip:port/工程名/ 。
-
为什么通过斜杆能获得绝对路径地址值呢?
-
他就是个映射到web目录中了,需要了解tomcat一些详情,记住"/"代表着当前磁盘当前路径的位置。
-
当然也可以通过这个当前路径给他设定其他的相对路径,例如"/CSS",就是往下拓展一个路径。
*/
System.out.println(“获取工程在服务器上硬盘的路径:”+context.getRealPath(“/”));
System.out.println(“获取工程在服务器上硬盘的路径:”+context.getRealPath(“/CSS”));
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
<?xml version="1.0" encoding="UTF-8"?>WebTest02
username
context
password
root
username
root
url
jdbc:mysql://localhost:3306/test
ContextServlet
com.test01.ContextServlet
ContextServlet
/context
第四步骤:存储数据
package com.test01;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ContextServlet01 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();//也可以直接调用getServletContext也行,源码还是要调用getServletConfig()。
//保存之前不管什么字符串值为null
System.out.println(“保存之前:Context1获取key1的值是:”+context.getAttribute(“key1”));
context.setAttribute(“key1”, “value”);
//因为一个工程只有一个ServletContext对象,一旦设置后,无论访问或者其他对象访问后都能取到这个ServletContext对象的内容属性值。
System.out.println(“Context1中获取域数据key1的值是:”+context.getAttribute(“key1”));
System.out.println(“Context1中获取域数据key1的值是:”+context.getAttribute(“key1”));
}
}
<?xml version="1.0" encoding="UTF-8"?>WebTest03
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
username
context
url
root
ContextServlet01
ContextServlet01
com.test01.ContextServlet01
ContextServlet01
/context1
ContextServlet02
ContextServlet02
com.test01.ContextServlet02
ContextServlet02
/context2
在这解释一下热加载和热部署,热加载就是我们在tomcat部署项目后,我们修改项目中的代码,tomcat会自动热加载,重新将项目移除再加载,这个过程就叫做热加载。这个部署就叫热部署。
此外,注意热加载仅仅加载{ }里面的东西,修改方法权限,添加方法,修改web.xml等都不能触发热加载。这就要重启tomcat;
我们想要查看源码,仅仅看tomcat中的servlet-api是不能的,因为它里面全是.class字节码文件,我们要看源码必须是.java文件,所以我们要去tomcat官方下载src源码才可以将他们关联attach起来,从而查看源码!
像Servlet,GenericServlet,HttpServlet都是在tomcat中的,我们本身并不自带,因此,第一次设置时,我们要添加library中的server runtime。
=======================================================================
**客户端向服务器发送的数据叫做请求。
服务器给客户端回传数据叫做响应。**
http有8中请求格式:
请求又分为GET请求和POST请求两种。
GET请求的报文格式如下:
POST请求的报文格式如下:
9.什么时候用doGet接受get请求,什么时候用doPost接受post请求?
=====================================================================================================
下面是常见的特殊情况:
=============================================================================
==========================================================================
对于响应码大体可以分为下面这几种:
**100开头:服务器正在处理,也就是pending;
200开头:完成;
300开头:重定向;
400开头:前端浏览器访问错误;
500开头:服务器内部错误。**
===========================================================================
MIME是HTTP协议中的数据类型。
=======================================================================================
每次只要有请求进入Tomcat服务器,Tomcat服务器就会报请求过来的HTTP协议信息解析好封装到Request对象中,然后传递到service()方法(doGet和doPost)中给我们使用。
我们可以通过HttpServletRequest对象,获取到所有请求的信息。
每请求一次,就创建一个HttpServletRequest对象,请求完成就销毁了。
HttpServletRequest对象对应的一些方法:
package com.test.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 RequestAPIServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//request.getRequestURI():获取请求资源路径。
System.out.println(“获取请求资源路径:”+request.getRequestURI());
//request.getRequestURL()):获取请求的统一资源定位符(绝对路径)。
System.out.println(“获取请求的统一资源定位符(绝对路径):”+request.getRequestURL());
//request.getRemoteHost():获取请求客户端的ip地址。
/*
-
使用localhost或127.0.0.1访问时,自然得到的客户端ip就是127.0.0.1
-
如果使用真实ip访问时,那么得到的也是真实ip的地址值。*/
System.out.println(“获取客户端ip地址:”+request.getRemoteHost());
//request.getHeader(“xxx”):获取特定的请求头中的信息。
System.out.println(“获取请求头User-Agent的信息:”+request.getHeader(“User-Agent”));
//request.getMethod():获取请求方式。
System.out.println(“获取请求方式:”+request.getMethod());
}
}
===================================================================================
需要用到getParameter()和getParameterValues()方法:
package com.test.servlet;
import java.io.IOException;
import java.util.Arrays;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ParameterServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//req.getParameter(“xxx”)方法:获取请求参数
String username = req.getParameter(“username”);
String password = req.getParameter(“password”);
//req.getParameterValues(“xxx”):也是获取请求参数,但是是多个值发送过来的时候。
/*
- 像input的checkbox类型,就可以进行一个多选的操作,同时发送多个参数到服务器。*/
String[] hobby = req.getParameterValues(“hobby”);
System.out.println(“用户名:”+username);
System.out.println(“密码:”+password);
System.out.println(“兴趣爱好:”+Arrays.asList(hobby));
}
}
注意,当你调用post请求,发送数据给客户端,发送的是汉字的话,就会乱码!
使用req.setCharacterEncoding(“UTF-8”)来解决乱码问题。
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置请求体的字符集为UTF-8,从而解决post请求的中文乱码问题。
//这个api方法必须要在接受请求内容之前调用,请求内容仍然乱码,像下面的username,password等等都是请求后得到的,都要在执行完该方法后才能不乱码!!
req.setCharacterEncoding(“UTF-8”);
System.out.println(“-------doPost--------”);
String username = req.getParameter(“username”);
String password = req.getParameter(“password”);
String[] hobby = req.getParameterValues(“hobby”);
System.out.println(“用户名:”+username);
System.out.println(“密码:”+password);
System.out.println(“兴趣爱好:”+Arrays.asList(hobby));
}
=======================================================================
**什么是请求的转发?
请求转发是指,服务器收到请求后,从一个资源跳转到另一个资源的。**
Servlet1和Servlet2的请求转发:
Servlet1:
package com.test.servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Servlet1 extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//处理请求参数
String username = req.getParameter(“username”);
System.out.println(“Servlet1(柜台1)查看处理参数(材料):”+username);
//给username,处理一下,盖个章作为标识
req.setAttribute(“key1”, “柜台1的章”);
System.out.println(“已经盖完章”);
//问路:向Servlet2中处理
/*
- 这里请求转发必须以"/"斜杆开头,它代表:http://ip:port/工程名
*/
RequestDispatcher requestDispatcher = req.getRequestDispatcher(“/servlet2”);
System.out.println(“知道servlet2在哪里了”);
//走向servlet2
requestDispatcher.forward(req, resp);
}
}
Servlet2:
package com.test.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 Servlet2 extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter(“username”);
System.out.println(“Servlet2(柜台2)中查看参数(材料):”+username);
//必须与Servlet1的键一样!
Object key1 = req.getAttribute(“key1”);
System.out.println(“柜台1是否有章:”+key1);
//处理自己的业务
System.out.println(“Servlet2处理自己的业务”);
}
}
**注意事项:
1.像WEB-INF这种目录不能再浏览器直接访问,但是可以通过请求转发的这种方式来间接访问。
2.他是不可以访问工程以外的资源!因为"/"斜杆就代表着从当前工程开始的,如果你再想访问www.baidu.com那就不可能了。**
===========================================================================
请求转换跳转页面,如果用…/…/这样操作会返回不到想要的页面,原因如下:
这是a下的b下的c.html页面
因此遇到这种情况,就要用到base标签。
**此外还要注意下面的两种情况:
http://127.0.0.1:8080/WebTest04/a/b/
http://127.0.0.1:8080/WebTest04/a/b
这b后面有斜杆和没斜杆有区别!有斜杆说明他是一个目录,没有就是一个文件。**
====================================================================================
在浏览器端,斜杆代表:http://ip:port/
在服务器解析,斜杆代表:http://ip:port/工程名/
在Servlet中,Web.xml配置的url-pattern中一个斜杆,代表没有要访问的url则访问这个指向的类(具体见,20补充)。
=======================================================================================
18.1 HttpServletResponse的介绍 和 两个响应流
HttpServletResponse类和HttpServletRequest类是一样的,每次请求进来,Tomcat服务器都会常见一个Response对象传递给Servlet程序使用。HttpServletRequest接受请求的信息;HttpServletResponse就是响应的信息。
我们如果需要设置返回给客户端的信息,都可以通过HttpServletResponse对象来进行设置。
两个响应流,一个字节流,一个字符流。
字节流: response.getOutputStream() ,二进制
字符流:response.getWriter() , 字符
两个流只能调用一个!
创建字符或字节流,调用print()或write()方法发送数据。
package com.test.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ResponseIOServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter write = response.getWriter();
//向客户端传送数据,用write或print都可以
//write.print(“”);
write.write(“response’s content”);
}
}