Invalidating a session in Java Bean
Hi,
I need to write a logout() method in my Java Bean to invalidate
the current session upon logout.
But i keep getting error when i try to access the session.
bu using
session.invalidate();
Does anyone know how to retrieve the current session in a Java Bean??
Thanks..
Hi,
You can pass the session from the JSP to the Javabean method for invalidating it. For example, in your JSP you can have:
<%-- instantiate your bean --%>
<jsp:useBean id="bean" class="YourJavaBean" scope="page" />
<%-- call the method with the session parametere --%>
bean.logout(session);
And then in your bean, have:
public void logout(HttpSession session) {
//Any processing...
//invalidate the session
session.invalidate();
Hope that helps,
Srinivas
Similar Messages
-
Help with Login Form (JSP DB Java Beans Session Tracking)
Hi, I need some help with my login form.
The design of my authetication system is as follows.
1. Login.jsp sends login details to validation.jsp.
2. Validation.jsp queries a DB against the parameters received.
3. If the query result is good, I retrieve some information (login id, name, etc.) from the DB and store it into a Java Bean.
4. The bean itself is referenced with the current session.
5. Once all that's done, validation.jsp forwards to main.jsp.
6. As a means to maintain state, I prefer to use url encoding instead of cookies for obvious reasons.I need some help from step 3 onwards please! Some code snippets will do as well!
If you think this approach is not a good practice, pls let me know and advice on better practices!
Thanks a lot!Alright,here is an example for you.
Assume a case where you don't want to give access to any JSP View/HTML Page/Servlet/Backing Bean unless user logging system and let assume you are creating a View Object with the name.
checkout an example (Assuming the filter is being applied to a pattern * which means when a resource is been accessed by webapplication using APP_URL the filter would be called)
public doFilter(ServletRequest req,ServletResponse res,FilterChain chain){
if(req instanceof HttpServletRequest){
HttpServletRequest request = (HttpServletRequest) req;
HttpSession session = request.getSession();
String username = request.getParameter("username");
String password = request.getParameter("password");
String method = request.getMethod();
String auth_type = request.getAuthType();
if(session.getAttribute("useInfoBean") != null)
request.getRequestDispatcher("/dashBoard").forward(req,res);
else{
if(username != null && password != null && method.equaIsgnoreCase("POST") && (auth_type.equalsIgnoreCase("FORM_AUTH") || auth_type.equalsIgnoreCase("CLIENT_CERT_AUTH")) )
chain.doFilter(req,res);
else
request.getRequestDispatcher("/Login.jsp").forward(req,res);
}If carefully look at the code the autherization is given only if either user is already logged in or making an attempt to login in secured way.
to know more insights about where these can used and how these can be used and how ?? the below links might help you.
http://javaboutique.internet.com/tutorials/Servlet_Filters/
http://e-docs.bea.com/wls/docs92/dvspisec/servlet.html
http://livedocs.adobe.com/jrun/4/Programmers_Guide/filters3.htm
http://www.javaworld.com/javaworld/jw-06-2001/jw-0622-filters.html
http://www.servlets.com/soapbox/filters.html
http://www.onjava.com/pub/a/onjava/2001/05/10/servlet_filters.html
and coming back to DAO Pattern hope the below link might help you.
http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html
http://java.sun.com/blueprints/patterns/DAO.html
http://www.javapractices.com/Topic66.cjp
http://www.ibm.com/developerworks/java/library/j-dao/
http://www.javaworld.com/javaworld/jw-03-2002/jw-0301-dao.html
On the whole(:D) it is always a good practice to get back to Core Java/J2EE Patterns.and know answers to the question Why are they used & How do i implement them and where do i use it ??
http://www.fluffycat.com/java-design-patterns/
http://java.sun.com/blueprints/corej2eepatterns/Patterns/index.html
http://www.cmcrossroads.com/bradapp/javapats.html
Hope that might help :)
REGARDS,
RaHuL -
Make sure that your bean is implementing the serializable interface and that
you are accessing the bean from the session with the same name.
Bryan
"Sandeep Suri" <[email protected]> wrote in message
news:[email protected]..
Hi, I have quick question about use of USEBEAN tag in SP2. When I
specify a scope of SESSION for the java bean, it does not keep the
values that I set for variable in the bean persistent.Thanks,Sonny
Try our New Web Based Forum at http://softwareforum.sun.com
Includes Access to our Product Knowledge Base! -
Can we use an overloaded constructor of a Java Bean with Session Tracking
Hi Friends,
If any one can solve my query.... It would be helpful.
Query:
I have a Java Bean with an overloaded constructor in it. I want to use the overloaded constructor in my JSP.
1. One way of doing that is to use it directly in the "Scriptlets" (<% %>). But then I am not sure of the way to do session tracking. I think I can use the implicit objects like "session", "request" etc. but not sure of the approach or of the implementation method.
2. Another way is through the directive <jsp: useBean>. But I cannot call an overloaded constructor with <jsp: useBean>. The only alternative way is to use the directive <jsp: useBean> where I have to write getter and setter methods in the Java Bean and use the <jsp: setProperty> and <jsp: getProperty> standard actions. Then with this approach I cannot use the overloaded constructor.
Can any one suggest me the best approach to solve this problem ?
Thanks and Regards,
Gaive.My first reaction is that you can refactor your overloaded constructor into an init(arguments...) method. Instead of overloaded constructor, you can call that init method. This is the ideal solution if possible.
As to the two choices you listed:
1. This is OK, I believe. You can use scriplet to define the bean and put it into session scope of the pageContext. I am not sure exactly what you meant by session tracking; whatever you meant, it should be doable using HttpSessionAttributeListener and/or HttpSessionBindingListener.
2. Agreed. There is no way that <jsp:useBean> can call a constructor that has non-empty arguments.
Please tell me how it works for you. -
Importing Java Bean Model (Command Bean) with Session Bean
Hello all,
I'm trying to import a CommandBean as a Java Bean Model in WebDynpro but I'm prompted with an error indicating that I cannot import the EJB (Session Bean) DC that is being accessed by my CommandBean - hence failing to import the CommandBean DC as well.
How do I import this?
Help please.
-Regards,
JanHI,
just add ejb20.jar as an external jar in your WD project. -
'invalid class-java beans not available for import
Hi Experts,
i am using java bean model in my Web Dynpro Application.
for that one i have created command bean(Customer) and in my command bean i am using some other class(CustomerInfo) as reference. this CustomerInfo class contains 4 attribtes(name ,id ,etc all are of type String)
when i try to import these 2 class into web dynpro (using java bean model),for the Customer class it
is giving an error message 'invalid class-java beans not
available for import'.but 2nd class(CustomerInfo) is imported successfully with out giving the error.what might be reason.
any help will be appriciated
Thanks in advance
With Regards
NaiduNaidu,
Please try to repeat JavaBean model import when running IDE in console mode (correspondign short-cut should be available in Windows Start menu, otherwise just copy original short-cut, edit it and change in command string "javaw" to java).
Post here what is shown on console during import.
VS -
Create Java bean for a http session
how can i create a java bean for an http session. also is it possible to access it from another java class within that session
Try the following forum (about JSP technology)
http://forum.java.sun.com/forum.jspa?forumID=45 -
Invalid Class -Java Bean not available for import
hai friends,
i have declared the bean but unable to import into webdynpro application
i am geting the error invalid Class-java bean Not available for improt
i am try for this since more than 10 days , i get the same error
even i thought there may be issues with nwds , i have installed thrice.
there is application download from the sdn tu_bonus calculation it is working
plz help me neede very urgentlyHi,
Go thro' these solved threads dealing with similar type of problem,
Import JavaBean Error : Invalid Class
invalid class - ..as Model Class already exist
Hope it helps to fix the error !
Regards
Srinivasan T -
Maintaining sessions with a java bean
I am trying to debug a Web App that occasionally loses its session when one
page is being jsp_forwarded to another. The original JSP coder uses a Java
bean named Session to hold all the sesssion information. The usebean tag is
used to maintain the correct instance. This session loss did not occur when
we were still running 5.1. Now with 6.1 we have this mysterious problem. Can
anyone make suggestions on how to tackle this problem?
The weblogic.xml file contains the working directory and the keepgenerated
set to true. All else is default.
Kenny
Try the following forum (about JSP technology)
http://forum.java.sun.com/forum.jspa?forumID=45 -
Column Not found error while trying to access databse through JSP+Java Bean
I am trying to acees MS Access 2003 db through JSP using Tomcat 5.0.28.The code for accessing the databse is incorporated in the bean.The jsp only calls the particular method of the bean .
Code for Java Bean:
package ActiveViewer;
import java.sql.*;
import java.util.*;
public class CompanyBean
Connection con;
ResultSet rs=null;
Statement st;
public CompanyBean(){}
public void connect()
try
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Here4");
con=DriverManager.getConnection("jdbc:odbc:activeviewer","","");
System.out.println("Here1");
catch (ClassNotFoundException e)
System.out.println("Could not locate driver.");
catch (SQLException e)
System.out.println("An SQL Exception has occured :: "+e);
e.printStackTrace();
catch (Exception e)
System.out.println("An unknown Exception has occured :: "+e);
e.printStackTrace();
public void disconnect()
try
if (con!=null)
con.close();
catch (SQLException e)
System.out.println("An SQL Exception has occured :: "+e);
e.printStackTrace();
public ResultSet select(String username)
if(con!=null)
try
st=con.createStatement();
rs=st.executeQuery("select * from company where username='" + username + "'");
catch (SQLException e)
System.out.println("An SQL Exception has occured :: "+e);
e.printStackTrace();
catch (Exception e)
System.out.println("An Exception has occured while retrieving :: "+e);
e.printStackTrace();
else
System.out.println("Connection to database was lost.");
return rs;
The code for JSP that uses the above bean is:
<%@ page language="java" import="java.sql.*,ActiveViewer.* " contentType="text/html"%>
<jsp:useBean id="conn" scope="session" class="ActiveViewer.CompanyBean" />
<html>
<body>
<% String username=request.getParameter("username");
String password=request.getParameter("password");
System.out.println("username:"+username);
System.out.println("password:"+password);
conn.connect();
ResultSet rs=conn.select(username);
System.out.println("Below select ");
while (rs.next())
String dbusername=rs.getString("username");
String dbpassword=rs.getString("password");
if(dbusername.equals(username) && dbpassword.equals (password))
{ %> out.println("OK");
<% }
else { %>Invalid Username and / or Password.
<br>Clickhere to go back to Login Page.
<% }
} %>
</body>
</html>
I get the following error:
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Column not found
though the database is not int he same folder as the jsp, the DSN is set correctly to pint to the db location.The jsp does print in stdout file:
Here4 (from connect method above)
Here 1 (from connect method above)
Below Select (from jsp)
This means that the jsp does connect to db but it gives the above error.Also the field name also matches that in the database and data is present in the db too.
All other things like creating package for bean,incorporating the packakage are done.
Can someone please help me with their precious advice?U're getting this error because there is no field called 'password' in ur database, the field in ur database is named 'cpassword' and not 'password'. So change the statement rs.getString("password"); to rs.getString("cpassword");
-
Problem with java beans and jsp on web logic 6.0 sp1
HI ,
I am using weblogic6.0 sp1.
i have problem with jsp and java beans.
i am using very simple java bean which stores name and email
from a html form.
but i am getting following errors:
Full compiler error(s):
D:\bea4\wlserver6.0sp1\config\mydomain\applications\DefaultWebApp_myserver\WEB-INF\_tmp_war_myserver_myserver_DefaultWebApp_myserver\jsp_servlet\_savename2.java:89:
cannot resolve symbol
symbol : class userbn
location: class jsp_servlet._savename2
userbn ud = (userbn) //[ /SaveName2.jsp; Line: 7]
^
D:\bea4\wlserver6.0sp1\config\mydomain\applications\DefaultWebApp_myserver\WEB-INF\_tmp_war_myserver_myserver_DefaultWebApp_myserver\jsp_servlet\_savename2.java:89:
cannot resolve symbol
symbol : class userbn
location: class jsp_servlet._savename2
userbn ud = (userbn) //[ /SaveName2.jsp; Line: 7]
^
D:\bea4\wlserver6.0sp1\config\mydomain\applications\DefaultWebApp_myserver\WEB-INF\_tmp_war_myserver_myserver_DefaultWebApp_myserver\jsp_servlet\_savename2.java:94:
cannot resolve symbol
symbol : class userbn
location: class jsp_servlet._savename2
ud = (userbn) java.beans.Beans.instantiate(getClass().getClassLoader(),
"userbn"); //[ /SaveName2.jsp; Line: 7]
^
3 errors
in which directory should i place java bean source file(.java file)
here is my jsp file:
<%@ page language = "java" contentType = "text/html" %>
<html>
<head>
<title>bean2</title>
</head>
<body>
<jsp:usebean id = "ud" class = "userbn" >
<jsp:setProperty name = "ud" property = "*" />
</jsp:usebean>
<ul>
<li> name: <jsp:getProperty name = "ud" property = "name" />
<li> email : <jsp:getProperty name = "ud" property = "email" />
</ul>
</body>
<html>
here is my bean :
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import java.io.*;
public class userbn implements Serializable
private String name ;
private String email;
public void setName(String n)
name = n;
public void setEmail(String e)
email = e;
public String getName()
return name;
public String getEmail()
return email;
public userbn(){}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pls help me.
Thanks
sravana.
You realy can do it like Xiang says, but the better way is to use packages. That's
the way BEA is designed for. If you use packages you can but your bean classes
in every subfolder beneath Classes. Here for example we have the subfolders test
and beans:
You have to declare the package on top of your Bean Source Code:
package test.beans;
In your JSP you don't need the import code of Xiang. You only have to refer the
path of your bean class:
<jsp:useBean id="testBean" scope="session" class="test.beans.TestBean" />
There are some other AppServers that only can deploy Java Beans in packages. So
if you use packages you are always on the right side.
ciao bernd
"sravana" <[email protected]> wrote:
>
>Thank you very much Xiang Rao, It worked fine.
>Thanks again
>sravana.
>
>"Xiang Rao" <[email protected]> wrote:
>>
>><%@ page import="userbn" language = "java" contentType = "text/html"
>>%> should
>>work for you.
>>
>>
>>"sravana" <[email protected]> wrote:
>>>
>>>HI ,
>>>
>>>I am using weblogic6.0 sp1.
>>>
>>>i have problem with jsp and java beans.
>>>
>>>i am using very simple java bean which stores name and email
>>>
>>>from a html form.
>>>
>>>but i am getting following errors:
>>>
>>>________________________________________________________________
>>>
>>>Full compiler error(s):
>>>D:\bea4\wlserver6.0sp1\config\mydomain\applications\DefaultWebApp_myserver\WEB-INF\_tmp_war_myserver_myserver_DefaultWebApp_myserver\jsp_servlet\_savename2.java:89:
>>>cannot resolve symbol
>>>symbol : class userbn
>>>location: class jsp_servlet._savename2
>>> userbn ud = (userbn) //[ /SaveName2.jsp; Line: 7]
>>> ^
>>>D:\bea4\wlserver6.0sp1\config\mydomain\applications\DefaultWebApp_myserver\WEB-INF\_tmp_war_myserver_myserver_DefaultWebApp_myserver\jsp_servlet\_savename2.java:89:
>>>cannot resolve symbol
>>>symbol : class userbn
>>>location: class jsp_servlet._savename2
>>> userbn ud = (userbn) //[ /SaveName2.jsp; Line: 7]
>>> ^
>>>D:\bea4\wlserver6.0sp1\config\mydomain\applications\DefaultWebApp_myserver\WEB-INF\_tmp_war_myserver_myserver_DefaultWebApp_myserver\jsp_servlet\_savename2.java:94:
>>>cannot resolve symbol
>>>symbol : class userbn
>>>location: class jsp_servlet._savename2
>>> ud = (userbn) java.beans.Beans.instantiate(getClass().getClassLoader(),
>>>"userbn"); //[ /SaveName2.jsp; Line: 7]
>>> ^
>>>3 errors
>>>
>>>____________________________________________________________
>>>
>>>in which directory should i place java bean source file(.java file)
>>>
>>>here is my jsp file:
>>>--------------------------------------------------------
>>>
>>><%@ page language = "java" contentType = "text/html" %>
>>><html>
>>><head>
>>><title>bean2</title>
>>></head>
>>><body>
>>><jsp:usebean id = "ud" class = "userbn" >
>>><jsp:setProperty name = "ud" property = "*" />
>>></jsp:usebean>
>>><ul>
>>><li> name: <jsp:getProperty name = "ud" property = "name" />
>>><li> email : <jsp:getProperty name = "ud" property = "email" />
>>></ul>
>>></body>
>>><html>
>>>
>>>-------------------------------------------------------------
>>>
>>>here is my bean :
>>>
>>>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>>
>>>import java.io.*;
>>>
>>>public class userbn implements Serializable
>>>{
>>>
>>> private String name ;
>>>
>>> private String email;
>>>
>>> public void setName(String n)
>>> {
>>>
>>> name = n;
>>> }
>>>
>>> public void setEmail(String e)
>>> {
>>>
>>> email = e;
>>> }
>>>
>>> public String getName()
>>> {
>>>
>>> return name;
>>> }
>>>
>>> public String getEmail()
>>> {
>>>
>>> return email;
>>> }
>>>
>>> public userbn(){}
>>>}
>>>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>>
>>>pls help me.
>>>Thanks
>>>sravana.
>>>
>>
>
-
WDVisibility type paramter in Java Bean(Web Dynpro DC in CE 7.1)
Hi All,
I need to set a WDVisibility type paramter in a java bean and i am able to do so.It imports com.sap.tc.webdynpro.progmodel.api.WDVisibility class .
The problem is when i am creating a Java Bean model it is not allowing me to select the Java bean class it gives an error message" Invalid Class : No class files found for JavaBean : com/sap/tc/webdynpro/progmodel/api/WDVisibility".
The java Bean class exists in Web Dynpro DC>src folder.
Any Pointers?
Regards
AvinashHi Ondrej ,
Refer the above links to convert the component of the first dc as a public part.
Then declare the methods which are written in the component controller of the first DC in the interface controller with the same signature.( When method is called automatically component controller method is called)
In second dc in component controller properties tab add the reference of the first dc.
Then access the method in second dc as
wdthis.wdget<UsedComponent name>Interface.methodname();
Thanks & Regards,
Ardhendu -
An error in jsp page useBean when i try to include the java bean package?
Hi Sir,
I am using import com.project.ShowData in java beans inorder to connect to the jsp.
The jsp code goes like this:-
<%@ page import="com.project.ShowData" %>
<jsp:useBean id="jspuser1" class="com.viman.ShowData" scope="session"/>
<jsp:setProperty name="jspuser1" property="*"/>
Here the error is showing as cannot resolve symbols in com.project.ShowData.But i have given the same in java beans like this:-
Package com.project;
//import of package goes here
public class ShowData
//code goes here
It seems that all are correct here.I have done few programs using this without any problem.But suddenly it is giving a problem like this.What is the problem here?It is quite Urgent.Thank u.
Regards,
m.ananthuGreetings,
Here the error is showing as cannot resolve symbols in com.project.ShowData.But i have given the
same in java beans like this:-Seeing the actual error would be more helpful. ;)
Regards,
m.ananthu Regards,
Tony "Vee Schade" Cook -
Unable to get java bean working
I have been trying to get my iWS6 to run java bean but has not been able to do so. I tried a few beans and I get the same message on the log:
3 errors, 1 warning
, stack: org.apache.jasper.JasperException: Unable to compile class for JSPNote: sun.tools.javac.Main has been deprecated.
..\ClassCache\https-sgp-web.sci.sing.seagate.com\departments\eis\_jsps\_testuser_jsp.java:60: Class jsp.UserProfile not found.
jsp.UserProfile profile = null;
^
..\ClassCache\https-sgp-web.sci.sing.seagate.com\departments\eis\_jsps\_testuser_jsp.java:63: Class jsp.UserProfile not found.
profile= (jsp.UserProfile)
^
..\ClassCache\https-sgp-web.sci.sing.seagate.com\departments\eis\_jsps\_testuser_jsp.java:68: Class jsp.UserProfile not found.
profile = (jsp.UserProfile) Beans.instantiate(this.getClass().getClassLoader(), "jsp.UserProfile");
^
3 errors, 1 warning
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:284)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.loadJSP(JspServlet.java:193)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.access$4(JspServlet.java:167)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:477)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:589)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at com.iplanet.server.http.servlet.NSServletRunner.invokeServletService(NSServletRunner.java:891)
at com.iplanet.server.http.servlet.WebApplication.service(WebApplication.java:1064)
at com.iplanet.server.http.servlet.NSServletRunner.ServiceWebApp(NSServletRunner.java:953)
, root cause:Sir
Still my jsp files can't find java beans.
I set classpath properly .
my document directory = c:/raju
i put my jsp file = c:/raju/jsp/<jsp files>
and servlet files = c:/raju/servlet/<servlet class files>
actually jsp files and servlets were working successfully,
if i add my java beans inside my jsp file the it shows error.
inside my jsp file
<%@ page import ="something.myclass" %>
<jsp:useBean id="db" scope="session" class="something.myclass" />
Note: "something" is package name;
#----- this line i modified inside jvm12.conf file
jvm.classpath=C:/iPlanet/Servers/plugins/servlets/examples/legacy/beans.10/SDKBeans10.jar;c:/raju/WEB-INF/classes/;
Thanks
raju -
Java Bean access in a JSP Tag handler class
Hello Everybody,
I am trying to access my java bean(ErrorBean.java) in th doEndTag() method of the tag handler class(MyTagHandler.java) and iam getting an "CLASSCAST EXCEPTION"
I am doing it like this in the tag handler class.
MyBeans.ErrorBean errorBean = (MyBeans.ErrorBean)pageContext.getSession().getAttribute("ErrorBean");
Where MyBeans is the package in which my Error Bean is and iam placing the ErrorBean object in the session object before it comes to the jsp page where i have the jsp Custom tag:
<%@ taglib uri="/WEB-INF/taglib.tld" prefix="errors" %>
<errors:message/>
<%@ include file="footer.jsp"%>
I was of the opinion that i was casting it right...can anyone help me to find ...where iam doing it wrong...its really URGENT ..PLEASEFriend that did not work either ...
u know what...the ErrorTagHandler class is comiling fine with out errors but in the jsp page when the custom tag is hit..this error is showing up when i see the server log.
can you throw some light on this..
thanks..
Firasath
Maybe you are looking for
-
i have a white macbook 2.4 GHz Intel Core 2 Duo (Macbook 4,1) i wanted to hook it up to my tv and i would like both video AND audio to work any one with a suggestion for the best way to do this?
-
When executed xslsample - returns error
I am running XDK 9.0.1.2.0 release version on Oracle 9i database. Installation of XDK was clean. When I try to run xslsample which is demo procedure, I get the following error. SQL> exec xslsample ('D:\Projects\XML\PLSQL\xdk\demo\plsql\parser','famil
-
Why can't I open PDF's since installing OSx 10.9.4 on my Mac?
Ever since I installed the latest version of OSx I have been unable to open, view or print PDF's. I have downloaded Abode Reader XI (version 11.0.07) a few times but still no luck. I have had to load Firefox on my desktop as an alternative browser,
-
How u will set the external parameters like packet size and number
How u will set the external parameters like packet size and number of parallel process.
-
We have performance on a scale of 1 to 5 and potential on a scale of 2. We want to split talent into a "4 box" matrix by excluding anyone with 1 and 2 performance ratings (the lowest) and in the four boxes see employees plotted as follows: top right