Gabi Jack's Blog
  • About

Tag Archives: java

Reconstructing Original Request URL

Posted on June 5, 2012 by Posted in Java, JSP and Servlets

My JSP and Servlets professor handed me down this code as a nice way to reconstruct an original request URL and avoid the temptation to ever hard-code a URL, which is also really bad practice.  It’s really useful for getting out of tricky situations. In my case, I needed a way to go back to the http:// protocol after having accessed resources using SSL.  I found that if I simply used RequestDispatcher, even when the resource I was requesting wasn’t included among those restricted ones, the protocol continued to be https:// .  I had searched everywhere for a way to fix this and this is what he suggested.

 

Example:
// http://hostname.com:80/mywebapp/servlet/MyServlet/a/b;c=123?d=789
public static String getUrl3(HttpServletRequest req) {
    String scheme = req.getScheme();             // http
    String serverName = req.getServerName();     // hostname.com
    int serverPort = req.getServerPort();        // 80
    String contextPath = req.getContextPath();   // /mywebapp
    String servletPath = req.getServletPath();   // /servlet/MyServlet
    String pathInfo = req.getPathInfo();         // /a/b;c=123
    String queryString = req.getQueryString();          // d=789

    // Reconstruct original requesting URL
    String url = scheme+"://"+serverName+":"+serverPort+contextPath+servletPath;
    if (pathInfo != null) {
        url += pathInfo;
    }
    if (queryString != null) {
        url += "?"+queryString;
    }
    return url;
}

 

java jsp request servlet url

Monday Morning Rambling

Posted on June 4, 2012 by Posted in personal

Now that the semester is over and Summer is practically here, I have some time to slow down a bit and go back to review any extra material or information that wasn’t covered properly due to lack of time. I had thought about enrolling in a class or two during the Summer term, but the ones I wanted are not  offered, so I guess it’s time to hit the books on my own, while I continue to look for other opportunities.

Something I realized while working on the last school project is that I really need to polish my skills in HTML and CSS, and become fluent in Javascript. I know, I’ve heard that some Java programmers look down on Javascript for some strange reason, but it seems to me that it’s an incredibly useful tool for web development. I only used a couple of scripts for my last project, one for validating a form and another one for creating a block of text that seemed to slide with you as you scrolled up and down the page, remaining always in view. I’ve seen some other  really cool things done with Javascript  and I’m eager to learn.

For now, I’m reading Murach’s  HTML, XHTML, and CSS. I actually won the book a few months ago, during a little contest the publishers had on Twitter. Can you believe that!  It’s basic, yes, but full of solid and useful information. I’ve been using parts of it as needed while working on some of my projects, but I guess it doesn’t hurt to read the whole book.  Actually, I must confess I love Murach’s books. They are amazingly clear, full of examples and read fast too.  It was actually thanks to Murach’s  Servlets and JSP book that I was able to successfully code the classes I needed to establish the connection pool with a MySQL database for one of my projects, since the official  textbook for my class, Servlet and JSP (A Tutorial) by Budi Kurniawan, doesn’t offer enough detail about it.  Of course, I also helped myself with information from  Howard Hyde’s    Java Web Database Application Development,  Volume 1 . That’s another book that wouldn’t hurt reading from cover to cover.

I’ll be sharing more about the code, and the code itself, in future writings.

java murach MySQL
December 2019
S M T W T F S
« Dec    
1234567
891011121314
15161718192021
22232425262728
293031  

Recent Posts

  • TIL: Working with a D3-based chart library
  • Diary of a Junior Dev: The Joys of Building
  • In search of a faster query
  • Living la vida Ruby
  • I love peer reviews… I hate peer reviews

Recent Comments

    Categories

    CyberChimps

    CyberChimps

    Marketed By Neil Patel
    © Gabi Jack's Blog