Foren

How to pass values ​​between jsp page and java class?

Abzal Amantaev, geändert vor 12 Jahren.

How to pass values ​​between jsp page and java class?

New Member Beiträge: 22 Beitrittsdatum: 16.05.11 Neueste Beiträge
Hi friends!
I need to pass values ​​between jsp page and java class. How to create a shared variable for JSP_name.jsp and Class_name.java, or how to pass values ​​between them? How do you solve this problem???
thumbnail
Raja Nagendra Kumar, geändert vor 12 Jahren.

RE: How to pass values ​​between jsp page and java class?

Expert Beiträge: 484 Beitrittsdatum: 02.03.06 Neueste Beiträge
Is the JSP page and class part of the same portlet or belong to portlets which are packaged in same war or different war..
or JSP page different context and not belong to portlet..

Could you clarify.

Regards,
Raja Nagendra Kumar,
C.T.O
www.tejasoft.com
Abzal Amantaev, geändert vor 12 Jahren.

RE: How to pass values ​​between jsp page and java class?

New Member Beiträge: 22 Beitrittsdatum: 16.05.11 Neueste Beiträge
Raja Nagendra Kumar:
Is the JSP page and class part of the same portlet or belong to portlets which are packaged in same war or different war..
or JSP page different context and not belong to portlet..

Could you clarify.

I just need to create a local variable, JSP, and Java-class hotel in one war file.
amit nakate, geändert vor 9 Jahren.

RE: How to pass values ​​between jsp page and java class?

New Member Beitrag: 1 Beitrittsdatum: 12.12.14 Neueste Beiträge
hi
i am create one jsp page and i want to send name and password to java class
thumbnail
devi nimmagadda, geändert vor 12 Jahren.

RE: How to pass values ​​between jsp page and java class?

Regular Member Beiträge: 109 Beitrittsdatum: 09.12.10 Neueste Beiträge
Hi,

you can pass the variables through request scope or session scope.

request.setAttribute("variable name","value of its");

session.setAttribute("variable name","value");

( OR)

you can send the parameters through url.
Abzal Amantaev, geändert vor 12 Jahren.

RE: How to pass values ​​between jsp page and java class?

New Member Beiträge: 22 Beitrittsdatum: 16.05.11 Neueste Beiträge
devi nimmagadda:

you can pass the variables through request scope or session scope.

request.setAttribute("variable name","value of its");

session.setAttribute("variable name","value");

( OR)

you can send the parameters through url.


Hi Devi!
In the my View.jsp page I wrote these lines:

String value="Hello";
request.setAttribute("value",value);

Then in my Java-class, which extends from MVCPortlet I wrote:

