aspose file tools*
The moose likes Servlets and the fly likes encodeUrl not working properly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "encodeUrl not working properly" Watch "encodeUrl not working properly" New topic
Author

encodeUrl not working properly

Malhar Barai
Author
Ranch Hand

Joined: Aug 17, 2001
Posts: 399
hi all
in the code below "encodeUrl" doesnt encode the URL with sessionid. what could be the reason...??

public void service(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
{
init();
PrintWriter out=response.getWriter();
String id = request.getParameter("id");

//setting session
HttpSession session=request.getSession(true);
HttpSessionContext context=session.getSessionContext();

out.println("<html>");

out.println("<body>");
try
{
rs1=st.executeQuery("SQL Query");
if(rs1.next())
{
//Getting values from database
}
else
{
out.println("<b>Nothing exist..</b>");
}

if (Values in Database)
{
String link = "/servlets/updateprofile?id="+id;
out.println("<td"><a href=\""+ response.encodeUrl(update) +"\">Update Profile");
}
}
catch(Exception e)
{}
tia
malhar


Malhar Barai
SOA & Java Book
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Two things.

1. your link is in a variable called 'link' but you encode something called 'update'.

2. you're using a deprecated method. use response.encodeURL(String url)
Adam Hardy
Ranch Hand

Joined: Oct 09, 2001
Posts: 566
Hey? Which bit's deprecated?
Also, if you are using cookies, the session id won't get appended to the URL anyway.
Adam

I have seen things you people would not believe, attack ships on fire off the shoulder of Orion, c-beams sparkling in the dark near the Tennhauser Gate. All these moments will be lost in time, like tears in the rain.
narasimha reddy
Greenhorn

Joined: Nov 24, 2001
Posts: 13
Hi..
Instead of using encodeURL use encodeRedirectURL of the httpservletresponse object. That will even support the encoding of url with or without the browser's cookies support.
Just read this....
public java.lang.String encodeRedirectURL(java.lang.String url)
Encodes the specified URL for use in the sendRedirect method or, if encoding is not needed, returns the URL unchanged. The implementation of this method includes the logic to determine whether the session ID needs to be encoded in the URL. Because the rules for making this determination can differ from those used to decide whether to encode a normal link, this method is seperate from the encodeURL method.
All URLs sent to the HttpServletResponse.sendRedirect method should be run through this method. Otherwise, URL rewriting cannot be used with browsers which do not support cookies.

------------------
Narasimha Reddy
---------------
Sun certified Enterprise architect(Part-I)
Sun Certified Web Component Developer
IBM Certified in OOAD with UML
IBM Certified in enterprise connectivity test
Oracle Certified in SQL and PL/SQL.
Sun Certified Java2 Programmer


Narasimha Reddy<BR>---------------<BR>Sun certified Enterprise architect(Part-I)<BR>Sun Certified Web Component Developer<BR>IBM Certified in OOAD with UML<BR>IBM Certified in enterprise connectivity test<BR>Oracle Certified in SQL and PL/SQL.<BR>Sun Certified Java2 Programmer
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Adam: check the API carefully. There are two methods, encodeURL and encodeUrl, the former being the one to use, and the later being deprecated.

narasimha: the original poster is not using the URL to redirect a response, only to output the link to the output stream.

Both methods (encodeRedirectURL and encodeURL) will support the encoding urls when the browser doesn't offer cookie support.

Adam's idea is probably accurate, that the browser probably has cookies enabled, so encodeURL doesn't appear to do anything.

malhar, if you use Tomcat, there is a post already here at javaranch that talks about how to force Tomcat to use URL re-writing, no matter what the browser uses (cookies or no). That way, you could test the encodeURL without having to affect your browser settings. Other servlet containers might have similar functionality. http://jakarta.apache.org/tomcat/tomcat-4.0-doc/config/context.html (search this page for 'cookies')
Malhar Barai
Author
Ranch Hand

Joined: Aug 17, 2001
Posts: 399
Originally posted by Mike Curwen:

malhar, if you use Tomcat, there is a post already here at javaranch that talks about how to force Tomcat to use URL re-writing, no matter what the browser uses (cookies or no). That way, you could test the encodeURL without having to affect your browser settings. Other servlet containers might have similar functionality. http://jakarta.apache.org/tomcat/tomcat-4.0-doc/config/context.html (search this page for 'cookies')

sorry dear
i m not using TOMCAT, but then how wud i resolve the prob, i.e to encode the URL with the session id..??
tia
malhar
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Whatever you *are* using, there is probably a similar setting that would force the container to perform URL re-writing.

iPlanet offers this functionality, for example.

So consult your documentation about how to turn on this behaviour.
Meng Tan
Ranch Hand

Joined: Jan 20, 2001
Posts: 115
Hi,
another reason may be that your getSession() method returns an existing session and that the the session id is from Cookie.
Check with call to req.isRequestedSessionIdFromCookie().
mak pandian
Ranch Hand

Joined: Jul 06, 2011
Posts: 30

Please refer my reply at http://www.coderanch.com/t/291676/JSP/java/response-encodeURL-working#2800568
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: encodeUrl not working properly