HTTPServletReauest和HTTPServletResponse
Servlet配置三种方式
- 1.全路径匹配
以/开始 例如www.baidu.com/test/aa
- 2.路径匹配,前半段匹配
以/开始,但是以结束/aa/
- 3.扩展名匹配
不能以/开始 是以开始的。例如,.jsp
- 优先级:全路径>目路径匹配>扩展名匹配
- *是一个通配符,可以匹配任何文字
ServletContext
Servlet上下文
每个web工程都只有一个ServletContext对象。说白了就是不管在哪个Servlet里面,获取到的这个类的对象都是同一个。
如何得到对象
ServletContext context = getServletContext();
有什么用
获取全局配置参数
<context-param>
<param-name>address</param-name>
<param-value>湖南长沙</param-value>
</context-param>
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servlet = getServletContext();
String address = servlet.getInitParameter("address");
System.out.println(address);
}
获取web工程的资源
- 获取资源在tomcat里面的绝对路径
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servlet = getServletContext();
// String address = servlet.getInitParameter("address");
// System.out.println(address);
String path = servlet.getRealPath("info/address");
System.out.println(path);
}
- getResourceAsStream获取资源流对象
- 直接给相对路径,然后获取流对象
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servlet = getServletContext();
String address = servlet.getInitParameter("address");
System.out.println(address);
String path = servlet.getRealPath("info/address");
System.out.println(path);
//1、创建属性对象
Properties properties = new Properties();
//
InputStream is = servlet.getResourceAsStream("info/address");
properties.load(is);
String name = properties.getProperty("address");
System.out.println("name="+name);
is.close();
}
- 通过classloader去获取web工程下的资源
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servlet = getServletContext();
String address = servlet.getInitParameter("address");
System.out.println(address);
String path = servlet.getRealPath("info/address");
System.out.println(path);
//1、创建属性对象
Properties properties = new Properties();
//
InputStream is = servlet.getResourceAsStream("info/address");
properties.load(is);
String name = properties.getProperty("address");
System.out.println("name="+name);
is.close();
InputStream i = this.getClass().getClassLoader().getResourceAsStream("../../info/address");
properties.load(i);
String nam = properties.getProperty("address");
System.out.println("nam="+nam);
i.close();
}
存取数据,servlet间共享数据域对象
使用ServletContext存取数据
- 定义一个登陆的html页面,定义一个form表单
<body>
<h2>请输入一下内容,完成登录</h2>
<form action="LoginServlet">
账号:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
- 定义一个Servlet,名为LoginServlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取数据
String userName = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("userName"+userName+"==password+"+password);
//2.校检数据
//向客户端输出内容
PrintWriter pw = response.getWriter();
if("admin".equals(userName) && "123".equals(password)) {
System.out.println("登录成功");
pw.write("login success...");
}else {
pw.write("login failed...");
}
}
- 针对成功或者失败,进行判断,然后跳转到不同的网页
public class LoginServlet extends HttpServlet {
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取数据
String userName = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("userName="+userName+",=password+"+password);
//2.校检数据
//向客户端输出内容
PrintWriter pw = response.getWriter();
if("admin".equals(userName) && "123".equals(password)) {
// System.out.println("登录成功");
// pw.write("login success...");
//设置状态码,重定位状态码
response.setStatus(302);
response.setHeader("Location", "LoginSuccess.html");
}else {
pw.write("login failed...");
}
}
ServletContext何时创建,何时销毁
创建:服务器启动的时候,会为托管的每一个web应用程序,创建一个ServletContext对象
销毁:从服务器移除托管,或者是关闭服务器
- ServletContext的作用范围
只要在这个项目里面,都可以去。只要是同一个项目。两个项目ServletContext对象是不同的。
HttpServletRequest
这个对象封装了客户端提交过来的一切数据
- 可以获取客户端请求头信息
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Enumeration<String> head = request.getHeaderNames();
while(head.hasMoreElements()) {
String name = head.nextElement();
String value = request.getHeader(name);
System.out.println(name+"="+value);
}
}
结果如下:
host=localhost:8088
connection=keep-alive
upgrade-insecure-requests=1
accept=text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14) AppleWebKit/605.1.15 (KHTML, like Gecko) Safari/522.0
accept-language=zh-cn
cache-control=no-cache
accept-encoding=gzip, deflate
- 获取客户端提交过来的数据
Map<String, String[]> map = request.getParameterMap();
Set<String> keySet = map.keySet();
Iterator<String> iterator = keySet.iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next();
System.out.println("key="+key + "--的值总数有:"+map.get(key).length);
String value = map.get(key)[0];
String value1 = map.get(key)[1];
String value2 = map.get(key)[2];
System.out.println(key+" ======= "+ value + "=" + value1 + "="+ value2);
}
- 获取中文数据
客户端提交数据给服务器端,如果数据中带有中文,可能会出现乱码
如果是GET方式
- 代码转换
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); System.out.println("username="+username+",password="+password); username = new String(username.getBytes("ISO-8859-1"),"Utf-8"); System.out.println("username="+username+",password="+password); }
- 可以在tomcat里面做设置处理conf/server.xml加上URIEncoding="utf-8"
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>
如果是POST方式
request.setCharacterEncodiong("UTF-8");
这行一定要设置在getParameter之前
HttpServletResponse
输出数据到页面上
- 以字符流的方式写数据
response.getWriter().write("hello response...");
- 以字节流的方式写数据
response.getOutputStream().write("hello,world...".getBytes());
响应的数据中有中文,那么可能出现中文乱码
- 以字符流输出
response.getWriter()
//1. 指定输出到客户端的时候,这些文字使用UTF-8编码
response.setCharacterEncoding("UTF-8");
//2. 直接规定浏览器看这份数据的时候,使用什么编码来看。
response.setHeader("Content-Type", "text/html; charset=UTF-8");
response.getWriter().write("我爱中国...");
- 以字节流输出
response.getOutputStream()
//1. 指定浏览器看这份数据使用的码表
response.setHeader("Content-Type", "text/html;charset=UTF-8");
//2. 指定输出的中文用的码表
response.getOutputStream().write("我爱中国..".getBytes("UTF-8"));
不管是字节流还是字符流,直接使用一行代码就可以了。
response.setContentType("text/html;charset=UTF-8");
然后再写数据即可。