Skip to content

Instantly share code, notes, and snippets.

@darbyluv2code
Created August 24, 2017 15:12
Show Gist options
  • Select an option

  • Save darbyluv2code/67036024a882ee182b5712c76e72033c to your computer and use it in GitHub Desktop.

Select an option

Save darbyluv2code/67036024a882ee182b5712c76e72033c to your computer and use it in GitHub Desktop.
JSP course - session demo
<%@ page import="java.util.*" %>
<html>
<body>
<!-- Step 1: Create HTML form -->
<form action="todo-demo.jsp">
Add new item: <input type="text" name="theItem" />
<input type="submit" value="Submit" />
</form>
<!-- Step 2: Add new item to "To Do" list -->
<%
// get the TO DO items from the session
List<String> items = (List<String>) session.getAttribute("myToDoList");
// if the TO DO items doesn't exist, then create a new one
if (items == null) {
items = new ArrayList<String>();
session.setAttribute("myToDoList", items);
}
// see if there is form data to add
String theItem = request.getParameter("theItem");
if (theItem != null) {
items.add(theItem);
}
%>
<!-- Step 3: Display all "To Do" item from session -->
<hr>
<b>To List Items:</b> <br/>
<ol>
<%
for (String temp : items) {
out.println("<li>" + temp + "</li>");
}
%>
</ol>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment