HttpUtil.encodeUrl not appending jsessionid when cookies are disabled? - java

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

Related

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

What does this expression language ${pageContext.request.contextPath} exactly do in JSP EL?

I have a web app, where I have different navigation anchor tags such as Home, Profile and etc.
What I want:
When I press anchor tags like home or profile. I just want to ensure that current user gets its information in that Tags/JSP Page.
Sample Example that I am trying:
Profile
The pageContext is an implicit object available in JSPs. The EL documentation says
The context for the JSP page. Provides access to various objects including:
servletContext: ...
session: ...
request: ...
response: ...
Thus this expression will get the current HttpServletRequest object and get the context path for the current request and append /JSPAddress.jsp to it to create a link (that will work even if the context-path this resource is accessed at changes).
The primary purpose of this expression would be to keep your links 'relative' to the application context and insulate them from changes to the application path.
For example, if your JSP (named thisJSP.jsp) is accessed at http://myhost.com/myWebApp/thisJSP.jsp, thecontext path will be myWebApp. Thus, the link href generated will be /myWebApp/JSPAddress.jsp.
If someday, you decide to deploy the JSP on another server with the context-path of corpWebApp, the href generated for the link will automatically change to /corpWebApp/JSPAddress.jsp without any work on your part.
Include <%# page isELIgnored="false"%> on top of your jsp page.
use request.getContextPath() instead of ${pageContext.request.contextPath} in JSP expression language.
<%
String contextPath = request.getContextPath();
%>
out.println(contextPath);
output: willPrintMyProjectcontextPath
For my project's setup, "${pageContext.request.contextPath}"= refers to "src/main/webapp". Another way to tell is by right clicking on your project in Eclipse and then going to Properties:

jstl - redirect keeping the old parameters

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>

Calling a webpage from another webpage via jsp

I am trying to figure out how to do the following.
I have webpage at a certian location called www.hello.com/logout.jsp
What I am trying to do with logout.jsp is delete all the cookies that were stored when initially logging in. The problem is that there exists a cookie for a website with a different domain that is stored when logging in. The one way I can delete that cookie is through the logout link for that website e.g. www.hello2.com/logout.jsp
Is there anyway I can call www.hello2.com/logout.jsp from www.hello.com/logout.jsp?
I am trying to just make a call for www.hello2.com/logout.jsp from www.hello.com/logout.jsp and then redirect the user to another page on www.hello.com
Thanks in advance :D
If I understand correctly, you are trying to do an HTTP POST( or GET ) to www.hello2.com/logout.jsp while processing HTTP request to your web application's logout.jsp.
You should really consider coding your logic in Servlets and using JSPs only to present data, but in the meantime you can create a scriptlet inside your logout.jsp and do a call to another web page in there ( just don't code the whole thing in JSP, only make a call to a static method ).
In that static method you can use HttpClient to do whatever HTTP request you need from www.hello2.com.
Here are additions to your logout.jsp
<%# page import="my.package.Hello2Call" %>
<%
Hello2Call.postLogoutRequest( );
%>

Categories

Resources