[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 937
  • Last Modified:

jsp:forward and bean jsp:setProperties

I am trying to set all properties to the bean on one page, where they are entered then forward it to another page, which confirms set data and then make a link to a servlet which will save them to the database.

Can someone help me and let me know if there is a special way to do it?

The form is redirected to itself, but where do I add the forward tag?
<jsp:useBean id="customer" class="business.Customer" scope="session" />      
<jsp:setProperty name="customer" property="*"/>

Can I use something like:
<%
 if ( request.getParameter("button name") != null )
 {
 <jsp:forward page="/application/Confirmation.jsp" />
 return;
  }
%>
0
anna_suchodolska
Asked:
anna_suchodolska
  • 14
  • 11
  • 5
1 Solution
 
copyPasteGhostCommented:
you would want to do this...

<%
 if ( request.getParameter("button name") != null )
 {
%>
 <jsp:forward page="/application/Confirmation.jsp" />
 <%
  }
%>
Cheers,
Ghost
0
 
cpuburstCommented:
Hi,

Your form can be submitted to a processing jsp which actually sets all the values.
if validation is ok, then you can forward it your servlet from here. If failed, then forward it back to the first jsp.

This way you need to place your forwarding code in the processing jsp.
( Personally I prefer servlets to do the form processing part)

Cheers
Cpuburst
0
 
anna_suchodolskaAuthor Commented:
what is processing jsp? why would I need it if I have

I want to use set * properties:
<jsp:useBean id="employee" class="business.Employee" scope="session" />      
<jsp:setProperty name="employee" property="*"/>
</jsp:useBean>

I thought that you can set all properties to th ebean on one jsp, then forward it either to another jsp or a servlet

By the way, what likn should I use in my form? That's the biggest question.
I have been told that the form should be pointing to itself, but... I am not sure about it.

Please help.

P.s. thank you ghost, I'll try it as soon as I figure out how my form tag should look like.
Cheers.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
copyPasteGhostCommented:
you can have it post to the same page
that way you will not have to use "processing.jsp"

what you do is have this sort of thing
<%
String posted = request.getParameter("NAME OF YOUR SUBMIT BUTTON") {

if(posted != null){
<jsp:useBean id="employee" class="business.Employee" scope="session" />    
<jsp:setProperty name="employee" property="*"/>
</jsp:useBean>
}
else{
%>
Display the Form in html here
ex <form></form>
<%
}
%>

Hope that helps
Ghost
0
 
cpuburstCommented:

There are lots of ways you can do request processing.

A good way is to have a jsp collect user information via a form and when the user submits the form,
a request handler like servlet takes up the processing of the form data submitted. This handler can be a servlet also,
as it is a server side component. To use <jsp:setProperty >, you should have a jsp to handle your request.
I understood you are submitting the form to itself. This way is ok, but not the best way to do form processing.

You can move your request processing code either to a servlet/jsp. Just change your action attribute on the form to point to the request handler.

Which link are you talking about? Clarify

Cheers
Cpuburst
0
 
anna_suchodolskaAuthor Commented:
my form is pointing to itself and methd="post", is it ok?

<form name="tableform" action="/application/EmployeesData.jsp" method="post">
0
 
anna_suchodolskaAuthor Commented:
I am getting these errors

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 27 in the jsp file: /EmployeesData.jsp

Generated servlet error:
    [javac] Compiling 1 source file

C:\tomcat\tomcat-4-1-29\work\Standalone\localhost\TaxiGlobe\EmployeesData_jsp.java:166: illegal start of expression
                  
                ^



An error occurred at line: 27 in the jsp file: /EmployeesData.jsp

Generated servlet error:
C:\tomcat\tomcat-4-1-29\work\Standalone\localhost\TaxiGlobe\EmployeesData_jsp.java:166: ';' expected
                  
                                           ^



An error occurred at line: 27 in the jsp file: /EmployeesData.jsp

Generated servlet error:
C:\tomcat\tomcat-4-1-29\work\Standalone\localhost\TaxiGlobe\EmployeesData_jsp.java:166:  expected
                  
                                                ^



An error occurred at line: 27 in the jsp file: /EmployeesData.jsp

Generated servlet error:
C:\tomcat\tomcat-4-1-29\work\Standalone\localhost\TaxiGlobe\EmployeesData_jsp.java:169: '{' expected
^



An error occurred at line: 27 in the jsp file: /EmployeesData.jsp

Generated servlet error:
C:\tomcat\tomcat-4-1-29\work\Standalone\localhost\TaxiGlobe\EmployeesData_jsp.java:171: 'else' without 'if'
            else {
                ^



An error occurred at line: -1 in the jsp file: null

Generated servlet error:
C:\tomcat\tomcat-4-1-29\work\Standalone\localhost\TaxiGlobe\EmployeesData_jsp.java:373: 'catch' without 'try'
    } catch (Throwable t) {
      ^
C:\tomcat\tomcat-4-1-29\work\Standalone\localhost\TaxiGlobe\EmployeesData_jsp.java:39: 'try' without 'catch' or 'finally'
    try {
    ^
C:\tomcat\tomcat-4-1-29\work\Standalone\localhost\TaxiGlobe\EmployeesData_jsp.java:383: '}' expected
^
8 errors

What's wrong?
0
 
anna_suchodolskaAuthor Commented:
<%       

      String posted = request.getParameter("SubmitForm");
      
      
      if(posted != null) {

            out.println("<jsp:useBean id=\"employee\" class=\"business.Employee\" scope=\"session\" />");      
            out.println("<jsp:setProperty name=\"employee\" property=\"*\"/>");      
            out.println("</jsp:useBean>");
      
            } else {
%>            
< form>
.
.
.
.</form>

<% } %>      

Now it compiled, but I am still checking...
0
 
cpuburstCommented:
This means you have missed some brace.
Go to tomcat work directory and find your application cache.
see EmployeesData_jsp.java file and go to line number 166.
See what is the error there.
 
0
 
anna_suchodolskaAuthor Commented:
2004-04-21 13:30:31 StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
org.apache.jasper.JasperException: Attempted a bean operation on a null object.


What is happening? Can you help?
Some thing maybe unneccesary, please let me know if there are any.

Anna

<%@ page session = "true"
      import="java.util.*, java.sql.*,
            java.lang.Boolean, java.lang.String"%>
<html>

<head>
<title>Taxi Globe</title>
<link rel=stylesheet href="TaxiGlobe.css" type="text/css">
</head>

<body>




<table border="0" width="100%">
      <tr>
            <td colspan="2"><jsp:include page="Titlebar.jsp" flush="true" /></td>
      </tr>
      <tr>
            <td valign="top" width="15%"><%@ include file="Sidebar.jsp" %></td>
            
            <td valign="top" >
            <div align="center"><h2>Create New Employee Entry</h2></div>
            
            <div align="center">
            
<%       

      String posted = request.getParameter("SubmitForm");
      
      
      if(posted != null) {

            out.println("<jsp:useBean id=\"employee\" class=\"business.Employee\" scope=\"session\" />");      
            out.println("<jsp:setProperty name=\"employee\" property=\"*\"/>");      
            out.println("</jsp:useBean>");
      
            } else {
%>            
            
            <form name="tableform" action="/TaxiGlobe/DisplayEmployee.jsp" method="post">
            <fieldset style="padding: 2; width:568px; height:325px">
            <br>
            <table border="0" width="90%" align=center>
            <tr>
                  <td colspan="2" align=center><font color="red"><b>Required fields *</b></font></td>
            </tr>
            <tr>
                  <td >&nbsp;</td>
            </tr>

            <tr>
                  <td><b>First name</b></td>
                  <td><input type="text" name="firstName" size="20"
                        value="<jsp:getProperty name ="employee" property="firstName"/>">
                        <font color="red">* </font>
                  </td>
            </tr>
            <tr>
                  <td><b>Surname</b></td>
                  <td><input type="text" name="surname" size="40"
                        value="<jsp:getProperty name ="employee" property="surname"/>">
                        <font color="red">* </font>
                  </td>
            </tr>
            <tr>
                  <td><b>Full address</b></td>
                  <td><input type="text" name="address" size="60"
                        value="<jsp:getProperty name ="employee" property="address"/>">
                        <font color="red">* </font>
                  </td>
            </tr>
            <tr>
                  <td><b>Date of birth </b></td>
                  <td><input type="text" name="dateOfBirth" size="10"
                  value="<jsp:getProperty name ="employee" property="dateOfBirth"/>"
                  onKeypress="if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = false;">
                  <font color="red">* (dd/mm/yyyy)</font>
                  </td>
            </tr>
            <tr>
                  <td><b>Start date </b></td>
                  <td><input type="text" name="emplStartDate" size="10"
                  value="<jsp:getProperty name ="employee" property="emplStartDate"/>"
                  onKeypress="if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = false;">
                  <font color="red">* (dd/mm/yyyy)</font>
            </td>
            </tr>
            <tr>
                  <td><b>End date </b></td>
                  <td><input type="text" name="emplEndDate" size="10"
                  value="<jsp:getProperty name ="employee" property="emplEndDate"/>"
                  onKeypress="if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = false;">
                  <font color="black"> (dd/mm/yyyy)</font>
                  </td>
            </tr>

            <tr>
                  <td><b>Home phone</b></td>
                  <td><input type="text" name="homePhone" size="20"
                  value="<jsp:getProperty name ="employee" property="homePhone"/>"
                  onKeypress="if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = false;"></td>
            </tr>
            <tr>
                  <td><b>Mobile phone</b></td>
                  <td><input type="text" name="mobilePhone" size="20"
                  value="<jsp:getProperty name ="employee" property="mobilePhone"/>"
                  onKeypress="if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = false;">
                  <font color="red">*</font>
                  </td>
            </tr>
            <tr>
                  <td><b>Email address</b></td>
                  <td><input type="text" name="email" size="40"
                  value="<jsp:getProperty name ="employee" property="email"/>">
                  <font color="red">*</font>
            </td>
            </tr>
            <tr>
                  <td><b>User name</b></td>
                  <td><input type="text" name="userName" size="20"
                  value="<jsp:getProperty name ="employee" property="userName"/>">
                  <font color="red">*</font>
                  </td>
            </tr>
            <tr>
                  <td><b>Password</b></td>
                  <td><input type="text" name="password" size="20"
                  value="<jsp:getProperty name ="employee" property="password"/>">
                  <font color="red">*</font>
            </td>
            </tr>
            <tr>
                  <td><b>Role</b></td>
                  <td><select name="role" value="<jsp:getProperty name ="employee" property="role"/>">
                        <option value="Sales Person">Sales Person
                    <option value="Admin">Admin
                    </select><font color="red">*</font>
                  </td>
            </tr>
            <tr>
                  <td>&nbsp;</td>
            </tr>

      </table>      
            </fieldset>
            
            <br>
            <br>
            
            <input type="submit" name="SubmitForm" value="Save Details" style="width: 87; height: 26">
            <input  type="reset" value="Reset" style="width: 87; height: 26">
            
            </form>
            
                  

<% } %>            

            <a href="/TaxiGlobe/Home.jsp">Back</a>
                  
                  
      
            </div>
            </td>
      </tr>
</table>
</body>

</html>
0
 
anna_suchodolskaAuthor Commented:
Sorry for being so unpatient but I am running out of time :)

I can see, that in this way the values in the form are not using bean, I bet it is not what you thought.

I want a user to enter these data, bean - to get them and set them,  and then I would like them to be displayed on this or on the next  page.

Please, help me if you can.  Let me know if you can't, so I could try something else.

Cheers
0
 
cpuburstCommented:
<jsp:useBean> is a action element in jsp and it should be processed in the server.
So outputting it to the client with out.println() is wrong. Use it like the following

<%
     if(posted != null)
     {
%>
         <jsp:useBean id="employee" class="business.Employee" scope="session" />    
                      <jsp:setProperty name="employee" property="*"/>
         </jsp:useBean>

<%
     }
%>
0
 
copyPasteGhostCommented:
shouldn't it be

<%
     if(posted != null)
     {
%>
         <jsp:useBean id="employee" class="business.Employee" scope="session" />    
         <jsp:setProperty name="employee" property="*"/>
         
<%
     }
%>

 :) you are closing the <jsp:useBean> tag twice
Cheers,
Ghost
0
 
cpuburstCommented:

Correct.. it shouldn't have. Copy paste error. I got it from what anna had posted.

Cheers
Cpuburst
0
 
copyPasteGhostCommented:
i firgured. it's all good,
I hope that works :S
Ghost
0
 
anna_suchodolskaAuthor Commented:
Thanks

one more thhing which I am thinking about is:

I believe that bean should be created before the form
after the form I should have the code with String post ...and if statement
then <jsp:setProperty name="employee" property="*"/>      
then display confirmation like
First name: <jsp:getProperty name ="employee" property="firstName"/>
then redirect to another page

Is it wrong thinking, it seems to be very logical...
0
 
copyPasteGhostCommented:
it doesn't matter.

cause you are breaking the page into two parts..
when you are using the page as a form
the code for the bean will not be executed
and when you are using the page for the bean the code for the form will not be executed.
does that make sense?

<%
     if(posted != null)
     {
%>
        IF THE PAGE HAS BEEN POSTED        
<%
     }
     else {
%>
     IF THE PAGE HAS NOT BEEN POSTED
<%
     }
%>

hope that helps,
Ghost
0
 
copyPasteGhostCommented:
I think I understand what you are saying.
<%
     if(posted != null)
     {
%>
        IF THE PAGE HAS BEEN POSTED        
        Declare the bean
        Set the bean
        Use the bean
        HERE IS WHERE YOU WOULD ADD ALL THE
        First name: <jsp:getProperty name ="employee" property="firstName"/>
<%
     }

Hope that's what you wanted,
Ghost
0
 
anna_suchodolskaAuthor Commented:
I also need to know what to do with form action, I got a bit confused, but not for long...
and where can I place redirect code

That's what I have done:

1)before the form

<%       
      String posted = request.getParameter("SubmitForm");
%>
< form></form>

2) after the form
<%
      if(posted != null)
      {
%>            
    <jsp:useBean id="employee" class="business.Employee" scope="session" />  
    <jsp:setProperty name="employee" property="*"/>

   First name: <jsp:getProperty name ="employee" property="firstName"/>  //for now
      
<% } else { %>            

      <a href="/TaxiGlobe/Home.jsp">Back</a>  //for now
                  
<% } %>            

it returns to the same      page but with First name empty ...
why it is not reading the bean?

thank you in advance
0
 
anna_suchodolskaAuthor Commented:
Yes!

how do I deal with else, do I need it at all
0
 
copyPasteGhostCommented:
think of it this way...

the else is the first time you come to this page.

So in the else you will have the form letting the user enter all their information and then when they click on submit they will get sent back to this page will all the information read in from the bean. Because when you click submit the IF part of the page will be executing. What we are doing is combining two jsp pages into one.
clearer?
Ghost
0
 
anna_suchodolskaAuthor Commented:
It is getting clearer

Let me show you what I have now
This is the beginning of my page

<%       
      String posted = request.getParameter("SubmitForm");
      
      if(posted != null)
      {
%>            
    <jsp:useBean id="employee" class="business.Employee" scope="session" />  
    <jsp:setProperty name="employee" property="*"/>
      First name: <jsp:getProperty name ="employee" property="firstName"/>
      
<% } else { %>            

      <form name="tableform" action="/TaxiGlobe/DisplayEmployee.jsp" method="post">
...
<tr>
<td><b>First name</b></td>
<td><input type="text" name="firstName" size="20"
      value="<jsp:getProperty name ="employee" property="firstName"/>">
         <font color="red">* </font>
</td>
</tr>
...
</form>

<% } %>            

but First name from
 First name: <jsp:getProperty name ="employee" property="firstName"/>
still remains empty...?
can it be the case  that the bean can not have properties set and display them straight after that?

well, even worse, I just restarted tomcat... it is not happening at all, I got a blank page

do you think that getting data from the bean on the following page would be a better practise?

and why, when I am selecting the link to this page I am getting http://localhost/TaxiGlobe/EmployeesData.jsp? <--- what does this question mark mean?

0
 
anna_suchodolskaAuthor Commented:
----- Root Cause -----
java.lang.NullPointerException

Ghost? Do you know why did it happen?

And please, please, let me know if you can't sort it out for any reason.
Don't leave me without an answer.  I will be very grateful.

In my table with entry boxes I still have the form tag, and the form is pointing to the same page, is it ok?
0
 
copyPasteGhostCommented:
the ? means that you are writing parameters to the url don't worry about that.

I think you are getting a null pointer cause

String posted = request.getParameter("SubmitForm");
that is failing..
umm
try this

<%      
     try{
        String posted = request.getParameter("SubmitForm");
     %>          
    <jsp:useBean id="employee" class="business.Employee" scope="session" />  
    <jsp:setProperty name="employee" property="*"/>
     First name: <jsp:getProperty name ="employee" property="firstName"/>
     
<% } catch(NullPointerException error) { %>          

     <form name="tableform" action="/TaxiGlobe/DisplayEmployee.jsp" method="post">
...
<tr>
<td><b>First name</b></td>
<td><input type="text" name="firstName" size="20"
     value="<jsp:getProperty name ="employee" property="firstName"/>">
         <font color="red">* </font>
</td>
</tr>
...
</form>

<% } %>          

