1-1 请求的结构
HTTP请求的结构:
包含三部分:请求行、请求头、请求体
请求行 : 请求方式; url地址;http版本
请求头 : 语言 zh-CN(国际化);user-Agent 使用操作系统和浏览器环境
请求体 : 参数,键值对的形式
get请求没有请求体,post请求才有请求体
get请求方式的数据存放在url,在调试工具中查看为Query String Parmeters中
post请求方式的数据存放在请求体中,在调试工具中查看为Form Data中
get请求把数据放在url后
post请求把数据存放在请求体中
1-2 利用请求头开发多端应用
获取请求头(User-Agent客户端环境)
代码:UserAgentServlet.java
package com.imooc.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class UserAgentServlet
*/
@WebServlet("/ua")
public class UserAgentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public UserAgentServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String userAgent = request.getHeader("User-Agent");
if(userAgent.length() > 0) {
System.out.println("userAgent已存在");
}
response.setContentType("text/html;charset=utf-8");
response.getWriter().println(userAgent);
String output = "";
if(userAgent.indexOf("Windows NT") != -1) {
output = "这是PC端首页
";
}else if(userAgent.indexOf("iPhone") != -1 || userAgent.indexOf("Android") != -1) {
output = "这是移动端首页
";
}
response.getWriter().println(output);
}
}
1-3 响应的结构
【F12】–>Network–>Name–>Response Headers(view source)
代码:ContentTypeServlet.java
package com.imooc.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ContentTypeServlet
*/
@WebServlet("/ct")
public class ContentTypeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ContentTypeServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String output = "百度
";
//response.setContentType("text/plain;charset=utf-8");//纯文本
//response.setContentType("text/xml;charset=utf-8");//XML文档
//response.setContentType("application/x-msdownload;charset=utf-8");//需要下载的资源
response.setContentType("text/html;charset=utf-8");//HTML文档
response.getWriter().println(output);
}
}
2-1 请求转发与响应重定向的使用
◆ 多个Servlet(JSP)之间跳转有两种方式:
◆ request.getRequestDispatcher().forword() - 请求转发
◆ response.sendRedirect() - 响应重定向
区别:
请求转发不会改变一开始访问的映射地址(url不会改变)
响应重定向会改变为最后请求的映射地址(url会改变)
代码:请求转发
CheckLoginServlet.java
package com.imooc.servlet.direct;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class CheckLoginServlet
*/
@WebServlet("/direct/check")
public class CheckLoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public CheckLoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("用户登录成功");
//实现了请求转发的功能
request.getRequestDispatcher("/direct/index").forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
代码:响应重定向
package com.imooc.servlet.direct;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class CheckLoginServlet
*/
@WebServlet("/direct/check")
public class CheckLoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public CheckLoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println(