Passing dynamic name from one jsp to another - java

I wanted to know, how I could pass a dynamic value from one JSP to another.
For example, I want to know what I need to call to pass the value of name parameter to JSP2, when I clicked on the link in JSP1. I have tried following, but it's not working.
in JPS1:
<a href="JPS2.jsp" name="<%= packets.getString("BatchNo")%>">
in JPS2:
<% String UN = request.getParameter("name");%>

If you want to pass the value of packets.getString("BatchNo") from first.jsp to second.jsp, you can do that as below.
first.jsp :
LINK
second.jsp :
<% String un = request.getParameter("name");%>

Related

Assigning form input value to java variable in jsp

I have a scenario in which i am rendering a jsp page .One of my value is in my model Form entity.I am assigning it in one of my form input like below :
<s:hidden name="myModelName.myUserName"/>
I am trying to assign it to my java variable inside my jsp.I tried accessing the model directly like this
<%= String myName = myModelName.myUserName %>
But i get error message "myModelName cannot be resolved.I have then tried accessing from the hidden field.But i dont know how to use it.Anyway i need the value in java variable inside jsp for some reason.Anyone help me out how to do it
You can do something like this:
assign your model property value to variable userName
<c:set var="userName" scope="session" value="${myModelName.myUserName}"/>
and then you can output it using this tag:
<c:out value = "${userName}"/>
You can read more about JSTL tags here: Using JSTL tags
Hope this helps. Ask if you need anything else.

Passing value from one jsp to another

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");

Passing values in `<portlet:param>` using EL

I'm trying to pass a parameter with <portlet:actionURL> to a portlet in liferay, but it turns out that, using EL to pass values in not working, however, using JSP expression tag is working fine.
Here's my relevant code:
<%
ResultRow row = (ResultRow)request.getAttribute(WebKeys.SEARCH_CONTAINER_RESULT_ROW);
Course course = (Course) row.getObject();
long groupId = themeDisplay.getLayout().getGroupId();
String name = Course.class.getName();
String primaryKey = String.valueOf(course.getPrimaryKey());
%>
<liferay-ui:icon-menu>
<c:if test="<%= permissionChecker.hasPermission(groupId, name, primaryKey, ActionKeys.UPDATE)%>">
<portlet:actionURL name="editCourse" var="editURL">
<portlet:param name="resourcePrimaryKey" value="${primaryKey}"/>
</portlet:actionURL>
<liferay-ui:icon image="edit" message="Edit" url="${editURL}" />
</c:if>
</liferay-ui:icon-menu>
As you see, in <portlet:param> tag, I have used EL for passing value attribute. But it doesn't work, and I receive the value as 0 for "resourcePrimaryKey" in my action method, when I do:
long courseId = ParamUtil.getLong(request, "resourcePrimaryKey");
// courseId is 0 here
However, if I use JSP expression tag in place of EL, it works fine:
<portlet:actionURL name="editCourse" var="editURL">
<portlet:param name="resourcePrimaryKey" value="<%= primaryKey %>"/>
</portlet:actionURL>
Now, I get the required value for "resourcePrimaryKey".
Can anyone figure what's going on here? Surprisingly, EL at other place work fine, as you see - ${editURL} value for url attribute, is working fine, and redirecting to the corresponding url.
I came across this thread on apache mail archive regarding the same issue, but that doesn't really solve the problem.
A variable in the scriptlet cannot be used directly in an EL, you would first need to set it like:
<c:set var="primKey"><%=primaryKey %></c:set>
and use ${primKey} or set it as a request attribute:
request.setAttribute("primKey", primaryKey);
Clearly, better would be to directly use the expression.
Also regarding the ${editURL} working, it is a portlet jsp tag which sets the variable in the page context so that it is available to the EL.
Our el wiki is a good place to know these things, look out for the heading Make objects available to EL for this question :-)

Pass value of text box to java servlets

Alright cannot find this anywhere and I was wondering how to grab the values of a text box from a jsp or servlet and display it in another servlet.
Now my issue isn't passing the data and actually displaying it, my issue is that whenever a space is in the value I can only get that first bit of information. For example:
<form method="post" action="Phase1Servlet">
<p>Favorite Place:</p> <input type="text" name="place"></div>
<input id="submit" type="submit" value="Submit">
</form>
Say The user types in "The Mall"
in the Servlet I use:
String place = request.getParameter("place");
Then output the variable place somewhere in my code I only get the word "The"
Do I need to use request.getParameterValues("place"); instead? If so how do I pass the values from servlet to servlet through a hidden field? When I do this:
String [] placeArr = request.getParameterValues("place");
out.println("<input type=\"hidden\" name=\"place\" value="+ placeArr +">");
The hidden field actually stores [Ljava.lang.String;#f61f5c
Do i have to parse this or convert this somehow?
Should be
String placeArr = request.getParameterValue("place");
out.println("<input type=\"hidden\" name=\"place\" value=\""+ placeArr +"\">");
Escape the string in the hidden field
Are you really sure that when you use
String place = request.getParameter("place");
the place variable contains only word before first space? Because it is rather weird situation. If you want to pass a parameter to another servlet(assuming that another servlet is called from this servlet) you can set a request attribute in first servlet and then dispatch that request to another servlet, for example:
request.setAttribute("place", "The mail");
RequestDispatcher dispatcher=getServletContext().getRequestDispatcher( path_to_another_servlet );
dispatcher.forward( request, response );
and then in another servlet ypu can use it as:
String place = request.getAttribute("place");

How to access query string using Struts 2 tags from a 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

Categories