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

时间:2015-06-01 06:27:34

标签: java jsp servlets

下面是我的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应用程序时,servletcontainer将创建ServletContext一次并保留在服务器的内存中。 ServletContext是适用于整个Web应用程序的属性和配置的集合

根据上述servleteample,request.setattribute用于创建新属性。

1)

这些属性是否存储在ServletContext

2)

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

3 个答案:

答案 0 :(得分:2)

有三个范围:

  1. 应用范围(即SevletContext)
  2. 会话范围(即HttpSession)
  3. 请求范围(即HttpServletRequest)
  4. 获取ServletContext对象:

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

      获取HttpSession对象:

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

    3. 访问name2属性
    4. request.set属性(“name3”,“value”);

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

    5. 问题:存储属性的位置?

      Ans:属性存储在各个Scope的Map(名称 - 值对)中。      即会话映射,请求映射和ServletContext映射。

答案 1 :(得分:1)

ServletContext是一个包含有关Web应用程序的元信息的对象。它将包含您设置的属性。您可以通过HttpRequest对象访问它,如下所示:

ServletContext context = request.getSession().getServletContext();

您可以设置请求的属性(根据您的示例),响应和会话对象。您可以直接将其设置为上下文。

context.setAttribute("someValue", "aValue");

可以像

一样检索它
Object attribute = context.getAttribute("someValue");

存储在ServletContext中的属性可供应用程序中的所有servlet以及请求和会话之间使用。这意味着,该属性可供Web应用程序的所有访问者使用。会话属性仅供单个用户使用。

ServletContext属性仍存储在servlet容器的内存中。这意味着存在与服务器群集中的会话属性相同的问题。

答案 2 :(得分:1)

java中的Web应用程序有几个范围Request,Session,Global(ServletContext)

request.getAttribute("firstname");
request.getAttribute("lastname");
  

这些属性是否存储在ServletContext中?

firstname&amp; lastname存储在仅适用于该特定请求的请求范围中。

  

存储的属性和配置是什么?   ServletContext的?

任何旨在在所有servlet之间共享的信息都存储在ServletContext中。例如,您正在开发一个Web应用程序,您希望在整个应用程序中访问管理员的电子邮件。您希望将此电子邮件ID设置为@一个位置,并且每个Servlet和Jsp都可以访问该电子邮件。在这种情况下,您将获得ServletContext的帮助。您可以通过web.xml中的init参数在servletcontext中添加此电子邮件。现在,该值(管理员的电子邮件ID)将可用于Web应用程序中的每个Jsp和Servlet。

相关问题