Solved

jsp:forward and bean jsp:setProperties

Posted on 2004-04-16
30
924 Views
Last Modified: 2012-05-04
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
Comment
Question by:anna_suchodolska
  • 14
  • 11
  • 5
30 Comments
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10851851
you would want to do this...

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

Expert Comment

by:cpuburst
ID: 10859960
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
 

Author Comment

by:anna_suchodolska
ID: 10876893
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
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10877081
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
 
LVL 1

Expert Comment

by:cpuburst
ID: 10877107

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
 

Author Comment

by:anna_suchodolska
ID: 10877202
my form is pointing to itself and methd="post", is it ok?

<form name="tableform" action="/application/EmployeesData.jsp" method="post">
0
 

Author Comment

by:anna_suchodolska
ID: 10877229
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
 

Author Comment

by:anna_suchodolska
ID: 10877310
<%       

      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
 
LVL 1

Expert Comment

by:cpuburst
ID: 10877463
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
 

Author Comment

by:anna_suchodolska
ID: 10877472
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
 

Author Comment

by:anna_suchodolska
ID: 10877522
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
 
LVL 1

Expert Comment

by:cpuburst
ID: 10877559
<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
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10877640
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
 
LVL 1

Expert Comment

by:cpuburst
ID: 10877661

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

Cheers
Cpuburst
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10877664
i firgured. it's all good,
I hope that works :S
Ghost
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:anna_suchodolska
ID: 10877693
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
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10877711
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
 
LVL 13

Accepted Solution

by:
copyPasteGhost earned 50 total points
ID: 10877940
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
 

Author Comment

by:anna_suchodolska
ID: 10877942
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
 

Author Comment

by:anna_suchodolska
ID: 10877973
Yes!

how do I deal with else, do I need it at all
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10878007
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
 

Author Comment

by:anna_suchodolska
ID: 10878272
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
 

Author Comment

by:anna_suchodolska
ID: 10878550
----- 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
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10878606
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
 

Author Comment

by:anna_suchodolska
ID: 10878659
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
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10878742
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
 

Author Comment

by:anna_suchodolska
ID: 10879086
not
<input type="text" name="firstName" size="20"
value="<jsp:getProperty name ="employee" property="firstName"/>">   ?
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10879127
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
 

Author Comment

by:anna_suchodolska
ID: 10879596
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
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10879607
Glad I can help you out.
Ghost
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Learn to move / copy / export exchange contacts to iPhone without using any software. Also see the issues in configuration of exchange with iPhone to migrate contacts.
This article will show you how to create an ISO CD-ROM/DVD-ROM image (*.iso), and MD5 checksum signature, for use with VMware vSphere Hypervisor 6.5 (ESXi 6.5). It's a good idea to compare checksums, because many installations fail because of a corr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now