Go Back   Java Programming Forums > Java EE (Enterprise Edition) > Java Servlet


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 26-06-2009, 12:53 PM
Junior Member
 

Join Date: Jun 2009
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
oss_2008 is on a distinguished road
Default Struggling with cookies

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.

Java 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.



Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 26-06-2009, 03:47 PM
Junior Member
 

Join Date: Jun 2009
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
oss_2008 is on a distinguished road
Default 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.

Java 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;
			}
}
Reply With Quote
  #3 (permalink)  
Old 26-06-2009, 03:51 PM
Junior Member
 

Join Date: Jun 2009
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
oss_2008 is on a distinguished road
Default 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?
Reply With Quote
  #4 (permalink)  
Old 26-06-2009, 04:06 PM
Junior Member
 

Join Date: Jun 2009
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
oss_2008 is on a distinguished road
Default 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.

Java 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;
			}
}
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
New to Java/struggling with arrays Fendaril Collections and Generics 18 13-11-2008 12:31 PM


100 most searched terms
Search Cloud
2d arraylist java actionlistener actionlistener in java actionlistener java addactionlistener addactionlistener in java addactionlistener java applications of oops could not create java virtual machine xp double format java double to int double to int java double to integer in java double to integer java eclipse shortcut keys eclipse tutorial for beginners exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double java get mouse position java java 2d arraylist java actionlistener java addactionlistener java convert list to map java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming help java sendkeys java two dimensional arraylist java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics two dimensional arraylist java writing ipod apps

All times are GMT. The time now is 09:21 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.