Skip to content

Instantly share code, notes, and snippets.

@amirdt22
Last active August 29, 2015 14:00
Show Gist options
  • Save amirdt22/11262658 to your computer and use it in GitHub Desktop.
Save amirdt22/11262658 to your computer and use it in GitHub Desktop.
CT390 Spring '14 Submission #1
package edu.drexel.goodwin.cst.ct390;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/HelloName")
public class HelloName extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
handleGreeting(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
handleGreeting(request, response);
}
/**
* Generic method to handle greetings
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
protected void handleGreeting(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO: Clean XSS-vulnerable parameters
HttpSession session = request.getSession();
// Handle cookie output
int cookieCount = request.getCookies().length;
response.addHeader("Cookie-Count", "Hi " + session.getAttribute("name") + " you have " + String.valueOf(cookieCount) + " cookie(s)");
// Get provided name
String name = request.getParameter("name");
if(name == null)
{
// No name set
response.getWriter().println("hello world");
}
else
{
// Set session attribute
session.setAttribute("name", name);
// Greet with name
response.getWriter().println("Greetings, " + name);
}
}
}
@ecamp340
Copy link

All variables and methods are comprehensible, properly named, and functional which allows this code to be well written. I do not see any obvious errors, correct me if I'm wrong..

The commentary provided throughout is helpful. It makes it easier for the reader to follow along step by step and get a better understanding of exactly what is happening on each line.

The overall code would be easy to update and maintain because it is organized without any excess code.

Great submission.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment