• Post Reply Bookmark Topic Watch Topic
  • New Topic

jsp:getProperty Bean Problem.

 
Melanie Miller
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to use getProperty to get data out of my Bean and populate
a field on the FORM upon an error condition from my Servlet. I use a redirect to get back to the form which works great but my data isn't
populated back in the fields on the form. Please send help! What am
I doing wrong?


<html>
<head>
<title>Client Login Project</title>
</head>

<body bgcolor="#FDF5E6" text="black" link="blue" alink="purple" vlink="red">
<h1 align="center">Registration</h1>

<%-- The JSP page directive --%>
<%@ page import="java.util.*, Homework4.*" %>

<%-- JSP Scriptlet --%>
<% // Add Expiration header, so that a new request is made
// everytime a browser Refresh occurs.
long lastweek = (new java.util.Date()).getTime() - 604800000L;
response.setDateHeader("Expires", lastweek);

// Prevent Web Cache servers from caching document.
// The Expires header probably makes this unnecessary.
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
%>

<%!
private String printHeading(HttpServletRequest request) {
String registrationStatus =
request.getParameter(RegistrationServlet.REGISTRATION_STATUS);
String cmd = "";
String greeting = "";

if (registrationStatus != null) {
if (registrationStatus.equals(RegistrationServlet.REGISTRATION_SUCCESS)) {
greeting = "Please complete all fields.";
} else if (registrationStatus.equals(RegistrationServlet.REGISTRATION_FAIL_DUPLICATE)) {
greeting = "duplicate";
} else if (registrationStatus.equals(RegistrationServlet.REGISTRATION_FAIL)) {
greeting = "Error in registration process." +
"<BR>Please try again.";
}
} else {
greeting = "Welcome. Please complete all fields.";
}

cmd = "<H2 ALIGN=\"center\"><FONT COLOR=\"RED\">" + greeting + "</FONT></H2>";
return(cmd);
}
%>

<jsp:useBean id="UR"
class="Homework4.InputRecord"/>
<jsp:setProperty name="UR" property="*" />

<%= printHeading(request) %><br>

<form action="servlet/Homework4.RegistrationServlet" method="post">
<center>

<b>Login ID:</b>
<input type="text" name="userID" value ="<jsp:getProperty name="UR" property="userID" />" />

<b>Password:</b>
<input type="text" name="password" value=""><br><br>
<jsp:getProperty name="UR" property="password" /></input>

<b>Name:</b>
<input type="text" name="name">

<b>Address:</b>
<input type="text" name="address" value=""><br><br>

<b>City:</b>
<input type="text" name="city" value="">

<b>State:</b>
<select name="state">
<option value="MD">Maryland
<option value="VA">Virginia
<option value="DC">District of Columbia
<option value="PA">Pennsylvania
</select>

<b>Zipcode:</b>
<input type="text" name="zipcode" value=""><br><br>

<b>Email Address:</b>
<input type="text" name="email" value="">

<b>Home Phone Number:</b>
<input type="text" name="home" value=""><br><br>

<input type="SUBMIT" value="Registrate">
</center>
</form>
</body>
</html>
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use a redirect to get back to the form


The redirect is most likely the cause of your problem.

I didn't read all the unformatted code in your post (aside #1: use the UBB code tags to preserve formatting when posting code) (aside #2: you might want to rethink the amount of Java you have on your JSP -- less is better -- none is best), but it's most liklely that your bean has gone out of scope. Inspect the lifetime of your bean to see if a redirect is really what you need.
 
Melanie Miller
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IN my RegistrationServlet I have:



This will take me back to the page with the address of: http://localhost/millerm/Registration.jsp?status=fail

The Registration.jsp page has...I have tired all different
scopes on the bean but they all don't work. The redirect
I used above the instructor used in his last example...Why won't
a redirect work with a bean?


[ October 25, 2004: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added UBB code tags to your post to show you their effect. Please use them on future posts.

A redirect causes a new request to be initiated. Beans in request or page scope will not survive the transition since the corresponding request and page contexts go out of scope when the request ends.

Rather than posting code, describe the sequence of events that occur. Here is a typical sequence (determine how your sequence differs):

1) The form is presented to the user on a JSP page.
2) The form page is submitted to a servlet for processing. Note that the request that resulted in the form display is long gone before the form submission occurs.
3) The processing servlet validates the form data.
4) If the data is in error, the servlet forwards back to the form display page. Note that in this case, the submitting request and displaying request are the same. If a redirect is performed, a new request is initiated for the display request.
5) If the form data is ok, the data is processed and then the servlet either redirects or forwards to the next resource as appropriate.
[ October 25, 2004: Message edited by: Bear Bibeault ]
 
Melanie Miller
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


What do you think if I turn the redirect into this instead?
Will I have the same problem with the bean going out of scope?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is your bean created in the process and what context is it placed onto?
 
Sonny Gill
Ranch Hand
Posts: 1211
IntelliJ IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Melanie,

I didnt read all of your code, but it seems that you are not specifying a scope for your bean, in which case it is set to 'page' AFAIK.



This could be the cause of some of your problems, you may need to set the scope to request if appropriate.

If you dont fully understand what scope means and how the bean related tags are used, I suggest you go through some of the JSP articles and tutorials available online. Sun JSP page is a good starting point.

Moreover, as Bear suggested, try to structure your code better. That will help you narrow down the problem to the most likely cause of it, and more people would be willing to go through your code to help you.

As a starting point, I suggest something like the following -

1. A request is sent to a Servlet with some 'id' for the bean to be modified. The servlet initializes the bean, and sets it as a request attribute.

2. A JSP that receives the bean (make sure that the 'id' mathces the one used to set the request attribute in the servlet, and the scope in useBean tag is request) and uses that bean to populate the form fields. The 'action' for the form is a servlet (which could be the same as the one above or a different servlet). There should be no other java code on this JSP.

3. A servlet receives the submitted form and does whatever processing is needed.

HTH
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!