一尘不染

由servlet设置的属性存储在哪里?

jsp

以下是我的servlet:

public class ServletExample extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        if(request.getParameter("firstname") == null || request.getParameter("lastname") == null){
            this.getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
            return;
        }

        String firstName = request.getParameter("firstname");
        String lastName = request.getParameter("lastname");

        request.setAttribute("firstname", firstName);
        request.setAttribute("lastname", lastName);

        this.getServletContext().getRequestDispatcher("/output.jsp").forward(request, response);
    }

}

以下是我的index.jsp

<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
    <form action="servletexample" method="post" >
       <table border="0">
         <tr>
            <td>First Name:</td> <td><input type="text" name="firstname" /></td>
         </tr>
         <tr>
            <td>Last Name:</td> <td><input type="text" name="lastname"  /></td>
         </tr>
         <tr>
            <td colspan="2"> <input type="submit" value="Submit" /></td>
         </tr>
       </table>
    </form>
</body>
</html>

以下是我的output.jsp

<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
    <h1>Your first and last name is: </h1>
    <%
        /*String firstName = (String)request.getAttribute("firstname");
        String lastName = (String)request.getAttribute("lastname");*/
        String firstName = request.getParameter("firstname");
        String lastName = request.getParameter("lastname");

        out.print(firstName + " " + lastName);
    %>

</body>
</html>

我了解到,当加载Web应用程序时,servlet容器将创建ServletContext一次并保存在服务器的内存中。ServletContext是适用于整个Web应用程序的属性和配置的集合

按照上面的servletexample,request.setattribute用于创建新属性。

1)

这些属性存储在ServletContext吗?

2)

存储在其中的那些属性和配置是ServletContext什么?


阅读 338

收藏
2020-06-10

共1个答案

一尘不染

有三个范围:

  1. 应用范围(即SevletContext)
  2. 会话范围(即HttpSession)
  3. 请求范围(即HttpServletRequest)

获取ServletContext对象:

  1. getServletContext()。set attribute(“ name”,“ value”); //现在可以从应用程序内的任何Servlet访问name属性。

获取HttpSession对象:

  1. request.getSession(true).set attribute(“ name2”,“ value”); //现在可以从当前会话访问name2属性

  2. request.set attribute(“ name3”,“ value”);

//现在,在将响应发送回客户端之前,可以在Servlet或jsp中的任何位置访问name3属性。

问题:属性存储在哪里?

回答:属性存储在相应范围的Map(名称/值对)中。即会话映射,请求映射和ServletContext映射。

2020-06-10