Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

encodeUrl not working properly

 
Malhar Barai
Author
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 567
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey? Which bit's deprecated?
Also, if you are using cookies, the session id won't get appended to the URL anyway.
Adam
 
narasimha reddy
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
Eclipse IDE Java Tomcat Server
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic