@@ -41,6 +41,9 @@ public class SessionExample extends HttpServlet {
4141
4242 private static final long serialVersionUID = 1L ;
4343
44+ private static final int SESSION_ATTRIBUTE_COUNT_LIMIT = 10 ;
45+
46+
4447 @ Override
4548 public void doGet (HttpServletRequest request , HttpServletResponse response ) throws IOException , ServletException {
4649 ResourceBundle rb = ResourceBundle .getBundle ("LocalStrings" , request .getLocale ());
@@ -76,15 +79,34 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
7679 out .println (rb .getString ("sessions.lastaccessed" ) + " " );
7780 out .println (new Date (session .getLastAccessedTime ()));
7881
82+ // Count the existing attributes
83+ int sessionAttributeCount = 0 ;
84+ Enumeration <String > names = session .getAttributeNames ();
85+ while (names .hasMoreElements ()) {
86+ names .nextElement ();
87+ sessionAttributeCount ++;
88+ }
89+
7990 String dataName = request .getParameter ("dataname" );
8091 String dataValue = request .getParameter ("datavalue" );
8192 if (dataName != null ) {
82- session .setAttribute (dataName , dataValue );
93+ if (dataValue == null ) {
94+ session .removeAttribute (dataName );
95+ sessionAttributeCount --;
96+ } else if (sessionAttributeCount < SESSION_ATTRIBUTE_COUNT_LIMIT ) {
97+ session .setAttribute (dataName , dataValue );
98+ sessionAttributeCount ++;
99+ } else {
100+ out .print ("<p> Session attribute [" );
101+ out .print (HTMLFilter .filter (dataName ));
102+ out .print ("] not added as there are already " + SESSION_ATTRIBUTE_COUNT_LIMIT + " attributes in the " );
103+ out .println ("session. Delete an attribute before adding another." );
104+ }
83105 }
84106
85- out .println ("<P >" );
107+ out .println ("<p >" );
86108 out .println (rb .getString ("sessions.data" ) + "<br>" );
87- Enumeration < String > names = session .getAttributeNames ();
109+ names = session .getAttributeNames ();
88110 while (names .hasMoreElements ()) {
89111 String name = names .nextElement ();
90112 String value = session .getAttribute (name ).toString ();
@@ -96,37 +118,41 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
96118 out .println ("<br>" );
97119 }
98120
99- out .println ("<P>" );
100- out .print ("<form action=\" " );
101- out .print (response .encodeURL ("SessionExample" ));
102- out .print ("\" " );
103- out .println ("method=POST>" );
104- out .println (rb .getString ("sessions.dataname" ));
105- out .println ("<input type=text size=20 name=dataname>" );
106- out .println ("<br>" );
107- out .println (rb .getString ("sessions.datavalue" ));
108- out .println ("<input type=text size=20 name=datavalue>" );
109- out .println ("<br>" );
110- out .println ("<input type=submit>" );
111- out .println ("</form>" );
112-
113- out .println ("<P>GET based form:<br>" );
114- out .print ("<form action=\" " );
115- out .print (response .encodeURL ("SessionExample" ));
116- out .print ("\" " );
117- out .println ("method=GET>" );
118- out .println (rb .getString ("sessions.dataname" ));
119- out .println ("<input type=text size=20 name=dataname>" );
120- out .println ("<br>" );
121- out .println (rb .getString ("sessions.datavalue" ));
122- out .println ("<input type=text size=20 name=datavalue>" );
123- out .println ("<br>" );
124- out .println ("<input type=submit>" );
125- out .println ("</form>" );
126-
127- out .print ("<p><a href=\" " );
128- out .print (HTMLFilter .filter (response .encodeURL ("SessionExample?dataname=exampleName&datavalue=exampleValue" )));
129- out .println ("\" >URL encoded </a>" );
121+ if (sessionAttributeCount < SESSION_ATTRIBUTE_COUNT_LIMIT ) {
122+ out .println ("<p>" );
123+ out .print ("<form action=\" " );
124+ out .print (response .encodeURL ("SessionExample" ));
125+ out .print ("\" " );
126+ out .println ("method=POST>" );
127+ out .println (rb .getString ("sessions.dataname" ));
128+ out .println ("<input type=text size=20 name=dataname>" );
129+ out .println ("<br>" );
130+ out .println (rb .getString ("sessions.datavalue" ));
131+ out .println ("<input type=text size=20 name=datavalue>" );
132+ out .println ("<br>" );
133+ out .println ("<input type=submit>" );
134+ out .println ("</form>" );
135+
136+ out .println ("<p>GET based form:<br>" );
137+ out .print ("<form action=\" " );
138+ out .print (response .encodeURL ("SessionExample" ));
139+ out .print ("\" " );
140+ out .println ("method=GET>" );
141+ out .println (rb .getString ("sessions.dataname" ));
142+ out .println ("<input type=text size=20 name=dataname>" );
143+ out .println ("<br>" );
144+ out .println (rb .getString ("sessions.datavalue" ));
145+ out .println ("<input type=text size=20 name=datavalue>" );
146+ out .println ("<br>" );
147+ out .println ("<input type=submit>" );
148+ out .println ("</form>" );
149+
150+ out .print ("<p><a href=\" " );
151+ out .print (HTMLFilter .filter (response .encodeURL ("SessionExample?dataname=exampleName&datavalue=exampleValue" )));
152+ out .println ("\" >URL encoded </a>" );
153+ } else {
154+ out .print ("<p>You may not add more than " + SESSION_ATTRIBUTE_COUNT_LIMIT + " attributes to this session." );
155+ }
130156
131157 out .println ("</body>" );
132158 out .println ("</html>" );
0 commit comments