1.ServletContext基本概念
ServletContext是是一个全局的储存信息的空间随服务器开启、关闭。一个web应用对应一个ServletContext。ServletContext实例包含了所有servlet共享的资源信息。通过提供一组方法给servlet使用,用来和servlet容器通讯,比如获取文件的MIME类型、分发请求、记录日志等。
这里需要注意一点,如果你的应用是分布式部署的,那么每台服务器实例上部署的应用实例都各自拥有一个ServletContext实例。
2.简单的数据共享方式
1.创建一个Hello类,继承HttpServlet,在里买按写入传递给ServletContext的字符串
public class ServletContext extends HttpServlet {
/*
重写doGet和doPost,让doPost方法调用doGet方法
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//使用ServletContext中的setAttribute方法传递数据(“hello”为数据名,“你好”为数据值)
this.getServletContext().setAttribute("hello","你好");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
2.创建一个Get类,在其中调用getServletContext().getAttribute()方法,得到数据
public class GetHello extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String s = (String) context.getAttribute("hello");
//设置解析类型
resp.setContentType("text/html");
//设置解码方法
resp.setCharacterEncoding("utf-8");
//在页面输出内容
PrintWriter writer =resp.getWriter();
writer.write("内容:" + s);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
3.在注册表注册两个HttpSerlet类,对应不同的地址,首先访问Get类对应的地址,发现传递内容为null,再访问Hello对应地址,再次访问Get类对用地址,返回内容为"你好"。
注意:servletContext具有全局性,任何用户访问过Hello类后,内容都会被传递。
3.ServletContext其余对象的用法
3.1获取初始化参数(在web服务器注册过的内容的参数)
首先,选取一个在web服务器的配置(web.xml)
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql:/localhost3306/mybatis</param-value>
</context-param>
获取初始化对象的方法演示
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = getServletContext();
String url = context.getInitParameter("url");
resp.getWriter().write(url);
}
请求到的网页内容
3.2请求转发
public class ServletDemo04 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = getServletContext();
//RequestDispatcher requestDispatcher = context.getRequestDispatcher("/gp");//转发到的请求路径
//requestDispatcher.forward(req,resp);//调用forward实现请求转发
context.getRequestDispatcher("/gp").forward(req,resp);
System.out.println("已进入此页面");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
访问此路径结果(地址不被改变):
请求结果为200:
3.3读取资源文件
Properties类的使用
- 在java目录下新建properties
- 在resources目录下新建properties
发现:都被打包到同意路径下:classes,我们俗称这个路径为classpath
思路:需要一个文件流
public class ServletDemo05 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
Properties prop = new Properties();
prop.load(is);
String name = prop.getProperty("username");
String psd = prop.getProperty("password");
resp.getWriter().write(name + ":" + psd);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
4.HttpServletResponse和HttpServletRequest
Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象、和代表响应的response对象。
request和response对象即然代表请求和响应,那我们要获取客户机提交过来的数据,只需要找request对象就行了。要向客户机输出数据,只需要找response对象就行了。
4.1响应(HttpServletResponse)
负责给客户端发送信息的方法:
ServletOutputStream getOutputStream()
PrintWriter getWriter()
用来设置响应头的方法
void setCharacterEncoding(String var1);
void setContentLength(int var1);
void setContentType(String var1);
void setBufferSize(int var1);
void setDateHeader(String var1, long var2);
void addDateHeader(String var1, long var2);
void setHeader(String var1, String var2);
void addHeader(String var1, String var2);
void setIntHeader(String var1, int var2);
void addIntHeader(String var1, int var2);
常见应用
1.向浏览器输出消息
2.下载文件
1.获取下载文件的路径
2.获取下载的文件名
3.想办法让浏览器能支持下载我们的东西(响应头)
4.获取下载文件的输入流
5.创建缓冲区
6.获取OutputStream对象
7.将FileOutputStream写入buffer缓冲区
8.使用OutputStream将缓冲区的数据输出到客户端
4.2请求(HttpServletRequest)
HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象提供的方法,可以获得客户端请求的所有信息。
主要应用:
package gacl.request.study;
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;
/**
* @author gacl
* 通过request对象获取客户端请求信息
*/
public class RequestDemo01 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/**
* 1.获得客户机信息
*/
String requestUrl = request.getRequestURL().toString();//得到请求的URL地址
String requestUri = request.getRequestURI();//得到请求的资源
String queryString = request.getQueryString();//得到请求的URL地址中附带的参数
String remoteAddr = request.getRemoteAddr();//得到来访者的IP地址
String remoteHost = request.getRemoteHost();
int remotePort = request.getRemotePort();
String remoteUser = request.getRemoteUser();
String method = request.getMethod();//得到请求URL地址时使用的方法
String pathInfo = request.getPathInfo();
String localAddr = request.getLocalAddr();//获取WEB服务器的IP地址
String localName = request.getLocalName();//获取WEB服务器的主机名
response.setCharacterEncoding("UTF-8");//设置将字符以"UTF-8"编码输出到客户端浏览器
//通过设置响应头控制浏览器以UTF-8的编码显示数据,如果不加这句话,那么浏览器显示的将是乱码
response.setHeader("content-type", "text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.write("获取到的客户机信息如下:");
out.write("<hr/>");
out.write("请求的URL地址:"+requestUrl);
out.write("<br/>");
out.write("请求的资源:"+requestUri);
out.write("<br/>");
out.write("请求的URL地址中附带的参数:"+queryString);
out.write("<br/>");
out.write("来访者的IP地址:"+remoteAddr);
out.write("<br/>");
out.write("来访者的主机名:"+remoteHost);
out.write("<br/>");
out.write("使用的端口号:"+remotePort);
out.write("<br/>");
out.write("remoteUser:"+remoteUser);
out.write("<br/>");
out.write("请求使用的方法:"+method);
out.write("<br/>");
out.write("pathInfo:"+pathInfo);
out.write("<br/>");
out.write("localAddr:"+localAddr);
out.write("<br/>");
out.write("localName:"+localName);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}