I am new in jsp domain and I don't know how to attribute a String value from a message because it give me error
<%String mesg=%>
<fmt:message key="msg_valid" bundle="${message}" />
<%;%>
The error is this:
Syntax error on token "=", ; expected
As nicely stated in one of the post by Aniket Kulkarni:
JSTL variables are actually attributes, and by default are scoped at
the page context level. As a result, if you need to access a JSTL
variable value in a scriptlet, you can do so by calling the
getAttribute() method on the appropriately scoped object (usually
pageContext and request)
You can try this:
<%
String mesg= (String)pageContext.getAttribute("message");
out.println(resp);
%>
Related
First jsp page contains code:
<a href='select.jsp?param1=${person.name}'>link to other jsp</a>
In html this link refers to:
http://sitename/select.jsp?param1=gsdf
A code from select.jsp page:
<c:out value="${param1}">No name</c:out>
<br/><%=request.getParameter("param1")%>
But I get:
No name
gsdf
Why the value of param1 did not pass to second jsp in the case of using c:out?
you need to use EL (JSP Expression Language).
from javaDoc :
param: Maps a request parameter name to a single value
so you juste need to do something like
<c:out value="${param.param1}"/>
You can send Using Session object.
session.setAttribute("prsonName", prsonName);
These values will now be available from any jsp as long as your session is still active.
Object userid = session.getAttribute("prsonName");
How can i pass parameters to a function while calling it through JSTL.
<c:forEach items="${sessionScope.student_var.details(x)}" var="display_details"> </c:forEach>
I am trying to pass parameter x to a function but it is not working
If the variable is defined as follows in the JSP:
<% String x = "age"; %>
You have two options to get the expression to evaluate using x as an argument.
1. Leave the definition as is and set a PageContext attribute
<% pageContext.setAttribute("x", "age"); %>
An expression evaluates variables by looking for attributes by key name. So when your expression is evaluated, the engine is searching for an attribute with the key name 'x' in the pageContext, request, session, and servletContext scopes.
2. Move the definition of the variable to the backend
request.setAttribute("x", "age");
This statement would be found in your servlet/controller, setting the attribute before we reach the JSP.
I am trying to get attribute of spanId and set Attribute by using request. Then I want to pass the output. Although the first input is has value, it still return me null.
Below are my codes. Help will be appreciate! :)
<input id="spanId" name="spanId">
<%
String spanId = request.getParameter("spanId");
request.setAttribute("spanId",spanId);
%>
<%= request.getParameter("spanId") %>">
Use the getAttribute method instead of getParameter() like
<%= request.getAttribute("spanId") %>">
Just think if you are setting an attribute to request object how you can get it? by using getAttribute.... String spanId = request.getAttribute("spanId").
Also request attribute has request scope (scope lasts only till 1 request).
More on this over here http://docs.oracle.com/javaee/1.4/api/javax/servlet/jsp/JspContext.html
Avoid using scripting elements in your JSP.. remember you are tightly coupling your presentation and business logic.
I'm trying to retrieve attribute values set by a servlet in a JSP page, but I've only luck with parameters by ${param}. I'm not sure about what can I do different. Maybe its simple, but I couldn't manage it yet.
public void execute(HttpServletRequest request, HttpServletResponse response) {
//there's no "setParameter" method for the "request" object
request.setAttribute("attrib", "attribValue");
RequestDispatcher rd = request.getRequestDispatcher("/Test.jsp");
rd.forward(request,response);
}
In the JSP I have been trying to retrieve the "attribValue", but without success:
<body>
<!-- Is there another tag instead of "param"??? -->
<p>Test attribute value: ${param.attrib}
</body>
If I pass a parameter through all the process (invoking page, servlets and destination page), it works quite good.
It's available in the default EL scope already, so just
${attrib}
should do.
If you like to explicitly specify the scope (EL will namely search the page, request, session and application scopes in sequence for the first non-null attribute value matching the attribute name), then you need to refer it by the scope map instead, which is ${requestScope} for the request scope
${requestScope.attrib}
This is only useful if you have possibly an attribute with exactly the same name in the page scope which would otherwise get precedence (but such case usually indicate poor design after all).
See also:
Our EL wiki page
Java EE 6 tutorial - Expression Language
Maybe a comparison between EL syntax and scriptlet syntax will help you understand the concept.
param is like request.getParameter()
requestScope is like request.getAttribute()
You need to tell request attribute from request parameter.
have you tried using an expression tag?
<%= request.getAttribute("attrib") %>
If the scope is of request type, we set the attribute using request.setAttribute(key,value) in request and retrieve using ${requestScope.key} in jsp .
I am redirecting to a JSP that has to print the whole incoming query string. Like in this other question, Request parameter in jsp page, I do not want to access one parameter but the whole query string which I would accomplish in a scriptlet like: <%= request.getQueryString() %>
Thanks!
You can get the paramater object by OGNL stack value #parameters
http://struts.apache.org/2.0.14/docs/ognl-basics.html
If you want to iterate it, you can do something like ( this example create hidden input for each param)
<s:iterator value="#parameters" var="param">
<s:hidden name="%{#param.key}" value="%{#param.value}" />
</s:iterator>
You can user s:iterator tag in struts2 and you can get your string value in Jsp by OGNL lang which is supported by Struts2 without writing code in Scriptlet.
Please check below links for your reference.
http://www.vaannila.com/struts-2/struts-2-example/struts-2-ognl-expression-language-example-1.html
http://www.vaannila.com/struts-2/struts-2-example/struts-2-iterator-tag-example1.html