public void printValue(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException {
String arg=actionRequest.getAttribute("value").toString();
System.out.println(arg);
}

But it does not work ... Can you show me an example please ...
thumbnail
Hitesh Methani, geändert vor 12 Jahren.

RE: How to pass values ​​between jsp page and java class?

Regular Member Beiträge: 171 Beitrittsdatum: 24.06.10 Neueste Beiträge
Hi Abzal,

When you go for request.setAttribute in jsp it would set attribute in HttpRequest and in portlet class you are getting actionRequest
in that case you will have to convert portletrequest to httprequest first and then get the attribute.

HttpServletRequest request = PortalUtil.getHttpServletRequest(renderRequest);
HttpServletRequest originalRequest = PortalUtil.getOriginalServletRequest(request);

Now from originalRequest(which is HttpServletRequest), you can try getting the attribute.

Hope it works for you. emoticon

Thanks and Regards,
Hitesh Methani
Luca kk, geändert vor 12 Jahren.

RE: How to pass values ​​between jsp page and java class?

Expert Beiträge: 284 Beitrittsdatum: 07.12.10 Neueste Beiträge
Hi Hitesh,
I have a similar problem with the session attributes.
How can I do to pass session values from java class to
jsp?
Thanks
thumbnail
Hitesh Methani, geändert vor 12 Jahren.

RE: How to pass values ​​between jsp page and java class?

Regular Member Beiträge: 171 Beitrittsdatum: 24.06.10 Neueste Beiträge
Hi Luca,

As mentioned you can get httprequest from actionRequest,
and then from httprequest, you can get httpsession by calling the method, httprequest.getSession().

Then setting attributes in httpsession attribute will work out your solution.

Thanks and Regards,
Hitesh Methani
Luca kk, geändert vor 12 Jahren.

RE: How to pass values ​​between jsp page and java class?

Expert Beiträge: 284 Beitrittsdatum: 07.12.10 Neueste Beiträge
Hi Hitesh,
maybe i don't undestand but I explain myself better.
I set a session attribute in ProcessAction as:

 public void processAction(ActionRequest request, ActionResponse response) throws PortletException,IOException {
        PortletSession session = request.getPortletSession();
        session.setAttribute("sessionValue", "Hello", PortletSession.PORTLET_SCOPE);  
    }


Then in a jsp I want to get "sessionValue" attribute but, in jsp, "session" is defalut defined
as "HttpSession" and:

session.getAttribute("sessionValue");


returns "null".
Which method have I use in jsp to convert HttpSession in PortletSession?
Thanks
thumbnail
Hitesh Methani, geändert vor 12 Jahren.

RE: How to pass values ​​between jsp page and java class?

Regular Member Beiträge: 171 Beitrittsdatum: 24.06.10 Neueste Beiträge
Hi Luca,
There are two ways to solve your problem :
1 )You can use portletSession in jsp instead of httpsession,
only thing you have to do is include taglib
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

and then if you using the tag

<portlet:defineObjects/>

will create portletSession object in your jsp.

2) You can get HttpSession and set the attribute in it to get it in jsp as follows

HttpServletRequest request = PortalUtil.getHttpServletRequest(renderRequest);
HttpServletRequest originalRequest = PortalUtil.getOriginalServletRequest(request);
HttpSession httpsession = originalRequest.getSession();

Setting attribute in httpsession will help you to retreive in jsp.

Thanks,
Hitesh Methani
Luca kk, geändert vor 12 Jahren.

RE: How to pass values ​​between jsp page and java class?

Expert Beiträge: 284 Beitrittsdatum: 07.12.10 Neueste Beiträge
Hi Hitesh,
the 1) solution works but only if i set the session attribute
in "doView" as:

public void doView(RenderRequest request,RenderResponse response) throws PortletException,IOException {

        PortletSession session = request.getPortletSession();
        session.setAttribute("sessionValue", "Hello", PortletSession.APPLICATION_SCOPE);
         
        response.setContentType("text/html");
        PortletRequestDispatcher dispatcher =
        getPortletContext().getRequestDispatcher("/WEB-INF/jsp/LiferayPortletOne_view.jsp");
        dispatcher.include(request, response);
    }


but if i set session attribute in "ProcessAction" as above don't works and in jsp
"sessionValue" returns null.
Do you know why?
thumbnail
Hitesh Methani, geändert vor 12 Jahren.

RE: How to pass values ​​between jsp page and java class?

Regular Member Beiträge: 171 Beitrittsdatum: 24.06.10 Neueste Beiträge
Hi Luca,

Confirm that if you are using portletSession in processAction method, you need to use only portletSession in jsp and not httpsession.

Just to mention that in java class and jsp type of the session should be same.

Thanks and Regards,
Hitesh Methani
Luca kk, geändert vor 12 Jahren.

RE: How to pass values ​​between jsp page and java class?

Expert Beiträge: 284 Beitrittsdatum: 07.12.10 Neueste Beiträge
Hi Hitesh,
in fact in the jsp I use portletSession as:


&lt;%@page contentType="text/html"%&gt;
&lt;%@page pageEncoding="UTF-8"%&gt;
&lt;%@page import="javax.portlet.*"%&gt;
&lt;%@taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%&gt;

<portlet:defineobjects />
&lt;%PortletPreferences prefs = renderRequest.getPreferences();%&gt; 

