REQUEST:
情况一:
e.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="f.jsp" method="post">
name:<input type="text" name="name">
<input type="submit" value="submit">
</form>
</body>
</html>
f.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
Hello:<%=request.getParameter("name")%>
</body>
</html>
测试:
进入e.jsp:
b表单提交后,进入f.jsp:
当一个响应返回时,即被销毁,当发送一个请求时被创建。而且f.jsp可以获取e.jsp传过来的name值。
情况二:
e.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="f.jsp">to fff</a>
<%request.setAttribute("name","sasa");%>
</body>
</html>
f.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
Hello:<%=request.getAttribute("name")%>
</body>
</html>
测试:
进入e.jsp:
通过超链接的方式进入f.jsp:
结论:通过超链接的方式进入f.jsp的话,f.jsp无法获取e.jsp页面request里面的属性。
情况三:
e.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%request.setAttribute("name","sasa");%>
<jsp:forward page="f.jsp" ></jsp:forward>
</body>
</html>
f.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
Hello:<%=request.getAttribute("name")%>
</body>
</html>
测试:
进入e.jsp后直接通过请求转发的方式进入f.jsp:
结论:请求转发只有一个request请求。
情况四:
TestServlet:
package com.sa.servlet;
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(name = "TestServlet")
public class TestServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("name","sasa");
request.getRequestDispatcher("/pages/f.jsp").forward(request,response);
}
}
f.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
Hello:<%=request.getAttribute("name")%>
</body>
</html>
测试:
情况五:
TestServlet:
package com.sa.servlet;
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(name = "TestServlet")
public class TestServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("name","sasa");
//request.getRequestDispatcher("/pages/f.jsp").forward(request,response);
response.sendRedirect(request.getContextPath()+"/pages/f.jsp");
}
}
f.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
Hello:<%=request.getAttribute("name")%>
</body>
</html>
测试:
结论:请求转发的重定向是两个请求。
SESSION:
①:当第一次访问WEB应用的一个JSP或servlet时,且该JSP或servlet中还需要创建session对象,此时服务器会创建一个session对象。
②:session销毁:session过期,直接调用session的invalidate方法,当前web应用被卸载(session可以被持久化)。
③:关闭浏览器,并不意味着session被销毁,还可以通过sessionid找到服务器中的session对象。
进入i.jsp:查看sessionid:
关闭浏览器,再次进入该页面:
发现session未被创建。
APPLICATION:
贯穿于当前WEB应用的生命周期,当前WEB应用被加载时,创建application对象,被卸载时,销毁application。