Tuesday, October 14, 2008

How to use getParameter on jsp page in liferayportlet

How to use getParameter on jsp page in liferayportlet

First you need to import this liferayportlet package in your JSP page:

<%@ page import="com.liferay.portal.util.PortalUtil" %>

HttpServletRequest myReq = PortalUtil.
getOriginalServletRequest(PortalUtil.getHttpServletRequest(renderRequest));
String myid = myReq.getParameter("myid")
;
out.println("myid="+myid);

Thursday, September 18, 2008

Create new HttpServletRequest object in Servlet

Create new HttpServletRequest object in Servlet

Sometimes you not need to use default object of HttpServletRequest in Servlet, you need to create new object of HttpServletRequest, in this situation you can use listed below code to create new object of HttpServletRequest. in this code just use default object " request" of HttpServletRequest in "doPost" or "doGet" method.



public void doPost(HttpServletRequest request, HttpServletResponse res)
        throws IOException, ServletException {

HttpServletRequest hsr = (HttpServletRequest) request;
// now use hsr

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public void doGet(HttpServletRequest request, HttpServletResponse res)
        throws IOException, ServletException {

HttpServletRequest hsr = (HttpServletRequest) request;
// now use hsr

}

Wednesday, September 10, 2008

Download image without cURL and with cURL

Download image without cURL and with cURL

<?php
// without cURL
$downloadImageUrl="http://fc04.deviantart.net/fs26/f/2008/072/b/1/Frozen_Galaxy_by_Vpr87.jpg";
////// Using without CURL
$img = file_get_contents($downloadImageUrl);
$file1 = dirname(__file__).'/aaa.jpg';
file_put_contents($file1, $img);



////// // with cURL
$fileName=$downloadImageUrl;
$header = array(
            "Accept-Encoding: gzip,deflate",
            "Accept-Charset: utf-8;q=0.7,*;q=0.7",
            "Connection: close"
        );
$useragent = 'Amit Kumar Gaur a@gmail.com  http://amitkgaur.blogspot.com';
$file2 = dirname(__file__).'/bbb.jpg';
$curlObj=curl_init();
curl_setopt($curlObj, CURLOPT_HTTPHEADER, $header);
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlObj, CURLOPT_HEADER, false);
curl_setopt($curlObj, CURLOPT_USERAGENT, $useragent);
curl_setopt($curlObj, CURLOPT_CONNECTTIMEOUT, 999);
curl_setopt($curlObj, CURLOPT_TIMEOUT, 9999);
curl_setopt($curlObj, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($curlObj, CURLOPT_URL, $downloadImageUrl);
$response = curl_exec($curlObj);
$return = false;
if(!curl_errno($curlObj)) {
    file_put_contents($file2, $response);
}










Wednesday, April 23, 2008

Show confirm message during browser window close.


Here is the code that will show a confirmation message if user want to close browser window/close.




<form name="form1" id="form1">
     <input type="submit" name="btn1" value="Submit1" onclick="return chk();">
 </form>
 <form name="form2" id="form2">
     <input type="submit" name="btn1" value="Submit2">
</form>
<script language="javascript">
    var fillout = false;
    window.onbeforeunload=function verifyclose() {
        var undefined;
        var msg;
        if (document.forms[0] == undefined)
            return;
        if (!fillout) {
            msg = " Do you want quit/close this window?";
        }
        return msg;
    };   
   
    function chk() {
        return false;   
    }
 </script>