<b>
    LiferayPortletOne - VIEW MODE
    <br>
    The value is:
    &lt;%=portletSession.getAttribute("sessionValue",PortletSession.APPLICATION_SCOPE)%&gt;
</b>


Where was I wrong?
Thanks and regards,
Luca
thumbnail
Raja Nagendra Kumar, geändert vor 12 Jahren.

RE: How to pass values ​​between jsp page and java class?

Expert Beiträge: 484 Beitrittsdatum: 02.03.06 Neueste Beiträge
Did you set the attribute "sessionValue" in render method..

some thing like this..

PortletSession portletSession = aRenderRequest.getPortletSession();
portletSession.setAttribute(sessionValue, "test");

First render method gets called and then the jsp page.
Hence for jsp to see the value attribute the value should been set in render method.

Regards,
Nagendra
Luca kk, geändert vor 12 Jahren.

RE: How to pass values ​​between jsp page and java class?

Expert Beiträge: 284 Beitrittsdatum: 07.12.10 Neueste Beiträge
Hi Nagendra,
if i set the portletSession attribute in doView method,
it works!
Do you know how to pass that attribute to another portlet?

In the "portlet.xml" file of the first portlet I inserted:

<portlet>
    <portlet-name>LiferayPortletOne</portlet-name>
    <instanceable>true</instanceable>
   [b] <private-session-attributes>false</private-session-attributes>[/b]
</portlet>

Then in the "doView" method of the first portlet I set a PortletSession
attribute as:

 public void doView(RenderRequest request,RenderResponse response) throws PortletException,IOException {
        [b]PortletSession session = request.getPortletSession();
        session.setAttribute("sessionValue", "Hello", PortletSession.APPLICATION_SCOPE);[/b]
        response.setContentType("text/html");
        PortletRequestDispatcher dispatcher =
        getPortletContext().getRequestDispatcher("/WEB-INF/jsp/LiferayPortletOne_view.jsp");
        dispatcher.include(request, response);
 }

Then in the "doView" method of the second portlet I get a PortletSession
attribute and i try to print as:

public void doView(RenderRequest request,RenderResponse response) throws PortletException,IOException {
        response.setContentType("text/html");

        [b]PortletSession ps = request.getPortletSession();
        String tabNames = (String)ps.getAttribute("sessionValue ",ps.APPLICATION_SCOPE);[/b]
        Writer writer = response.getWriter();
	writer.write("<br>");
        [b]writer.write(tabNames);[/b]

        PortletRequestDispatcher dispatcher =
        getPortletContext().getRequestDispatcher("/WEB-INF/jsp/LiferayPortletTwo_view.jsp");
        dispatcher.include(request, response);
    }

but the string is null!!!!!!
Do you know why?
Thanks and regards,
Luca
thumbnail
Leo PRATLONG, geändert vor 12 Jahren.

RE: How to pass values ​​between jsp page and java class?

Expert Beiträge: 363 Beitrittsdatum: 06.07.10 Neueste Beiträge
Your second portlet does not share its session. So you'll not able to read shared resource from PortletSession.

You just have to change the key of your resource in portletSession with the prefix "LIFERAY_SHARED". It will allow to "private" portlet to read this one.
So, it'll be :
session.setAttribute("LIFERAY_SHARED_sessionValue", "Hello", PortletSession.APPLICATION_SCOPE);


and

session.getAttribute("LIFERAY_SHARED_sessionValue", PortletSession.APPLICATION_SCOPE);


More infos : http://longgoldenears.blogspot.com/2008/03/liferay-session-sharing-demystified.html
Luca kk, geändert vor 12 Jahren.

RE: How to pass values ​​between jsp page and java class?

Expert Beiträge: 284 Beitrittsdatum: 07.12.10 Neueste Beiträge
Great Leo,
it works!!!!!
Thanks for the tip,
best regards,
Luca
thumbnail
Sagar A Vyas, geändert vor 12 Jahren.

RE: How to pass values ​​between jsp page and java class?

