java - Where are the attributes stored, which are set by servlets? -


below 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);     }  } 

below 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> 

below 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 , 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> 

i learnt that, when webapplication gets loaded, servletcontainer create servletcontext once , keep in server's memory. servletcontext collection of attributes , configurations apply entirety of web application

as per above servletexample, request.setattribute used create new attribute.

1)

are these attributes stored in servletcontext?

2)

what attributes , configurations stored in servletcontext?

there 3 scopes:

  1. application scope (i.e. sevletcontext)
  2. session scope (i.e. httpsession)
  3. request scope (i.e. httpservletrequest)

getting servletcontext object:

  1. getservletcontext().set attribute("name","value"); //now name attribute accessible servlet within application.

    getting httpsession object:

  2. request.getsession(true).set attribute("name2","value"); //now name2 attribute accessible current session

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

    //now name3 attribute accessible anywhere in servlet or jsp before sending response client.

ques:where attributes stored?

ans: attributes stored in map(in name-value pair) of respective scope. i.e. session map, request map , servletcontext map.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -