Passing portletSession value from Java to Javascript - java

I want to pass a value from my Java class to Javascript. I have set the value in portletSession. I don't know how to get a value from portletSession into the Javascript.
Here is the code I have used to set the value in the portletSession.
PortletSession portletSession = request.getPortletSession();
portletSession.setAttribute("noExist", noExist);
How can I do that?

Don't use portletSession, use Cookie instead. You can access cookies from javascript using document.cookies
But there is another way: just print the variable into script block and javascript can read it.
var variableFromJSP = <% print variable here %>;
hope this help

Related

How do I retrieve data from jsp pageContext

The following line of java code is giving me null pageContext.getRequest().getParameter("id");
When I print it out in jsp page using <%=request.getAttribute("id")%> I am able to see the data stored inside.
Is there another way to retrieve the data from the jsp page and assign it to a java variable?
Yes you can do it. Place your code between <% %>
<%
String userID = request.getParameter("userid");
out.println(userID);
%>
If you want to avoid scriptlets you can use JSTL:
<c:set var="userId" value="${param.usedId}"/>
In JSP you can use internal java function
if do you want to pull a query parameter you simply have to write
<%request.getParameter("id") %>
if do you want to assign something like you want to put a data inside P tag you simply have to write
<p><%=request.getParameter("message")%></p>
in this case your java message will be inside P tag
and like javascript variable
<script>var id=<%=request.getParameter("id")%></script>
here you have assigned id (javascript variable) with query parameter pulled from native java function, id do you want to assign normal java variable inside jsp you have simply write
<% String message=request.getParameter("message")%>
Hope this help :)

How to use Value of an Variable of JSP page in JAVA CLASS

I know its easy to use values of form ,from jsp to java,But how to use a variable value of JSP code into java class.
For e.g., I want to use value of vlaue in a java class
any help will be appreciated,thanx
<%
String value=null;
value= (String) session.getAttribute("name");
%>
Please be more specific. Where you need to access your request or session data?
You can get all data at you servlet code the same way as at JSP (i fact, JSPs are being compiled to servlets behind the curtain):
request.getSession().getAttribute("some");
If the data comes from a jsp/html then you should use:
request.getParameter("value")
if the data is saved in the session then get it from the session using:
req.getSession().getAttribute("value");
Then I also suggest you ensure it is not null:
String value = (String) request.getParameter("value");
if(value != null){
// the value is at the form, so you can get it and use it
}
else{
//the value is not at the html or the value is not given a value
}

pass attribute from Java to JavaScript

In my Java Class I added a variable:
model.addAttribute("pageID", "dashboard");
Inside JSP I can easily call that variable with ${pageID}. But is there a way I can call that variable directly in JavaScript? I tried all other suggestion I found over the internet, such as simply writing var value = "${pageID}" but that doesn't work.
Any suggestions ?
Have u tried this?
var value = "<%= pageID %>";

How to set a cookie value within JSP using an EL expression?

I'm trying to set a cookie value within a JSP without using Java code directly. I know I could do it by creating a custom tag lib for that, but I wanted to keep it simple so I'm trying to do that the same way I access cookies: with EL expressions.
I know I can read the value of a cookie using JSP EL with the expression ${cookie['cookieName'].value}, but how can I set a particular value to that cookie using EL? I found solutions using java code in the JSP, but I want to avoid that.
So far I found ways to set variables using the c:set tag, but that doesn't accept expressions as the 'var' parameter so I can't do something like:
<c:set var="${cookie['cookieName'].value}" value="123" />
I think the way to go is , but I don't know what expression to use for the var part of it, or how to write it so I can set the cookie value instead of just a variable.
Any help is appreciated!
There is no standard expression to set cookie in JSP. You can use custom tag if you want OR use JSP script-less
<%
javax.servlet.http.Cookie cookie
= new javax.servlet.http.Cookie("name", "value");
// cookie.setXXX()
response.addCookie(cookie);
%>
NOTE: Make sure cookie is added BEFORE the response is committed.

How do set request parameter value while calling java method?

I need to call a method of java class from jsp page.. While calling that in jsp page, need to set some request parameters also. How do i do that in jsp page?
Ex :
Java class :
public void execute() {
string msg = request.getParameter("text");
}
JSP file :
I need to call the method here and also need to set parameter values (eg : &text=hello)
Please help me...
Just embed your Java code inside the JSP. The object "request" is available to get parameters (parameters can't be set). Unless you plan to forward to another JSP in another context with an attribute should be enough.
To refer to your own class do not forget to add the import statement at the beginning of the JSP (similiar to an import in a Java class).
<!-- JSP starts here -->
<%#page import="test.MyClass" %>
<%
MyClass myClass = new MyClass();
String text=myClass.execute();
// This is not neccessary for this JSP since text variable is already available locally.
// Use it to forward to another page.
request.setAttribute("text");
%>
<p>
This is the <%=text%>
</p>
You cannot set request parameters. I suggest you try using the attributes as suggested in the previous answers.
You can't set request parameters - they're meant to have come from the client.
If you need to give the method some information, you should either use normal method parameters, or some other state. The request parameters are not appropriate for this, IMO.

Categories

Resources