I think that will work.
Ghost
0
 
anna_suchodolskaAuthor Commented:
Did I say grateful?  :) Greatful

Ok it is working again with code

<jsp:useBean id="employee" class="business.Employee" scope="session" />       <------- here
<%       
      String posted = request.getParameter("SubmitForm");
      
      if(posted != null)
      {
%>            
 
    <jsp:setProperty name="employee" property="*"/>
      first name<jsp:getProperty name ="employee" property="firstName"/>
      
<% } else { %>            

            <form name="tableform" action="/TaxiGlobe/DisplayEmployee.jsp" method="post">

but it is not reading the bean, and I have no way even to check if the properties have been set.

If I decide to redirect it to another page, how can I use
<%
      if ( request.getParameter("button name") != null )
 {
%>
    <jsp:forward page="/application/Confirmation.jsp" />
 <%
     }
%>

should I use  <jsp:setProperty name="employee" property="*"/> before    <jsp:forward page="/application/Confirmation.jsp" />?  
0
 
copyPasteGhostCommented:
you have to set it then reDirect it..

if you are seting like this..
<jsp:setProperty name="employee" property="*"/>

make sure that your text field names correspond to the bean names.

like if your private variable is called firstName
your set metho would be
public void setFirstName(String x){
   firstName = x;
}

and then on your form you would have
<input type="text"  name="firstName" />

and that should work!
Ghost
0
 
anna_suchodolskaAuthor Commented:
not
<input type="text" name="firstName" size="20"
value="<jsp:getProperty name ="employee" property="firstName"/>">   ?
0
 
copyPasteGhostCommented:
well that depends on where it is..

if you want your used to be entering data then do it this way..
<input type="text"  name="firstName" />


if you want to dispaly what you already have then do it this way..

<input type="text" name="firstName" size="20" value="<jsp:getProperty name ="employee" property="firstName"/>">
actually I just noticed something..
try this..

<input type="text" name="firstName" size="20" value="<jsp:getProperty name ='employee' property='firstName'/>">

Ghost
P.S. single quotes instead of double quotes..

0
 
anna_suchodolskaAuthor Commented:
oh dear...

at last something is happening, after hours and hours of fighting... how beautiful

I realised a bad thing, my form was poining to another page and that was the reason why I could see the changes, but now it works

as you said, the values ramain in the input boxes because of the jsp tag
and I think that it is a good thing, (bad hting, good thing...  :) ) , because in this way a person using the application may view the entered data go back if they are wron, correct them ( what will overwrite the values in the bean I think) and then submit, check if ok and eventually save them

I will play now with saving it to the database and updaing db.  Let's see how it goes.

Ghost?  You are great!  Thanks a million.  You guys are really good.
Thanks for your time and not giving up!!!
0
 
copyPasteGhostCommented:
Glad I can help you out.
Ghost
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 14
  • 11
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now