sharing request-scoped beans

hi!

I made a “color preference” form that collects the user’s preferred foreground and background colors and send the data to a JSP page that displays come message using those colors. if the user fails to supply either of the colors, use whatever value they gave last time. If there was no previous value, then use a default.
So, if the user goes directly to the JSP page (bypassing the form), the JSP page should still work fine.

Is there any way of setting default color in smart way in the following code? Is this question related to sharing request-scoped beans?

Thanks,
+++++++++++++++
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> Color Preference 5-HW#6</TITLE>

</HEAD>
<jsp:useBean id="color" class="coreservlets.ColorBean" />
<jsp:setProperty
     name="color"
       property="bgColor"
       value='<%= request.getParameter("bgColor") %>' />

<jsp:setProperty
     name="color"
       property="fgColor"
       value='<%= request.getParameter("fgColor") %>' />

<BODY BGCOLOR="bgColor" TEXT="fgColor">
<CENTER>
<H1> Color Preference 5 </H1>

<jsp:getProperty name="color" property="bgColor" />
<jsp:getProperty name="color" property="fgColor" />

</CENTER>
</FORM>
</BODY>
</HTML>
+++++++++++++
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> Color Preference Form-HW#6</TITLE>

</HEAD>

<BODY>
<CENTER>
<H1> Color Preference Form </H1>

<FORM ACTION="ColorPreference5.jsp">
   Background: <INPUT TYPE="TEXT" NAME="bgColor"><BR><BR>
   Foreground: <INPUT TYPE="TEXT" NAME="fgColor"><BR><BR>
   <INPUT TYPE="SUBMIT" VALUE="Submit">
</CENTER>
</FORM>
</BODY>
</HTML>
+++++++++++
package coreservlets;

public class ColorBean{

   private String fgColor="black";
   private String bgColor="white";

   public String getfgColor(){
         return (fgColor);
   }

   public void setfgColor(String fgColor){
         this.fgColor=fgColor;
   }

   public String getbgColor(){
         return (bgColor);
   }

   public void setbgColor(String bgColor){
         this.bgColor=bgColor;
   }
}
++++++++
dkim18Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bloodredsunCommented:
Just do a null check on the request.getParameter() call

e.g.
-------------
<%
String pForegroundColor = request.getParameter("fgColor") ;
if ( pForegroundColor  == null ){ //if it's null
    pForegroundColor  = "red" ;//a default value
}
%>

<jsp:setProperty
     name="color"
      property="fgColor"
      value='<%= pForegroundColor %>' />
---------------

Remember, to use this method you need to first do a check for the bean being present, otherwise you are going to overwrite the last selected value if the user has not selected a color this time but has in the past.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dkim18Author Commented:
thanks for your help.
when a user fails to supply either of color. it doesn't use previoulsy used color. I put scope="session"
+++++++++++++++++++++

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> Color Preference 5-HW#6</TITLE>

</HEAD>
<jsp:useBean id="color" class="coreservlets.ColorBean" scope="session" />

<%
String ForegroundColor = request.getParameter("fgColor") ;
String BackgroundColor = request.getParameter("bgColor") ;

if((BackgroundColor == null) || (BackgroundColor.trim().equals(""))){
    BackgroundColor  = "white" ;//a default value
}
if((ForegroundColor == null) || (ForegroundColor.trim().equals(""))){
    ForegroundColor  = "black" ;//a default value
}
%>

<jsp:setProperty
     name="color"
       property="bgColor"
       value='<%= BackgroundColor %>' />

<jsp:setProperty
     name="color"
       property="fgColor"
       value='<%= ForegroundColor %>' />

<BODY BGCOLOR="<%= BackgroundColor %>" TEXT="<%= ForegroundColor %>">
<CENTER>
<H1> Color Preference 5 </H1>
<%= BackgroundColor %>
<%= ForegroundColor %>

</CENTER>
</FORM>
</BODY>
</HTML>
0
dkim18Author Commented:
hm...never mind it worked in school server..but not in my computer...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.