Appnding session id to the URL

Splash Forums PrettyFaces Users Appnding session id to the URL

This topic contains 12 replies, has 3 voices, and was last updated by  Lincoln Baxter III 4 years, 5 months ago.

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #19169

    Carolina
    Member

    Hi Pretty,

    I have used URL rewriting for my all application page URL.

    In this case I have redirected URL by actionListner button click.

    In below URL the sessionId is appending for each date parameter (data type of date is String)

    In browser URL looks like below:

    http://e125:8080/b2c/vacation-packages/search?trip=R&origin=RUH&destination=JED&depart_date=25-12-2012%3Bjsessionid%3D35A9CFB50848FBD0498B0A39FC0CD6AE.node1&arrival_date=29-12-2012%3Bjsessionid%3D35A9CFB50848FBD0498B0A39FC0CD6AE.node1&room1=1A-0C-0I&traveltype=D

    Even though i have remove the session id while building URL is still showing jsessionid in browser.

    May I know how to get rid of the session id in URL?

    code:

    void ButtonClick(ActionEvent event)
    {
    String sessionId = FacesUtils.getCurrentSession().getId();

    String url = urlbuilder.toString();
    url.replaceAll("jsessionid="+sessionId, "");
    FacesContext.getCurrentInstance().getExternalContext().redirect(FacesContext.getCurrentInstance().getExternalContext().encodeActionURL(FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() + "/pages/package/flighthotel/flightPlusHotelResults.xhtml?"+url));
    }

    pretty-config.xml:

    <url-mapping id="flightPlusHotel">
    <pattern value="/vacation-packages/search" />
    <query-param name="trip">#{urlHandlerBean.trip}</query-param>
    <query-param name="origin">#{urlHandlerBean.origin}</query-param>
    <query-param name="destination">#{urlHandlerBean.destination}</query-param>
    <query-param name="depart_date">#{urlHandlerBean.depart_date}</query-param>
    <query-param name="arrival_date">#{urlHandlerBean.arrival_date}</query-param>
    <query-param name="direct_flight">#{urlHandlerBean.direct_flight}</query-param>
    <query-param name="classtype">#{urlHandlerBean.classtype}</query-param>
    <query-param name="flexi">#{urlHandlerBean.flexi}</query-param>
    <query-param name="room1">#{urlHandlerBean.room1}</query-param>
    <query-param name="room2">#{urlHandlerBean.room2}</query-param>
    <query-param name="room3">#{urlHandlerBean.room3}</query-param>
    <query-param name="room4">#{urlHandlerBean.room4}</query-param>
    <query-param name="room5">#{urlHandlerBean.room5}</query-param>
    <query-param name="room6">#{urlHandlerBean.room6}</query-param>
    <query-param name="traveltype">#{urlHandlerBean.traveltype}</query-param>
    <view-id value="/pages/package/flighthotel/flightPlusHotelResults.xhtml" />
    </url-mapping>

    #23314

    I don’t understand why the session ID is appended only to the two date query parameters. Do you call encodeURL() or encodeRedirectURL() somewhere? Could you post the code that creates the date query parameters?

    #23315

    Please note that methods like HttpServletReponse.encodeURL() do NOT perform URL encoding. They are responsible for adding the session ID to the URL if cookies are not supported by the client browser.

    From the docs:

    Encodes the specified URL by including the session ID in it, or, if encoding is not needed, returns the URL unchanged.

    http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponse.html#encodeURL(java.lang.String)

    #23316

    Carolina
    Member

    Hi Christian,

    I already posted m code in 1st post itself. I have used FacesContext.getCurrentInstance().getExternalContext().encodeActionURL

    You can check in my ButtonClick method in my 1st post.

    Please let me know which encode i have to use?

    #23317

    Carolina
    Member

    Hi Christian,

    Here is the complete code to create URL parameters.

    StringBuilder urlbuilder = new StringBuilder();

    urlbuilder.append("trip=");
    urlbuilder.append(getUrlHandlerBean().getTrip());

    urlbuilder.append("&origin=");
    urlbuilder.append(getUrlHandlerBean().getOrigin());

    urlbuilder.append("&destination=");
    urlbuilder.append(getUrlHandlerBean().getDestination());

    urlbuilder.append("&depart_date=");
    urlbuilder.append(getUrlHandlerBean().getDepart_date());

    if(null!=getUrlHandlerBean().getArrival_date()){

    urlbuilder.append("&arrival_date=");
    urlbuilder.append(getUrlHandlerBean().getArrival_date());
    }
    if(null!=getUrlHandlerBean().getDirect_flight()){

    urlbuilder.append("&direct_flight=");
    urlbuilder.append(getUrlHandlerBean().getDirect_flight());
    }
    if(null!=getUrlHandlerBean().getClasstype()){
    urlbuilder.append("&classtype=");
    urlbuilder.append(getUrlHandlerBean().getClasstype());
    }
    if(null!=getUrlHandlerBean().getRoom1()){

    urlbuilder.append("&room1=");
    urlbuilder.append(getUrlHandlerBean().getRoom1());
    }
    if(null!=getUrlHandlerBean().getRoom2()){

    urlbuilder.append("&room2=");
    urlbuilder.append(getUrlHandlerBean().getRoom2());
    }
    if(null!=getUrlHandlerBean().getRoom3()){

    urlbuilder.append("&room3=");
    urlbuilder.append(getUrlHandlerBean().getRoom3());
    }
    if(null!=getUrlHandlerBean().getRoom4()){
    urlbuilder.append("&room4=");
    urlbuilder.append(getUrlHandlerBean().getRoom4());
    }
    if(null!=getUrlHandlerBean().getRoom5()){
    urlbuilder.append("&room5=");
    urlbuilder.append(getUrlHandlerBean().getRoom5());
    }
    if(null!=getUrlHandlerBean().getRoom6()){

    urlbuilder.append("&room6=");
    urlbuilder.append(getUrlHandlerBean().getRoom6());
    }
    if(null!=getUrlHandlerBean().getTraveltype()){

    urlbuilder.append("&traveltype=");
    urlbuilder.append(getUrlHandlerBean().getTraveltype());
    }
    String sessionId = FacesUtils.getCurrentSession().getId();

    String url = urlbuilder.toString();
    url.replaceAll(sessionId, "");

    FacesUtils.getFacesContext().getExternalContext().redirect(FacesUtils.getFacesContext().
    getExternalContext().encodeActionURL(FacesUtils.getFacesContext().getExternalContext().getRequestContextPath()
    + "/pages/package/flighthotel/flightPlusHotelResults.xhtml?"+url));

    #23318

    If I am not mistaken, externalContext().encodeActionURL() is responsible for adding the sessionID because it calls response.encodeURL under the covers.

    I suggest debugging through to see exactly where the sessionID is added. Then let us know and we’ll suggest how best to remove it.

    Sound good?

    ~Lincoln

    #23319

    I agree with Lincoln. I think you are using one of the encode*() methods somewhere. You shouldn’t do this. If you want to do URL encoding, use URLEncoder instead.

    #23320

    Carolina
    Member

    Hi Lincoln,

    I debugged and Jssessionid is adding after each date in below code. And also i used URLEncoder as suggested by Christian, still adding JssseionId.

    urlbuilder.append("&depart_date=");
    urlbuilder.append(getUrlHandlerBean().getDepart_date());

    if(null!=getUrlHandlerBean().getArrival_date()){

    urlbuilder.append("&arrival_date=");
    urlbuilder.append(getUrlHandlerBean().getArrival_date());
    }

    #23321

    I’m specifically looking for the place that actually does the appending of JSessionID. This looks like your code. What part of the framework/servlet container is doing the appending? And where is that called from your code?

    We need to know exactly where this is happening. If you could dump a debug stack trace of the actual line that appends the jsessionid, that would be helpful!

    #23322

    What’s getUrlHandlerBean().getArrival_date() doing? Could you post that code too?

    #23323

    Carolina
    Member

    Hi Lincoln/Christian,

    I got it where the JsessionId was setting.

    I debugged and the JsessionId was appending on the setting the Depart_date variable value in UrlHandlerBean.

    getUrlHandlerBean().setDepart_date(FacesUtils.getFacesContext().getExternalContext().encodeActionURL((dateFormat.format(departure.getDepartDate()))));

    Because of using encodeActionURL jsesionid was appending, now i am using URLEncoder.encode which solve my issue.

    Thanks for your questions :) Your questions makes me alert to debug & get the solution.

    JsessionId solved but every = and & is not encoding and getting %3D and %26 respectively in browser.

    Getting URL in browser:

    http://e125:8080/b2c/vacation-packages/%2Fb2c%2Fpages%2Fpackage%2Fflighthotel%2FflightPlusHotelResults.xhtml%3Ftrip%3DR%26origin%3DDUB%26destination%3DJED%26depart_date%3D26-12-2012%26arrival_date%3D30-12-2012%26room1%3D1A-0C-0I%26traveltype%3DI

    Expecting URL:

    http://e125:8080/b2c/vacation-packages/search?trip=R&origin=DUB&destination=JED&depart_date=26-12-2012&arrival_date=30-12-2012&room1=1A-0C-0I&traveltype=I

    #23324

    Great you got it working! :)

    #23325

    So there is an issue with URL-encoding entire URLs, which is that the path and the query need different forms of encoding. To solve this, you can use the AddressBuilder module from Rewrite. (Don’t mind the Alpha, this part is simple and stable.) This is a perfect use-case for AddressBuilder, you should be able to replace all of the StringBuilder code in your URLHandlerBean.

    <dependency>
    <groupId>org.ocpsoft.rewrite</groupId>
    <artifactId>rewrite-addressbuilder</artifactId>
    <version>2.0.0.Alpha2</version>
    </dependency>

    https://github.com/ocpsoft/rewrite/blob/master/addressbuilder/src/test/java/org/ocpsoft/urlbuilder/AddressBuilderEncodingTest.java#L96

    You’ll want to do something like this:

    AddressBuilderQuery builder = AddressBuilder.begin().protocol("http").host("localhost")
    .path("/{p}").setEncoded("p", "a%20b")
    .query("trip", getUrlHandlerBean().getTrip())
    .query("origin", getUrlHandlerBean().getOrigin())
    .query("destination", getUrlHandlerBean().getDestination())
    .query("depart_date", getUrlHandlerBean().getDepart_date());

    if(null!=getUrlHandlerBean().getArrival_date()) {
    builder.query("arrival_date", getUrlHandlerBean().getArrival_date());
    }
    if(null!=getUrlHandlerBean().getDirect_flight()) {
    builder.query"direct_flight", getUrlHandlerBean().getDirect_flight());
    }
    if(null!=getUrlHandlerBean().getClasstype()) {
    builder.query("classtype", getUrlHandlerBean().getClasstype());
    }

    And so on….

    But don’t use URLEncoder when you set your values. If you use AddressBuilder the way I’ve shown above, your values will automatically be encoded when the Address is built. No worries to you :)

    Let me know how it goes. You’ll still probably want to remove the sessionID if it shows up.

    ~Lincoln

Viewing 13 posts - 1 through 13 (of 13 total)

You must be logged in to reply to this topic.

Comments are closed.