Servlet
Servlet的体系结构:
Servlet – 接口 GenericServlet – 抽象类 HttpServlet – 抽象类
GenericServlet:将Servlet接口中其他方法做了默认空实现,只将service()方法作为抽象,将来定义Servlet类时,可以继承GenericServlet,实现service()方法即可
HttpServlet:对http协议的一种封装,简化操作:
1,定义类继承HttpServlet
2,复写doGet/doPost方法
Servlet相关配置:
一个Servlet可以定义多个访问路径:@WebServlet({"/d4","/dd4","/ddd4"})
路径定义三种规范:
1,/xxx:路径匹配 2,/xxx/xxx:多层路径,目录结构 3,*.do:扩展名匹配
HTTP
超文本传输协议:定义了客户端和服务器端通信时发送数据的格式
特点:
- 基于TCP/IP高级协议
- 默认端口号:80
- 基于请求/响应模型:一次请求对应一次响应
- 无状态,每次请求之间相互独立,不能交互数据
HTTP协议有7种请求方式,常用GET和POST
GET:请求参数在请求行中,在url后,请求的url长度有限制,不太安全
POST:请求参数在请求体中,请求的url长度没有限制,相对安全
Request
1,request和response对象是由服务器创建的。
2,request对象负责获取请求消息,response对象是来设置响应消息
request对象继承体系结构:
ServletRequest --接口 HttpServletRequest–接口org.apache.catalina.connector.RequestFacade – 类(tomcat)
获取请求行数据:
package com.wzc.web.request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/*
演示Request对象获取数据请求行数据
* */
@WebServlet("/RequestDemo1")
public class RequestDemo1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取请求方式:GET
String method = request.getMethod();
System.out.println(method);
// 获取虚拟路径
String contextPath = request.getContextPath();
System.out.println(contextPath);
// 获取Servlet路径
String queryString = request.getQueryString();
System.out.println(queryString);
// 获取请求URI
String requestURI = request.getRequestURI();
System.out.println(requestURI);
// 获取URL
StringBuffer requestURL = request.getRequestURL();
System.out.println(requestURL);
// 获取协议以及版本
String protocol = request.getProtocol();
System.out.println(protocol);
// 获取客户机的IP地址
String remoteAddr = request.getRemoteAddr();
System.out.println(remoteAddr);
}
}
获取请求头数据:
package com.wzc.web.request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
@WebServlet("/RequestDemo2")
public class RequestDemo2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取请求头数据
// 获取所有请求头名称
Enumeration<String> headerNames = request.getHeaderNames();
// 遍历
while(headerNames.hasMoreElements()){
String name = headerNames.nextElement();
// 根据名称获取请求头的值
String value = request.getHeader(name);
System.out.println(name+"------"+value);
}
}
}
获取请求体数据:
package com.wzc.web.request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
@WebServlet("/RequestDemo5")
public class RequestDemo5 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取请求消息体--请求参数
// 获取字符流
BufferedReader br = request.getReader();
// 读取数据
String line = null;
while ((line = br.readLine()) !=null){
System.out.println(line);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
其他功能:
获取请求参数通用方式:不论get还是post请求方式都可以使用下列方法来获取请求参数
1. String getParameter(String name):根据参数名称获取参数值 username=zs&password=123
2. String[] getParameterValues(String name):根据参数名称获取参数值的数组 hobby=xx&hobby=game
3. Enumeration getParameterNames():获取所有请求的参数名称
4. Map<String,String[]> getParameterMap():获取所有参数的map集合
如何解决中文乱码问题:
get方式:tomcat8已经将get方式乱码问题解决了
post方式:会乱码,通过设置request的编码request.setCharacterEnconding(“utf-8”)
请求转发:一种在服务器内部的资源跳转方式
1,通过request对象获取请求转发器对象:RequestDispatcher getRequestDispatcher(String path)
2,使用RequestDispatcher对象来进行转发:forward(ServletRequest
request,ServletResponse response)
特点:
1,浏览器地址栏路径不发生变化
2,只能转发到当前服务器内部资源中
3,转发是一次请求
共享数据:
域对象:一个有作用范围的对象,可以在范围内共享数据
request域:代表一次请求范围,一般用于请求转发的多个资源*享数据
package com.wzc.web.request;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;
@WebServlet("/RequestDemo7")
public class RequestDemo7 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("demo7777被访问了");
// 转发到demo8资源
/* RequestDispatcher requestDispatcher = request.getRequestDispatcher("/RequestDemo8");
requestDispatcher.forward(request,response);*/
// 存储数据到request域中
request.setAttribute("msg","hello");
request.getRequestDispatcher("/RequestDemo8").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
package com.wzc.web.request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;
@WebServlet("/RequestDemo8")
public class RequestDemo8 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取数据
Object msg = request.getAttribute("msg");
System.out.println(msg);
System.out.println("demo8888被访问了");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
获取ServletContext:
package com.wzc.web.request;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/RequestDemo9")
public class RequestDemo9 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext = request.getServletContext();
System.out.println(servletContext);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}