HTTP请求
HTTP请求的结构
HTTP请求包含三部分:请求行、请求头、请求体。
HTTP响应的结构
HTTP常见状态码
ContentType的作用
ContentType
示例:
/**
* 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 res) throws ServletException, IOException {
// TODO Auto-generated method stub
String output = "<h1><a href='http://www.baidu.com'>百度</a></h1>";
res.setContentType("text/html;charset=utf-8");
res.getWriter().println(output);
}
}
请求转发与重定向
- 多个Servlet(JSP)之间跳转有两种方式:
request.getRequestDispatcher().forward() 请求转发: 请求转发是服务器跳转,只会产生一次请求
response.sendRedirect() 响应重定向:重定向是浏览器端跳转会产生2次请求。
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
// TODO Auto-generated method stub
req.setAttribute("username", "admin");
//实现了请求转发的功能
// req.getRequestDispatcher("/direct/index").forward(req,res);
//响应重定向需要增加contextPath
res.sendRedirect("/myJsp/direct/index");
}
设置请求自定义属性
请求允许创建自定义属性。
设置请求属性:request.setAttribute(属性名,属性值)
获取请求属性:Object attr = request.getAttribute(属性名)
浏览器Cookie
- Cookie是浏览器保存在本地的文本内容
- Cookie常用于保存登录状态、用户资料等小文本
- Cookie具有时效性, Cookie内容会伴随请求发送给Tomcat
设置cookie
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Cookie coo = new Cookie("name","张三");
coo.setMaxAge(60*60*24);
response.addCookie(coo);
response.getWriter().println("login ok ");
}
获取
protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Cookie[] cs = req.getCookies();
if(cs==null) {
response.getWriter().println("user not login");
return;
}
String user = null;
for(Cookie c : cs) {
System.out.println(c.getName() + ":" + c.getValue());
if(c.getName().equals("name")) {
user = c.getValue();
break;
}
}
if(user == null) {
response.getWriter().println("user not login");
}else {
response.getWriter().println("user:" + user);
}
Session 用户会话
- Session 用于保存于“浏览器窗口”对应的数据
- Session 的数据存在服务器的内存中,具有时效性
- Session 通过浏览器Cookie的SessionId值提取用户数据
Session 常用方法
- request.getSession() --获取 Session对象
- getAttribute() setAttribute() removeAttribute() – 获取 设置 删除Session属性
- setMaxInactiveInterval — 设置Session超时时间
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
// TODO Auto-generated method stub
HttpSession session = req.getSession();
String sessionId = session.getId();
System.out.print(sessionId);
session.setAttribute("name", "lucy");
req.getRequestDispatcher("/session/index").forward(req, res);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
HttpSession session = request.getSession();
String sessionId = session.getId();
String name = (String)session.getAttribute("name");
response.setContentType("text/html;charset=utf-8");
response.getWriter().println("这是首页,当前用户为:" + name);
}
ServletContext
ServletContext(Servlet上下文对象),是web应用全局对象。
一个web应用只会创建一个ServletContext对象。
ServletContext随着web应用启动而自动创建。
Java Web三大作用域对象
HttpServletRequest ---- 请求对象
HttpSession ----- 用户会话对象
ServletContext ---- web应用全局对象。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
ServletContext context = request.getServletContext();
String copyright = context.getInitParameter("copyright");
context.setAttribute("copyright", copyright);
String title = context.getInitParameter("title");
context.setAttribute("title", title);
response.getWriter().println("init success");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
ServletContext context = (ServletContext)request.getServletContext();
String copyright = (String)context.getAttribute("copyright");
String title = (String)context.getAttribute("title");
response.setContentType("text/html;charset=utf-8");
response.getWriter().println("<h1>"+title+"</h1>"+copyright);
}
web应用的中文乱码问题
Tomcat默认使用字符集ISO-8859-1,属于西欧字符集。
解决乱码的核心思路是将ISO-8859-1转换为UTF-8
Servlet中请求与相应都要设置值utf-8字符集。
web.xml常用配置
- 修改web应用默认首页
- Servlet通配符映射及初始化参数
- 设置404、500等状态码默认页面
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>servlet_advanced</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>patternServlet</servlet-name>
<servlet-class>pattern.PatternServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>patternServlet</servlet-name>
<url-pattern>/pattern/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>copyright</param-name>
<param-value>© 12003892号-22</param-value>
</context-param>
<context-param>
<param-name>title</param-name>
<param-value>淘宝</param-value>
</context-param>
<!-- 指定错误页面 -->
<error-page>
<error-code>404</error-code>
<location>/error/404.html</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error/500.jsp</location>
</error-page>
</web-app>
JSP九大内置对象
Tomcat常用配置
- 修改web应用端口号
- 修改ContextPath上下文路径
- 设置应用自动重载
server.xml文件修改配置。
JAVA WEB打包与发布
- Java Web应用采用war包进行发布
- 发布路径为: {TOMCAT_HOME}/webapps
- Eclipse支持war包导出
项目上右键,Export ---->WAR file