由servlet设置的属性存储在哪里? [英] Where are the attributes stored, which are set by servlets?

查看:42
本文介绍了由servlet设置的属性存储在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的servlet:

Below is my 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应用程序的属性和配置的集合

I learnt that, When a webapplication gets loaded, the servletcontainer will create the ServletContext once and keep in server's memory. ServletContext is a collection of attributes and configurations that apply to the entirety of your web application

按照上述servlet示例,request.setattribute用于创建新属性.

As per above servletexample, request.setattribute is used to create a new attribute.

1)

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

Are these attributes stored in ServletContext?

2)

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

What are those attributes and configurations that is stored in ServletContext?

推荐答案

有三个范围:

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

获取ServletContext对象:

Getting ServletContext Object:

  1. getServletContext().set attribute("name","value"); //现在的名字 属性可以从应用程序内的任何Servlet进行访问.

  1. getServletContext().set attribute("name","value"); //now the name attribute will be accessible from any Servlet within the application.

获取HttpSession对象:

Getting HttpSession Object:

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

request.getSession(true).set attribute("name2","value"); //now the name2 attribute will be accessible from current session

request.set attribute("name3","value");

request.set attribute("name3","value");

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

//now the name3 attribute will be accessible anywhere in Servlet or jsp before sending response back to the client.

是:属性存储在哪里?

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

Ans: Attributes are stored in a Map(in name-value pair) of respective Scope. i.e. Session Map, Request Map and ServletContext Map.

这篇关于由servlet设置的属性存储在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