Forums Register Login

encodeUrl not working properly

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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)
+Pie Number of slices to send: Send
Hey? Which bit's deprecated?
Also, if you are using cookies, the session id won't get appended to the URL anyway.
Adam
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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')
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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().
+Pie Number of slices to send: Send
This tiny ad is suggesting that maybe she should go play in traffic.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2736 times.
Similar Threads
Sequencial ID by Date
How to store option format value in database in jsp
value not passing to the while loop
page refresh problem
Storing The ResultSet
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 02:59:19.