Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

sharing request-scoped beans

Posted on 2005-03-20
3
Medium Priority
?
283 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
  • 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

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

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

Choosing the right mix of apps is very much necessary for CPAs for making the most of the latest technology through which they can boost their growth.
Ready to kick start your career in 2018? Add app developer skills to your resume. January’s Course of the Month features Android App Development training with hands-on learning.  Read on to learn why these skills are important.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Loops Section Overview
Suggested Courses

580 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