How to get the session values in jsp - java

here is my samle request.jsp
pageContext.setAttribute("test", "ccavenue", PageContext.SESSION_SCOPE);
<form id="nonseamless" method="post" name="redirect" action="http://test.ccavenue.com/transaction/transaction.do?command=initiateTransaction"/>
<input type="hidden" id="encRequest" name="encRequest" value="<%= encRequest %>">
<input type="hidden" name="access_code" id="access_code" value="<%= accessCode %>">
<script language='javascript'>document.redirect.submit();</script>
</form>
here is my response.jsp like
String test = (String) pageContext.getAttribute("test", PageContext.SESSION_SCOPE);
out.print(test);
i am setting the pageContext value in request.jsp, redirect the request to ccavenue site and they send the response to response.jsp but in response.jsp getting the pageContext value is null
please help me!

Try using session instead:
session.setAttribute("test", "ccavenue");
response.jsp
String test = (String) session.getAttribute("test");
pageContext.setAttribute():
From docs
Session scope (only valid if this page participates in a session): the
named reference remains available from the HttpSession (if any)
associated with the Servlet until the HttpSession is invalidated.

PageScope
What you put on your page scope is available only for the JSP page
that put it. Any other page in the same request included via or
forwarded will not see object in the first page scope.
May be you should try Session scope or Request Scope for that !!

Instead of pageContext use request.setAttribute("val","value") in request.jsp and request.getAttribute("val") in response.jsp or set to session and use value="${value}" in jsp

Related

JSP not getting request parameters

I have a JSP page which is not seeing any of the request parameter values when displayed. Originally I tried with passing the parameters from a Servlet, which did not work. Just as a test I also tried calling that JSP from a form on an html page.
What I do in Servlet:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String sampleValue = sampleModel.getMyValue();
request.setAttribute("param", sampleValue);
RequestDispatcher view = request.getRequestDispatcher("samplePage.jsp");
view.forward(request, response);
}
How I call JSP from an HTML page through a form with hidden fields:
<div>
<form action="samplePage.jsp" method="post">
<input name="param" type="hidden" value="sampleValue"/>
<input type="submit" value="Update">
</form>
</div>
Finally what I have on the JSP:
<body>
<p>Some info: ${param}</p>
</body>
As I said the problem is the value of the request attribute "param" which is lets say "sampleValue", does not get rendered on the page.
I have seen lots of examples how this is done and I think my code is correct. Is there any other reason why this may not be working? I am running a maven project with Tomcat 8.5.
EDIT: What I have found out so far is that the problem is not that the Expression language is not working. The request attribute just has no value when it arrives at the JSP.
Please ensure that isELIgnored is false in your jsp page.use bellow tag at the top of your jsp.
<%# page isELIgnored="false" %>
also you can ensure this by ${2 * 4} output is print as 8 on JSP.
Your form is using method=post. Your Servlet code should be located on the doPost method instead of doGet.
For Servlet case, replace ${param} in your samplePage.jsp with
<%=request.getAttribute("param")%>
For JSP case, replace ${param} in your samplePage.jsp with
<%=request.getParameter("param")%>
First, check whether variable sampleValue is capturing the string that you are passing from JSP like below
String sampleValue = sampleModel.getMyValue();
System.out.println(sampleValue);

Passing parameter back from JSP in a portlet app

