?
Solved

sharing request-scoped beans

Posted on 2005-03-20
3
Medium Priority
?
277 Views
Last Modified: 2011-09-20
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;
   }
}
++++++++
0
Comment
Question by:dkim18
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 29

Accepted Solution

by:
bloodredsun earned 600 total points
ID: 13589504
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
 

Author Comment

by:dkim18
ID: 13592829
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
 

Author Comment

by:dkim18
ID: 13594950
hm...never mind it worked in school server..but not in my computer...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Learn how to use the free Acronis True Image app to easily transfer data between iPhones and Android phones.
This month, Experts Exchange’s free Course of the Month is focused on CompTIA IT Fundamentals.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question