Null pointer exception while using cookie to remember the previous color
I'm trying to write a simple program to have the background on a html page changed by selecting the colour on radio buttons, this I managed to do successfully. As soon as I add a cookie to remember the colour and load the same background the next time I get null pointer exceptions all over the place.
Code :
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ColourSelect extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
String pageColour = "Black";
String colour = req.getParameter("colour");
Cookie colourCookie = new Cookie("colour", colour);
if (colourCookie != null) {
res.addCookie(colourCookie);
colourCookie.setMaxAge(100000);
Cookie[] cookies = req.getCookies();
pageColour = getLastColour(cookies);
} else {
pageColour = colour;
}
res.setContentType("text/html");
ServletOutputStream out = res.getOutputStream();
out.println("<html>");
out.println("<body bgcolor=\"" + pageColour + "\">");
out.println("<font color=\"White\">");
out.println("<head><title>Change Colour</title></head>");
out.println("<h1><p>Background Colour</p></h1>");
out.println("<p>What colour do you want the background?</p>");
out.println("<form>");
out.println("Yellow</br><input type=\"radio\" name=\"colour\" value=\"Yellow\" /></br>");
out.println("Green</br><input type=\"radio\" name=\"colour\" value=\"Green\" /></br>");
out.println("Blue</br><input type=\"radio\" name=\"colour\" value=\"Blue\" /></br>");
out.println("Black</br><input type=\"radio\" name=\"colour\" value=\"Black\" /></br>");
out.println("<input type=submit value=\"Change Background\"/>");
out.println("</font>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
}
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
doPost(req, res);
}
String getLastColour(Cookie[] cookies){
String inColour="white";
for (int i = 0; i < cookies.length; i++) {
String cookieName = cookies[i].getName();
if (cookieName.equals("colour"))
inColour = cookies[i].getValue();
}
return inColour;
}
}
Can anyone help, I'm really stuck with this, no matter what I try it just doesn't seem to work.
Re: Struggling with cookies
OK I've solved the null pointer problem, if there are no cookies sent to the server it returns null so I added an if statement in the right place. Now however when I first set the colour it turns the background white no matter which colour I choose, then I select the next colour and it turns the background the colour of the first one I chose etc etc. Once that's done it's not remembering the colours when I reload it. Here's the code test it out and you'll see what I mean. I'll continue working away on this myself but would appreciate some help.
Code :
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ColourSelect extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
String pageColour;
String colour = req.getParameter("colour");
Cookie colourCookie = new Cookie("colour", colour);
res.addCookie(colourCookie);
colourCookie.setMaxAge(100000);
Cookie[] cookies = req.getCookies();
if (cookies != null) {
pageColour = getLastColour(cookies);
} else { pageColour = colour; }
res.setContentType("text/html");
ServletOutputStream out = res.getOutputStream();
out.println("<html>");
out.println("<body bgcolor=\"" + pageColour + "\">");
out.println("<font color=\"White\">");
out.println("<head><title>Change Colour</title></head>");
out.println("<h1><p>Background Colour</p></h1>");
out.println("<p>What colour do you want the background?</p>");
out.println("<form>");
out.println("Yellow</br><input type=\"radio\" name=\"colour\" value=\"Yellow\" /></br>");
out.println("Green</br><input type=\"radio\" name=\"colour\" value=\"Green\" /></br>");
out.println("Blue</br><input type=\"radio\" name=\"colour\" value=\"Blue\" /></br>");
out.println("Black</br><input type=\"radio\" name=\"colour\" value=\"Black\" /></br>");
out.println("<input type=submit value=\"Change Background\"/>");
out.println("</font>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
}
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
doPost(req, res);
}
String getLastColour(Cookie[] cookies){
String inColour="Black";
for (int i = 0; i < cookies.length; i++) {
String cookieName = cookies[i].getName();
if (cookieName.equals("colour"))
inColour = cookies[i].getValue();
}
return inColour;
}
}
Re: Struggling with cookies
Ah I see the problem now. Is there anyway of reloading a cookie only when the page is loaded and not everytime when the submit button is clicked?
Re: Struggling with cookies
OK it displays ok and changes colour ok it just doesn't do what it's supposed to do and remember the colour for the next time the page is loaded.
Code :
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ColourSelect extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
String pageColour;
String colour = req.getParameter("colour");
Cookie colourCookie = new Cookie("colour", colour);
colourCookie.setMaxAge(100000);
Cookie[] cookies = req.getCookies();
if (cookies != null) {
res.addCookie(colourCookie);
pageColour = getLastColour(cookies);
} else { pageColour = colour; }
res.setContentType("text/html");
ServletOutputStream out = res.getOutputStream();
out.println("<html>");
out.println("<body bgcolor=\"" + pageColour + "\">");
out.println("<font color=\"White\">");
out.println("<head><title>Change Colour</title></head>");
out.println("<h1><p>Background Colour</p></h1>");
out.println("<p>What colour do you want the background?</p>");
out.println("<form>");
out.println("Yellow</br><input type=\"radio\" name=\"colour\" value=\"Yellow\" /></br>");
out.println("Green</br><input type=\"radio\" name=\"colour\" value=\"Green\" /></br>");
out.println("Blue</br><input type=\"radio\" name=\"colour\" value=\"Blue\" /></br>");
out.println("Black</br><input type=\"radio\" name=\"colour\" value=\"Black\" /></br>");
out.println("<input type=submit value=\"Change Background\"/>");
out.println("</font>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
}
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
doPost(req, res);
}
String getLastColour(Cookie[] cookies){
String inColour="Black";
for (int i = 0; i < cookies.length; i++) {
String cookieName = cookies[i].getName();
if (cookieName.equals("colour"))
inColour = cookies[i].getValue();
}
return inColour;
}
}