cancel
Showing results for 
Search instead for 
Did you mean: 

Session in WebDynPro

david_fryda2
Participant
0 Kudos

Hi everyone,

Is it good to use an EJB statefull session to store a value for a specific session ?

Is it possible to use this EJB from a WebDynPro ?

Maybe using an EJB is to much heavy...is there another way ?

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi David,

If state is related to business logic of your application and contains references to business objects it is better to have it in statefull session bean.

On other hand, if state is related to display logic and quite simple HTTP session is sufficient in most cases.

However, NW till NW04s does not expose any session related API as public. Also proposed here cookies-based approach has the same non-standard smell

I noticed someone on this forum try to use transient attributes of IUser object, probably you will evaluate this approach as well.

VS

Former Member
0 Kudos

hello David,

HttpServletRequest request = ((IWebContextAdapter) WDWebContextAdapter.getWebContextAdapter()).getHttpServletRequest();

Regards,

Piyush.

david_fryda2
Participant
0 Kudos

Thanks,

Now it compiles.

Here is what I did :

1) Create an application Sender and its component.

In the view, I created a button "Send". Here is the code in its action:

HttpServletRequest request = ((IWebContextAdapter) WDWebContextAdapter.getWebContextAdapter()).getHttpServletRequest();

request.setAttribute("key1", "val1");

2) Create an application Receiver and its component.

In the view, I created a button "Receive". Here is the code in its action:

HttpServletRequest request = ((IWebContextAdapter) WDWebContextAdapter.getWebContextAdapter()).getHttpServletRequest();

String res = (String)request.getAttribute("key1");

wdComponentAPI.getMessageManager().reportSuccess(res);

In the Portal, I create 2 WebDynPro iViews : one for the send and one for the receiver.

I also created a Page that contains thoses 2 iViews.

When I click on the "Send" button and after on the "Receive" button, nothing appears.

Did I miss something ?

Thanks.

Former Member
0 Kudos

Your setting attributes in the request...

When you click Send you trigger a request in which you set attribute "key1".

When you click Receive you trigger ANOTHER request and since you probably don't go through the same Send action, the "key1" attribute is not set for the request.

Are both Web Dynpro views part of the same Web Dynpro application? If so, why not use the Component Controller Context to exchange data between two views?

david_fryda2
Participant
0 Kudos

The 2 views are created in seperate WebDynPro applications.

These 2 applications are defined in the same project.

How can I get the attribute with this configuration ?

Is it possible ?

Thanks.

Former Member
0 Kudos

Try

HttpSession session = request.getSession();

HttpSession has setAttribute() and getAttribute() methods.

I don't know if both Web Dynpro apps will share the same session though.

david_fryda2
Participant
0 Kudos

Hi Pascal,

It works with HttpSession session = request.getSession();

Great!

I am thinking that there could be a problem with it ? if several users are executing the same page, will the data be shared ? Will the users see the same thing on their screen ?

Thanks!

Former Member
0 Kudos

No, a session is per user and ends when the browser session ends.

david_fryda2
Participant
0 Kudos

Thanks a lot to all of you.

Everything works.

Former Member
0 Kudos

Good to read that our help was useful. Have fun with more Web Dynpro development!

david_fryda2
Participant
0 Kudos

Last question : Is the session working with diferent pages ?

I answere myself : Yes it does.

Message was edited by: David Fryda

Former Member
0 Kudos

Hi,

What is the jar file required for IWebContextAdapter. I wrote the same code what you have given for session in webdynpro, but I am getting error for IWebContextAdapter in the code only.

Regards,

Suresh

Answers (3)

Answers (3)

Former Member
0 Kudos

Hi,

Where can i get the httpsession or HttpServletRequest in webdynpro,which jar have it?

Thanks,

Damodhar.

Former Member
0 Kudos

Right click on your Web Dynpro project (in package explorer), choose "Set Additional Libraries" from the context menu.

In the popup dialog, select Installed Libraries -> sap.com -> servlet.

This adds the jar containing httpservletrequest (and related classes) to the build classpath of your project.

roberto_tagliento
Active Contributor
0 Kudos

There is an alternative.

I did this test time ago

<a href="http://www.awakening.it/provaLingua.zip">link</a>

Give few minute, look it again and explain a bit after.

Open the application in more window and look, the variable in one for all application.

roberto_tagliento
Active Contributor
0 Kudos

public java.lang.String Put_Lang_in_Session(String value )

