Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using NetBeans: 1. Create a web page (index.jsp) which contains options to show

ID: 3890112 • Letter: U

Question

Using NetBeans:

1. Create a web page (index.jsp) which contains options to show the list (just an ArrayList of Strings) or add a new email.

2. The first link calls a servlet which looks on the session object for an existing list. If there is none or it is empty, control is forward back to index.jsp. A nice addition to index.jsp would be the ability to display a message which is stored on the session as an attribute.

3. The link to add an email to the list brings us to the addUser.html, where a form is used to submit the new email to be added. The form posts the request parameters to a servlet (e.g. addUser.java).

4. The addUser retrieves the request parameter for the email. If the email string is null, control is forwarded back to the addUser.html page. The list of emails is retrieved from the session object. If the list is null, a new one is created. The email is added to the list, and the list is set as an attribute again on the session (effectively replacing any previous list object with the same name). Control is then forwarded to displayList.jsp.

5. Clicking on the link to add a new email brings us back to addUser.html, and we can thereby continue adding to the list.

lecalhast: 808 g Home-Reseanch Part... fip drect-Lund 27 Get the distribution list

Explanation / Answer

for this task you'll need to create four file
index.jsp, addUser.html, addUser.java and show.java.

Now, index.jsp will be your landing page. As you might already be knowing that the main URL request to the servers resposes back with the index page (in this case index.jsp). so you index page should display "Get" and "Add" options as below

index.jsp
<%
String result = (String)request.getAttribute("result");
if(result != null)
out.println(result + "<br/><br/><br/>");
%>
<a href="show">Get the distribution list</a>
<br/><br/>
<a href="addUser.html">Add an email to the list</a>

The request.getAttribute("result") will be used to display the message that tthe list is empty. We'll see later how.

Now, your show.java will be resposible to display the list stored in the session object. The main processRequesr() function will look like below:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
HttpSession session = request.getSession(false);
ArrayList<String> emails = (ArrayList<String>) session.getAttribute("emails");
if(emails == null || emails.size() == 0){
request.setAttribute("result", "The List is empty.");
request.getRequestDispatcher("index.jsp").forward(request, response);
}
else{
out.println("<h1>Distribution List</h1><br/><br/>");
for (int i = 0; i < emails.size(); i++) {
out.println(emails.get(i) + "<br/>");
}
out.println("<br/><br/><a href="addUser.html">Add an email to the list</a>");
}
}
}

So, here we'll try to get the list from the session and if it is empty, we'll simply forward to the index.jsp if not
we'll iterte throught the list and print it along with the option to add new emails.

Now, the third file addUser.html will be a simple HTML file with a form to add new user, and it will look like below:

<div>Add a new user to the distribution list</div>
<br/><br/>
<form action="addUser" method="post">
<input type="email" placeholder="email" name="email"/><br/>
<input type="submit" value="Submit" />
</form>

this one is pretty much self explanatory.

Now, the addUser.java, where we have our logic to add users to the session object. It' processRequest() will look like below:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
String email = request.getParameter("email");
if(email == null) {
request.getRequestDispatcher("addUser.html").forward(request, response);
}
else {
HttpSession session = request.getSession(true);
ArrayList<String> emails = (ArrayList<String>) session.getAttribute("emails");
if(emails == null || emails.size() == 0) {
emails = new ArrayList<>();
emails.add(request.getParameter("email"));
session.setAttribute("emails", emails);
}
else{
emails.add(request.getParameter("email"));
session.setAttribute("emails", emails);
}
}
request.getRequestDispatcher("show").forward(request, response);
}
}


we'll use request.getParameter("email"); to retrieve the email-id "posted" from the addUser.html page's form.
if it's empty forward to the same page again i.e. addUser.html.
If the posted data if not empty i.e. it contains an email address we'll get the session check if the email list in session is empty or not. if its empty we'll create a new list and add the email to it using emails.add(request.getParameter("email"));. if its not empty we'll just add the recieved email to the existing list.
After that forward the request to the show servlet, which will show the available list.