Why won't my JSP remove cookie - java

I want to delete a cookie in my login page, no matter what I do, it just won't go away. in chrome developer, it shows the response from the server contains no cookie even though I had add it using response.addCookie().
Here's the JSP code that removes the code. It's at the beginning of the page, right after the content-type being set and before any output. this JSP is in the tile view (not sure if it makes any difference). I also tried moving this code to the containing JSP, but no help either.
<%# page contentType="text/html; charset=UTF-8" %>
<%
// remove cookie. all the properties (domain, path secure) match those those when the cookie was being created and set.
Cookie ck = new Cookie(someName,"");
ck.setMaxAge(0);
ck.setValue("");
ck.setPath("/");
ck.setDomain(someDomain);
ck.setSecure(true);
response.addCookie(ck);
System.out.println("wwwwwww 999991111");
%>
thanks.

Use
cookie.setPath(request.getContextPath());
your code only works if the application is deployed to the root.

Related

jsp ${pageContext.request.contextPath} dosent get requested

Currently i have a .jsp project where my welcome page is a servlet
<welcome-file>frontpage</welcome-file>
The servlet sets gets two ressources, a header file containing the < nav> and a footer containing the < footer>
request.setAttribute("header1", sc.getResource("/includes/nav.jsp").toString());
request.setAttribute("footer", sc.getResource("/includes/footer.jsp").toString());
And forwards to the index.jsp page
getServletContext().getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request, response);
My question is.
When i get the ressource (footer.jsp), how can i in the footer.jsp dynamically import / include images?
I tried the following
<img src="${pageContext.request.contextPath}/images/picture1.png" alt="picture1"/>
But the expression ${pageContext.request.contextPath} gets treated as a string instead of a command, and does not get the context path.
I suspect its because the content of the footer.jsp is fetched in this manner and their for the context path isint actually ever requested within the footer.jsp.
But how do i solve this?
add <%# page isELIgnored="false" %> in top of your JSP page, to enable expression language.
and to include a JSP page with other use <jsp:include like:
<jsp:include page="/includes/nav.jsp"/>
<jsp:include page="/includes/footer.jsp"/>
This is not the way to include stuff. Use jsp:include action to include the header/footer. If for some reason you really want to do it in the servlet, see this post. As long as you just grab a resource like you do, you're reading the file like any text, there is no JSP compilation/evaluation.

how do i get utf8 value from querystring using jsp

I am using JSP pages and I am trying to get the UTF-8 value from query string using the statements as below request.getparameter("q");
It is working fine, it gives me the appropriate results but when I am trying it using IE9 it gives me ????? instead of unicode value.
My question is how do I get proper unicode value from query string using JSP that will gives correct values on all browser including IE, and what statements I need to add within JSP page to get correct values at IE as well.
Please help me in this regard.
thank you
In the jsp page directive you need to set content-type to utf-8 (for each jsp page)
<%# page contentType="text/html; charset=UTF-8" pageEncoding="ISO-8859-1" %>
If still problem persist then use this SO question to handle encoding for DB, Tomcat. HERE

How to transfer parameter to redirect web

We know that we can redirect an website by putting this below the tag:
<head>
<meta content=0;url=http://www.google.com http-equiv='Refresh'/>
</head>
But what happen if the link I want to redirect is not google.com but it is stored in a string variable that I processed from my Java code? I know if it load from head down, then it will redirect immediately to that page without going through the following code right? So, how to combine the string that I extracted and let the website to load to that link?
From JSP (or servlet):
<%
response.sendRedirect(redirectURL);
%>
Where redirectURL is the variable containing the page you want to go to.
Ok, finally I figured it out:
First, need to construct the index.jsp and its java class in an web app:
https://netbeans.org/kb/docs/web/quickstart-webapps.html
then in the index.jsp, use this to get variable from that java class and redirect:
<%#page import = "org.mypackage.Webredirect.*" %>
<%Webredirect obj = new Webredirect();%>
<%response.sendRedirect( obj.getName());%>
thank you.

Chinese characters not being rendered on JSP

I have a jsp say hello.jsp. Now there are 2 use cases
the request is redirected to hello.jsp through mainserverlet and in this case, it renders the "editable" text in chinese properly.
The second case is when i change the drop down menu on hello.jsp, it "resubmits" the request to itself, instead of mainservlet and in this case the text in chinese is not being displayed properly.
The charset=UTF-8 encoding has been set in HTML tag of the jsp.
I have tried to see how form is being submitted through javascript and the chinese text remains the same just before "submit". I don't know what happens that it is not being rendered after this.
Any pointers or suggestions?
Have you tried page tag?
Sample:
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
Obs: You need to put this on top of every page you use.
PS:
If you are thinking you can add above line in your header.jsp file and then include header file in your required file like this:
<jsp:include page="/resources/header.jsp" />
It won't work unless you add that line in every page.

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

Categories

Resources