Skip to content

Instantly share code, notes, and snippets.

@amirdt22
Created April 24, 2014 17:40
Show Gist options
  • Save amirdt22/11263087 to your computer and use it in GitHub Desktop.
Save amirdt22/11263087 to your computer and use it in GitHub Desktop.
CT390 Lab3 Submission #4
package edu.drexel.goodwin.cst.ct390.lab2;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@SuppressWarnings("serial")
@WebServlet("/HelloName")
public class HelloName extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
handlePostOrGet(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
handlePostOrGet(request, response);
}
private void handlePostOrGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
if (request.getParameter("name") == null) {
response.getWriter().println("hello world");
}
else {
response.getWriter().println("hello " + request.getParameter("name"));
}
HttpSession session = request.getSession();
session.setAttribute("name", request.getParameter("name"));
Cookie[] NumberCookies = request.getCookies();
if (NumberCookies.length < 1) {
response.setHeader("Cookie-Count", "Hi " + session.getAttribute("name") + " you have no cookies");
}
else {
response.setHeader("Cookie-Count", "Hi " + session.getAttribute("name") + " you have " + NumberCookies.length + " cookies");
}
}
}
@rwestergren
Copy link

Great job consolidating the functionality into one method. Good naming conventions and formatting as well.

Some minor improvements might be assigned variables instead of calling functions more than once. For example:

 String name = request.getParameter("name");

And then reusing that variable in other areas (the response). Also, instead of writing two different responses for the Cookie-Count header, you could've just written out the count (regardless if it was zero) which would have met the requirements.

@amirdt22
Copy link
Author

I'm not crazy about the NumberCookies variable name. Definitely do like that "handlePostOrGet" is factored out.

@dennisrhodes
Copy link

Once again looking at this one it made nice use of an extra function to handle the GET and POST's. I think that was really smart and probably a lot cleaner then my submission.

If I were this author I would think about including comments, (See Submission 1). Comments can REALLY help people overlooking your work see what your intention is. That way in your comments you can defend how you coded something the way you did, or have someone improve in an area you thought was well written.

Very good submission!

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