{

//@@begin Put_Lang_in_Session()

IMaintainScope maintainer = Utils.getScopeMaintainer( WDScopeType.CLIENTSESSION_SCOPE );

IScope scope = maintainer.getScope( );

String _key = new String( value );

scope.put( "Lang", _key);

// final IMaintainScope maintainer = Utils.getScopeMaintainer( WDScopeType.CLIENTSESSION_SCOPE );

// final IScope scope = maintainer.getScope( );

// scope.put( _key, _object);

return _key;

//@@end

}

public java.lang.String Get_Lang_from_Session( )

{

//@@begin Get_Lang_from_Session()

IMaintainScope maintainer = Utils.getScopeMaintainer( WDScopeType.CLIENTSESSION_SCOPE );

IScope scope = maintainer.getScope( );

String var_session = new String();

try{

var_session = scope.get("Lang").toString();

}

catch(Exception e){

var_session = e.toString();

}

return var_session;

//@@end

}

0 Kudos

Hi David,

You could use the Cookies class but it would be a non standard API for WebDynpro.

Code would be something like

Cookie[] cookies = request.getCookies();

String cookieName = SAPCookie;

String defaultValue = 1;

for ( int i=0; i<cookies.length; i++) {

Cookie cookie = cookies<i>;

if (cookieName.equals(cookie.getName()))

return(cookie.getValue());

}

return(defaultValue);

}

Regards,

shyam.

david_fryda2
Participant
0 Kudos

Hi Shyam,

The problem is where do you get the request object ?

Thanks.

Former Member
0 Kudos

hello David,

you can use EJBs for storing a value for a session.

also u can use cookie to store values.

regards,

Piyush.

david_fryda2
Participant
0 Kudos

Hi Piyush,

I just want to save for the session a simple String.

So maybe I should use the cookies.

Can you explan me to use them from WebDynPro ?

Former Member
0 Kudos

hello David,

u might have used cookies in jsp's. its similar to it.

first u have to get the HttpServeletRequest and HttpServletResponse objects. now create Cookie objects and add it to the response object.

this is a non standard API of WebDynpro.

search in forum for more details.

regards,

Piyush.

Former Member
0 Kudos

You can regard the Component Controller Context as being a session, so you can use this to store sessiondata.

Former Member
0 Kudos

hello David,

try to find it in Build paths -> add variables. add servlet.jar.

regards,

Piyush.

david_fryda2
Participant
0 Kudos

Hello Piyush,

I unsderstand that I have to get the HttpServletRequest and Response objects.

First, I am not using JSPs or servlets.

So how can I get those objects ?

Thanks

0 Kudos

Hi David,

HttpServletRequest request = (com.sap.tc.webdynpro.services.sal.adapter.core.IWebContextAdapter) WDWebContextAdapter.getWebContextAdapter()).getHttpServletRequest();

Task.getCurrentTask().getWebContextAdapter().getHttpServletRequest()

But it is a non-standard API.

Regards,

shyam.

david_fryda2
Participant
0 Kudos

Hi Shyam,

Thanks for the answer.

What are the risks for using a non standard API ?

Thanks!

Former Member
0 Kudos

hello David,

right click on the project and select

Properties -> library -> add variable -> TSSAP_JAVAX_HOME

click extend and add servlet.jar.

hope this will do.

regards,

Piyush.

david_fryda2
Participant
0 Kudos

Hello Piyush,

What is the diference between using your solution and using WDWebContextAdapter.getWebContextAdapter()).getHttpServletRequest(); ?

Thanks

david_fryda2
Participant
0 Kudos

Hi Shyam,

The method getHttpServletRequest does not exist.

Here is the exception.

Kind Status Priority Description Resource In Folder Location

Error The method getHttpServletRequest() is undefined for the type IWDWebContextAdapter MyAppView.java WD1/gen_wdp/packages/com/app line 101

Former Member
0 Kudos

hello David,

Shyam just specified the code to get the request and response object.

"WDWebContextAdapter.getWebContextAdapter()).getHttpServletRequest();"

this code will return the request object.

the rist factor is unknown.

regards,

Piyush.

david_fryda2
Participant
0 Kudos

Hi Piyush,

The method getHttpServletRequest() is unknown.

Maybe I have to add somthing to the project ?

I already added to servlet.jar as you told me.

Thanks.

Former Member
0 Kudos

The brackets in the posted code do not match (there's one ")" extra). Maybe that is causing the problem?

david_fryda2
Participant
0 Kudos

Hi Pascal,

The bracket is not the problem.

The method doesn't exist.

Why can i write the following :

HttpServletRequest request;

request.getSession(true);

Should this work ?