In my Liferay 6 app I'm able to pass parameter from java to jsp via:
final PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher("view");
request.setAttribute("description", "some description");
rd.include(request, response);
Then I want user to change the description and pass it back to back-end:
<form method="POST" action="${addItem}">
<input name="description"
type="text"
value="${description}"/>
<button type="submit">UPDATE</button>
</form>
Nevertheless when I call then System.out.println("request.getAttribute("description")); , I'm getting null. What am I doing wrong?
Youre passing in the parameter but checking the request attribute (assuming that the outer quotes are a question typo). Based on the information you provided, the initial request attribute was only available in the JSP but not any subsequent servlet. Try
System.out.println(request.getParameter("description"));

JSP pass hidden input value to servlet when the page load

I should be using getRemoteUser functionality to get the logged in user. Until the authentication part get created I am trying to hard code the user in the jsp page and pass that in the my servlet. But when I try to print out the value its null:
<input type="hidden" name="userId" id="userId" value="123456789" />
Here is how I tried to get the user:
String fakeUser = request.getParameter("userId");
PrintWriter out = response.getWriter();
out.println(fakeUser);
System.out.println(fakeUser)
I also tried the solution mentioned following Stackoverflow post but that didn't work either.
passing value from jsp to servlet
As you are trying to use hidden-form field I assume that you are trying to do some sort of state management.
try something like this
<form action="urlOfYourServlet" method="post">
Enter your name : <input type ="text" name = "name">
<input type="hidden" name="hidden" value="Welcome">
<input type="submit" value="submit">
</form>
In servlet
String getHiddenValue=request.getParameter("hidden");
String name=request.getParameter("name");
System.out.println(name+" Hidden field Value is :"+getHiddenValue);
Disadvantage :
Only textual information can be persisted between request.
This method works only when the request is submitted through input form
Instead try url-redirecting or HttpSession

Liferay hook input post on same page

I'm trying to retrieve a post value from a hook on the same page so when i value is right, content will appear.
I added this code in the hook to have it on the specified page.
<portlet:actionURL secure="<%= PropsValues.COMPANY_SECURITY_AUTH_REQUIRES_HTTPS || request.isSecure() %>" var="SecondloginURL">
<portlet:param name="saveLastPath" value="0" />
<portlet:param name="struts_action" value="/journal_content/view" />
</portlet:actionURL>
<aui:form action="" name="auth" method="POST">
<aui:input label="Second Password" type="password" name="password" />
<aui:button type="submit" value="authenticate" />
</aui:form>
I managed to retrieve the value but when it's validated session started but it doesn't move cross-pages.
Here is the code:
<% String pass = request.getParameter("password"); %>
<c:if test="<%= pass.equals(\"1234\") %>">
<%
HttpSession session1 = request.getSession();
session1.setAttribute("pass","authenticated");
String foo = (String) session1.getAttribute("pass");
out.println(foo);
%>
<h2>this is the second password and it's working</h2>
<div class="journal-content-article" id="article_<%= articleDisplay.getCompanyId() %>_<%= articleDisplay.getGroupId() %>_<%= articleDisplay.getArticleId() %>_<%= articleDisplay.getVersion() %>">
<%= RuntimePortletUtil.processXML(application, request, response, renderRequest, renderResponse, articleDisplay.getContent()) %>
</div>
</c:if>
In a Liferay hook you can override stock Liferay jsps, thus also add forms.
You can also override Liferay Actions and other Liferay classes. You don't make clear where you put the above code and what else you have in your hook, neither what you are trying to achieve.
First: You should direct your form to some <portlet:actionURL/> and then, depending on the portlet this jsp is invoked from, you'll have to implement or override the action handler for that portlet. In there you'll be able to get the parameter value from the ActionRequest

in JSP is there a way to pass HttpServletRequest object as an attribute to another HttpServletRequest object

I am using Struts/JSP for a webapp. I have a page A where I am collecting user defined parameters (as request parameters can't make them session params) and then I go to page B to ask yes/no kind of a question to the user. In case of yes I need to come back to page A and continue regular processing. But obviously the request object for page A is gone.
Is there a way to set page A's request object as parameter in page B so that when I come back to page A i have the same request object I had when i was there (on page A) the first time.
I need something like below:
page A --(req1)------> page B (set req.setAttr('prevReq', req1)) ------> page A (req = req.getAttr('prevReq'))
Any help is appreciated.
No, you can't do what you have in mind. Do you understand how the HTTP request-response cycle works?
User sends HTTP request to the server using a browser.
Server processes the request (your servlet or JSP is called).
Your servlet or JSP produces a response which normally consists of an HTML page.
The server sends the response back to the browser.
There is no way that you can save the request for page A, and then in page B respond to that request to make the browser go back to page A. That's just not how the request-response cycle works.
What you can do, is store data in the session object. You can call request.getSession() to get a HttpSession object, in which you can store data for the duration of the session of that user. In page A, you can get the data out of the session object again.
In a multi page process you will need to store all the intermittently gathered data into the session. See HttpServletRequest.getSession() and HttpSession.setAttribute(String, Object).
Use hidden input elements (input type="hidden") wherein you retain the request parameters of the form submit. Don't duplicate/store it as request attribute. They get lost when the response is finished.
Since I don't do struts, here's a basic example how the JSP should look like (leaving input labels and obvious security issues like XSS outside consideration, Struts should be smart enough to handle it itself).
Page A:
<form>
<input type="text" name="input1" value="${param.input1}">
<input type="text" name="input2" value="${param.input2}">
<input type="text" name="input3" value="${param.input3}">
<input type="hidden" name="yesorno" value="${param.yesorno}">
<input type="submit" value="go to page B">
<input type="submit" value="submit">
</form>
Page B
<form>
<input type="checkbox" name="yesorno" value="yes" ${!empty param.yesorno ? 'checked' : ''}>
<input type="hidden" name="input1" value="${param.input1}">
<input type="hidden" name="input2" value="${param.input2}">
<input type="hidden" name="input3" value="${param.input3}">
<input type="submit">
</form>

Categories