jstl - redirect keeping the old parameters - java

I want to call my web site like:
http://localhost:8080/?co=grav
When doing this the authentication page configured in web.xml comes up and the login is done through form login. After that it will go to index.jsp where I am doing a redirect to one of my main pages of the site:
<c:redirect url="index-userapp.jsp" >
The problem is that, this redirect does no longer forward my co parameter, and I really need to not loose it...
Do you see a workaround?
I think it can be done like:
<c:redirect url="index-userapp.jsp">
<c:param
name="co"
value=""></c:param>
</c:redirect>
but how I put in the new value, the old parameter value from the initial request?

try this:
<c:redirect url="index-userapp.jsp">
<c:param name="co"
value="${param.co}"></c:param>
</c:redirect>

Related

Redirect whole page from imported JSP

I have two jsp, let's say A.jsp and B.jsp. A.jsp has the following code:
<c:import url="B.jsp" >
<c:param name = "page_title" value = "Title" />
</c:import>
In B.jsp i need to check some conditions and do a redirection to Login.jsp. I achieve this by doing response.sendRedirect(Logn.jsp)
The problem is that the redirection is made on B.jsp so the result is that the browser displays the content of Login.jsp and A.jsp but i need to redirect the whole page to Login.jsp. That is, the browser should only show Login.jsp.
Consider that the redirection must be made on B.jsp unless there is a way that B.jsp can tell A.jsp the url to redirect to.
EDIT: A.jsp and B.jsp belong to different projects
In your B.jsp, instead of doing a redirect, set a flag on the request as
<c:set var="login" value="true" scope="request" />
Then in A.jsp check the flag and redirect if present.
<c:if test="${login == 'true'}">
<c:redirect url="/login.jsp" />
</c:if>
Try doing a jsp:include instead.
<jsp:include page="B.jsp" >
<jsp:param name="page_title" value="Title" />
</jsp:include>
There are two parts to solving this:
1) Making sure that you are not sending any HTML to the client in A until you have performed the logic in B. That means that you must call the B.jsp early (part of good MVC design).
2) Use the var parameter in your c:import:
<c:import url="B.jsp" var="output">
Now the output of the call to B will be in a buffer called "output" instead of being sent directly to the client. That allows you to make decisions in A (after the call to B). If you decide you want to redirect, you can, because you have dumped nothing (except some white space probably) to the browser. If you decide you want to send the output of B to the browser instead you can simply do this after the c:import:
${output}
There is even a trick to get rid of the white space being sent to the browser if that becomes necessary, but it rarely is (if you send too much white space the buffer will commit and then you cannot redirect, but that's rare and an entirely different question).
Make sense?

How to respond to 'included' JSPs from Servlets

I have a login.jsp form that I have written for a website that just contains a form for logging in that I can include and re-use in different places. The first place I am including it is in my index.jsp homepage.
I have an HTTPServlet that the form submits to, and if the username/password is invalid the Servlet sends a message back to display to the user, otherwise it forwards them on to their homepage.
My problem is that I want to forward them back to the same page they're on if their details are incorrect, but display the message, but forwarding to the login page displays only that form in the browser, outside of the page it was included in. Is there any way I can forward back to the current page? Or is this perhaps not the best way to go about this?
Thanks for reading.
I am presuming you are using a <jsp:include>. Here is something you might do:
<jsp:include page="loginForm.html">
<jsp:param name="currentPage" value="${whateverTheCurrentPageIs}" />
</jsp:include>
Include the currentPage variable in the loginForm. Then, in your servlet, you want to redirect/forward to the URL you passed in that currentPage variable.

c:url tag includes jsession id query string

What is the best way of obtaining context-root on a jsp page. If I hardcode my css/image to "/myapp/images/foo.gif" then it will be a broken link if the context root / app name is changed. Using relative path is not always preferrable because a context root can be multi-path (eg: /mysite/myapp)
So far I've tried using <c:url var="root" value="/"/> which works alright (${root} will give the context-root /myapp/), however if this is the very first time user is visiting the site (or if cookie is cleaned on the browser), the value assigned to ${root} became something like /myapp/;jsessionid=019762g1hk3781kh98uihilho and it breaks the images/css reference. Is there any other better way than this?
So far I've tried using <c:url var="root" value="/"/> which works alright (${root} will give the context-root /myapp/)
This is not the right way. The <c:url> should be applied on every single URL individually.
You'd better use
<c:set var="root" value="${pageContext.request.contextPath}" />
See also:
Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP

HttpUtil.encodeUrl not appending jsessionid when cookies are disabled?

I'm developing a liferay theme on which I have to place some links to other pages. Now I wanted to put those links like ${httpUtil.encodeUrl("\myPage"")}. However when I disable cookies and visit the page I still see the page without the jsessionid appended to the myPage url. Does anyone know why this happens and a possible sollution?
I also tried some velocity functions without any success.
If you use jsp to render your page, than you have to use <c:url> to print the url.
Added:
If you can not use <c:url> and you need to do it by hand, then have a look at the Implmentation of <c:url>.
Added:
In JSPs you have to "wapp" <c:url> by <c:out> to get the enhanced (by session id) url correct formated.
<c:url var='urlWithSession' value='\myPage' />
my page

Using request.setAttribute in a JSP page

Is it possible to use request.setAttribute on a JSP page and then on HTML Submit get the same request attribute in the Servlet?
No. Unfortunately the Request object is only available until the page finishes loading - once it's complete, you'll lose all values in it unless they've been stored somewhere.
If you want to persist attributes through requests you need to either:
Have a hidden input in your form, such as <input type="hidden" name="myhiddenvalue" value="<%= request.getParameter("value") %>" />. This will then be available in the servlet as a request parameter.
Put it in the session (see request.getSession() - in a JSP this is available as simply session)
I recommend using the Session as it's easier to manage.
The reply by Phil Sacre was correct however the session shouldn't be used just for the hell of it. You should only use this for values which really need to live for the lifetime of the session, such as a user login. It's common to see people overuse the session and run into more issues, especially when dealing with a collection or when users return to a page they previously visited only to find they have values still remaining from a previous visit. A smart program minimizes the scope of variables as much as possible, a bad one uses session too much.
You can do it using pageContext attributes, though:
In the JSP:
<form action="Enter.do">
<button type="SUBMIT" id="btnSubmit" name="btnSubmit">SUBMIT</button>
</form>
<% String s="opportunity";
pageContext.setAttribute("opp", s, PageContext.APPLICATION_SCOPE); %>
In the Servlet (linked to the "Enter.do" url-pattern):
String s=(String) request.getServletContext().getAttribute("opp");
There are other scopes besides APPLICATION_SCOPE like SESSION_SCOPE. APPLICATION_SCOPE is used for ServletContext attributes.
If you want your requests to persists try this:
example: on your JSP or servlet page
request.getSession().setAttribute("SUBFAMILY", subFam);
and on any receiving page use the below lines to retrieve your session and data:
SubFamily subFam = (SubFamily)request.getSession().getAttribute("SUBFAMILY");
Try
request.getSession().setAttribute("SUBFAMILY", subFam);
request.getSession().getAttribute("SUBFAMILY");
Correct me if wrong...I think request does persist between consecutive pages..
Think you traverse from page 1--> page 2-->page 3.
You have some value set in the request object using setAttribute from page 1, which you retrieve in page 2 using getAttribute,then if you try setting something again in same request object to retrieve it in page 3 then it fails giving you null value as "the request that created the JSP, and the request that gets generated when the JSP is submitted are completely different requests and any attributes placed on the first one will not be available on the second".
I mean something like this in page 2 fails:
Where as the same thing has worked in case of page 1 like:
So I think I would need to proceed with either of the two options suggested by Phill.
i think phil is right request option is available till the page load. so if we want to sent value to another page we want to set the in the hidden tag or in side the session if you just need the value only on another page and not more than that then hidden tags are best option if you need that value on more than one page at that time session is the better option than hidden tags.

Categories

Resources