Skip to main content

How to capture and record click event on a link to an external URL?



I have the following HTML. I want the URL 'http://www.google.com' to be inserted in the database table on click of the link.







<a href="http://www.google.com" onclick="insert.jsp">Google</a>







The insert.jsp contains the connectivity code which works fine on the click of the button, but above code is not working.


Comments

  1. Use rel="ext" on all those links, it gives additional SEO benefit

    <a href="http://www.google.com" rel="ext">
    <a href="http://www.stackoverflow.com" rel="ext">
    ...


    And use this piece of JavaScript to record them (I'm for simplicitly taking a little help of jQuery to make it reliably cross browser compatible without the need to write 100 more lines)

    $('a[rel=ext]').click(function(e) {
    var $link = $(this);
    var url = '/log?url=' + encodeURIComponent($link.attr('href'));

    if (e.ctrlKey || $link.attr('target') == '_blank') {
    window.open(url);
    } else {
    window.location = url;
    }

    e.preventDefault();
    });


    Finally have a servlet on an URL pattern of /log which does the job

    @WebServlet("/log")
    public class LogServlet extends HttpServlet {

    @EJB
    private LogService logService;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String url = request.getParameter("url");

    if (url == null || url.trim().isEmpty()) {
    response.sendError(HttpServletResponse.SC_BAD_REQUEST);
    } else {
    logService.log(url);
    response.sendRedirect(url);
    }
    }

    }


    Don't use JSP for this. It makes no sense.

    ReplyDelete
  2. The HttpServletRequest interface has a method getRequestURL ( ) that return the url of the current page. use this to assign url of current page in some string variable say reqUrl #Example# String reqUrl = request.getRequestURL() #End# and execute insert command as follows
    stmt=con.createStatement(); //create a Statement object
    rst=stmt.executeUpdate("insert into table_name VALUES ('" + reqUrl + "'");

    ReplyDelete
  3. If the URL is of the same domain and of the same application then you could easily do this by Mapping a Filter that will intercept each request and will add it to DB asynchronously

    But if it is of other domain then you need to rewrite such links so that it would pass from one of your Servlet of your app for example /GateWatServlet?url=google.com and you can do the job there in Servlet

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.