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

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

Understanding the Basics of a JSP Bean (BIG POINTS!)

Hi, I have just literally been introduced to JSP Beans, and have no Idea how JSP Beans work..

I have a reasonable understand on JSPs in generally, but nt sure how the beans part work.

I wish to have an example with good depth commenting to explin what is happened and how things are accesses etc..

The example has to include i.e. three files, html, jsp and java..

the html file will have a form, for a person to fill say a number 15, press submit for the jsp to create the jsp bean so that the java file can calculate the square result to the user using the jsp file..
Points earned for well explained example, similar to the one given.

Thank You For your expertise.
:)
0
deepanp
Asked:
deepanp
  • 3
  • 2
  • 2
  • +1
1 Solution
 
bloodredsunCommented:
By JSP Bean I assume that you mean a JavaBean as there is no such thing as a "JSP Bean".

The official Sun tutorial can be found here http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/JSPBeans.html

The main benefit of using Javabeans is that you remove java code from the JSP into a separate Java file so that maintenance and re-use can be more effective.

Rather than restating how this works the best thing for you would be to follow this tutorial http://www.gulland.com/courses/JavaServerPages/jsp_beans.jsp which shows you how JSPs can call javabeans using the bean tags: <jsp:useBean/>, <jsp:setProperty/> and <jsp:getProperty/>.

The only thing you would need to change would be to create a method called getSquareNumber() which returns the square of the int field squareNumber
0
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
http://www.roseindia.net/jsp/usingbeansinjsp.shtml

check out the above link as well as for more tutorails use google search becuase lot many resources are available on Internet about this.

http://www.java2s.com/Code/Java/JSP/Beans.htm

Here you get some good examples thank You.
0
 
seopherCommented:
i'll spent a few minutes giving a more in depth explanation:
-------------
HTML - simple form
-------------
<html>
<body>
   <form action="file.jsp" method="post" name="thisform">
       <input type="text" id="thenumber" name="thenumber" />
   <input type="submit" value="click me!" />
</form>
</body>
</html>
---------------
that handles the basic html form right?

JSP - gets the details from that form, connects to the javabean etc
---------------

<%@ page language="java" contentType="text/html"%>

<jsp:useBean id='bean' class='yourbean.bean' scope='page'/>
//this imports the javabean we've not written yet, just tells the jsp where it's found, i'll explain more later

<% int thenumber = Integer.parseInt(request.getParameter("thenumber")); %>
//this makes a new integer called 'thenumber' which gets the item you passed from the html form, also called 'thenumber'

<html>
<body>
 
 <%
        int squareroot = bean.work(thenumber);
        //this makes a new integer called 'squareroot' which will be the result of the bean processing 'thenumber' (which we've not written yet)

        out.println("you entered :"+thenumber);
        out.println("<br>the result was: "+squareroot); //these two just output what you did obviously
  %>
</body>
</html>

-------------------
right so that JSP gets the detail from the form, and passes it to the bean, which then returns the value, lets write the bean now then!

JavaBean
-------------------

package mybean; //this is basically the folder name that your bean is held inside (in the WEB-INF/classes directory)

public class bean
{
      public int work(int passedvalue) //the function to work out your square root, passed value is the number you pass to the function
      {
           //whatever processing you want here, for this example i'll just add 5 to the number passed cos it's easy =)
           passedvalue = passedvalue+5;
           return passedvalue; //this passes the item back to the JSP page
      }
}


------------------------
that should do you as a rough guideline for your specific example.  PHEW! took a while to right.  for more information on how to set JavaBeans up (ie. where you put them on the server etc, im sure one of the above tutorials should sort you.

Steve.



0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
seopherCommented:
**whoops, in the JSP i said the package was YourBean and in the Bean i put MyBean... they should both be the same
0
 
deepanpAuthor Commented:
:D.. Thanks!!
0
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
seopher I think you need to note that you are not suppose to give the full code as above as Per the Rules of EE.
0
 
seopherCommented:
are we not? i thought we were't allowed to copy and paste code from other sites because that's stealing, but you're allowed to write your own code?
0
 
deepanpAuthor Commented:
yeh you are allowed to write your own code, as far as i knw. every1 writes their own code, look at other posts
:)
Thanks again
0

Featured Post

Industry Leaders: 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!

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now