how to use jsp:useBean ?

Hi to all,I want to save data through jsp:useBean andhow to create bean ?Waiting for replyThanks
[124 byte] By [Vansha] at [2007-11-27 9:31:20]
# 1

HI vansh..

I yest got jsp:useBean working...so i think i can help u out correctly....test the below code..once u get to know how it is working u can then work on complex codes

This is javabean class

SimpleBean.java

package com.test;

public class SimpleBean implements java.io.Serializable {

/* Properties */

private String name = null;

private int age = 0;

/* Empty Constructor */

public SimpleBean() {}

/* Getter and Setter Methods */

public String getName() {

return name;

}

public void setName(String s) {

name = s;

}

public int getAge() {

return age;

}

public void setAge(int i) {

age = i;

}

}

now create a folder for package.... ie in ur c:\ create com folder n inside tht create test folder n place this SimpleBean.java file in it...if u know how package works this will b easy for u...

n then on command prompt set classpath for ur package

ie c:\>set classpath=,;c:\com;

now compile the SimpleBean.java

ie c:\com\test>javac SimpleBean.java

if everything is fine u wont get error....

now if u r using tomcat then go to webapps/root/web-inf/classes... inside classes u need to create same folder structure tht u created in C:\.. ie inside classes create com folder n inside tht create test folder and now copy ur SimpleBean.class file inside test folder

then place below testbean.jsp file in ur webapps/root folder of tomcat...

testbean.jsp

<html>

<head>

<title>SimpleBean Test Page</title>

</head>

<body>

<%-- Creating JavaBeans --%>

<jsp:useBean id="simple" class="com.test.SimpleBean">

<jsp:setProperty name="simple" property="name" value="hetal" />

<jsp:setProperty name="simple" property="age" value="00" />

</jsp:useBean>

<%-- Displaying JavaBean property's value --%>

Name retrieved from JavaBean has the value of :

<b><jsp:getProperty name="simple" property="name" /></b>.<br>

Age retrieved from JavaBean has the value of :

<b><jsp:getProperty name="simple" property="age" /></b> years.<br>

</body>

</html>

this should definitely work for u.........

Good luck.

hetal_giria at 2007-7-12 22:45:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Hello , hetal_giri Thanks for your reply actually i want to save data in mySql database . please tell me how to make connection through bean and how to useit . Expection in favorable reply Vansh
Vansha at 2007-7-12 22:45:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Use JDBC.

You can find the JDBC tutorial here: http://java.sun.com/docs/books/tutorial/jdbc/basics/index.html

And the MySQL JDBC driver here: http://www.mysql.com/products/connector/j/

Finally the MySQL driver reference: http://dev.mysql.com/doc/refman/5.0/en/connector-j.html

BalusCa at 2007-7-12 22:45:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
check this out... http://www.roseindia.net/jsp/usingbeansinjsp.shtml
hetal_giria at 2007-7-12 22:45:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
Hi Hetal,thanks for help .. i want to use session in my different JSP pages ...(Like a website)please tell me how to create session in JSP and how to use it ?Vansh
Vansha at 2007-7-12 22:45:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
You don't need to create the session yourself. Just access it by the 'session' reference.
BalusCa at 2007-7-12 22:45:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
Hello BalusC ,Thanks for ur reply actually i do not know how to use references for session in jsp ?please send me example for this ...or link. Vansh
Vansha at 2007-7-12 22:45:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
The 'session' reference is already available for you in the JSP. It refers to an instance of the HttpSession object which is specific per user session.
BalusCa at 2007-7-12 22:45:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

Hi vansh,

here is the example of session...

wotever you want into session u need to add tht into session first like

String username="abcxyz";

session.setAttribute("user",username);

in jsp u can directly use the session object ie session...and setAttribute() is the method to set content for session.... now "user" is the session name give to ur variable "username"....n u will access session content using this name...like

session.getAttribute("user");

hope u r able to get it.....

hetal_giria at 2007-7-12 22:45:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10
HI vansh,I think u need session in JSTL.... the example tht i gave is in JSP only...i m still learning the tutorial for JSTL...so if i m able to get it i will let u know...but even u can google it n check..may b you will get some idea to do....thanks
hetal_giria at 2007-7-12 22:45:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11

Here is wot i found out for session...n sorry for previous reply...

coz i m totally messed up between javabean and JSTL...

http://forum.java.sun.com/thread.jspa?threadID=518448&messageID=2474374

i tried out n it works fine..in tht program they have not used setProperty n getProperty...they have called method directly...

here is my previous code with session...

testbean.jsp

<html>

<head>

<title>SimpleBean Test Page</title>

</head>

<body>

<%-- Creating JavaBeans --%>

<jsp:useBean id="simple" scope="session" class="com.test.SimpleBean">

<jsp:setProperty name="simple" property="name" value="hetal giri" />

<jsp:setProperty name="simple" property="age" value="21" />

</jsp:useBean>

<%-- Displaying JavaBean property's value --%>

Name retrieved from JavaBean has the value of :

<b><jsp:getProperty name="simple" property="name" /></b>.<br>

Age retrieved from JavaBean has the value of :

<b><jsp:getProperty name="simple" property="age" /></b> years.<br>

<A href="page2.jsp">Continue</A>

</body>

</html>

page2.jsp

<html>

<head><title></title></head><body>

<jsp:useBean id="simple" scope="session" class="com.test.SimpleBean">

</jsp:useBean>

<jsp:getProperty name="simple" property="age" />

<%

int beanInt = simple.getAge();

%>

<%= "beanInt = " + beanInt %>

</body></html>

enjoy..............

hetal_giria at 2007-7-12 22:45:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...