Liferay Master Beiträge: 679 Beitrittsdatum: 17.04.09 Neueste Beiträge
Luca kk:
Hi Nagendra,
if i set the portletSession attribute in doView method,
it works!
Do you know how to pass that attribute to another portlet?

In the "portlet.xml" file of the first portlet I inserted:

<portlet>
    <portlet-name>LiferayPortletOne</portlet-name>
    <instanceable>true</instanceable>
   [b] <private-session-attributes>false</private-session-attributes>[/b]
</portlet>

Then in the "doView" method of the first portlet I set a PortletSession
attribute as:

 public void doView(RenderRequest request,RenderResponse response) throws PortletException,IOException {
        [b]PortletSession session = request.getPortletSession();
        session.setAttribute("sessionValue", "Hello", PortletSession.APPLICATION_SCOPE);[/b]
        response.setContentType("text/html");
        PortletRequestDispatcher dispatcher =
        getPortletContext().getRequestDispatcher("/WEB-INF/jsp/LiferayPortletOne_view.jsp");
        dispatcher.include(request, response);
 }

Then in the "doView" method of the second portlet I get a PortletSession
attribute and i try to print as:

public void doView(RenderRequest request,RenderResponse response) throws PortletException,IOException {
        response.setContentType("text/html");

        [b]PortletSession ps = request.getPortletSession();
        String tabNames = (String)ps.getAttribute("sessionValue ",ps.APPLICATION_SCOPE);[/b]
        Writer writer = response.getWriter();
	writer.write("<br>");
        [b]writer.write(tabNames);[/b]

        PortletRequestDispatcher dispatcher =
        getPortletContext().getRequestDispatcher("/WEB-INF/jsp/LiferayPortletTwo_view.jsp");
        dispatcher.include(request, response);
    }

but the string is null!!!!!!
Do you know why?
Thanks and regards,
Luca


or you can do it like as below for second portlet
<portlet>
<portlet-name>LiferayPortletTwo</portlet-name>
<instanceable>true</instanceable>
<private-session-attributes>false</private-session-attributes>
</portlet>

it should work either way.

Thanks,
Sagar
thumbnail
Hitesh Methani, geändert vor 12 Jahren.

RE: How to pass values ​​between jsp page and java class?

Regular Member Beiträge: 171 Beitrittsdatum: 24.06.10 Neueste Beiträge
Hi Luca,

Its strange that you are not getting the value of session attribute,
So I tried your code by setting portlet session attribute and getting the same in jsp and it worked for me.

Please see attached portlet, in which I tried your code and its giving me the expected result.

I have tried it in 6.0 enterprise version

Thanks,
Hitesh Methani
Luca kk, geändert vor 12 Jahren.

RE: How to pass values ​​between jsp page and java class?

Expert Beiträge: 284 Beitrittsdatum: 07.12.10 Neueste Beiträge
Hi Hitesh,
it works well for me too.
The problem is when i set the portletSession attribute
in "ProcessAction" method and only there.
If i set that attribute in "doView", everything works.
Thanks,
Luca
thumbnail
Raja Nagendra Kumar, geändert vor 12 Jahren.

RE: How to pass values ​​between jsp page and java class?

Expert Beiträge: 484 Beitrittsdatum: 02.03.06 Neueste Beiträge
Nope this does not work.. as when form is submitted, Action-handler gets called first and then view.jsp
Luca kk, geändert vor 12 Jahren.

RE: How to pass values ​​between jsp page and java class?

Expert Beiträge: 284 Beitrittsdatum: 07.12.10 Neueste Beiträge
Hi Raja,
i don't undestand,
can you explain better?
Thanks,
Luca
Henal Saraiya, geändert vor 12 Jahren.

RE: How to pass values ​​between jsp page and java class?

New Member Beiträge: 18 Beitrittsdatum: 13.07.11 Neueste Beiträge
Hi Abzal,

Well whenever you submit the form all your controls which are placed inside form tag gets automatically pass to the request object.

So in your action class you can very well retrieve it via request.getParameter("controlName");

Thanks,
Henal Saraiya