Thursday, 19 July 2012

What is Thread Dump and how to create a Thread Dump

Thread Dump basically helps to track a activity of each thread.What are the job/task each thread is doing at a particular point of time we can get by thread dump.

To Create a thread dump in console Press Ctrl+Break from Key board.

Create a java program with infinite loop , at the time of running press Ctrl+Break key from key board and see the Full Thread dump is printed on console ( Now write that into a file).

Program :- xLoop.java

public class xLoop{     public static void main(String str[])
{     
       boolean x=true;             
       while(x)
{       
  System.out.println("Hello Manoj ");    
 }  } }

Now run this program on console and at the run time press Ctrl+Break , now the Full Thread Dump like below :-

Note- If any body find complex to find the Full Thread Dump message in the console , do some primary setup with your console window (command prompt). Change the CMD property-->Layout-->Height , set height to 2000 and CMD property-->options-->Buffer Size , set buffer size to 200.Now it will work and you can see this Dump messages.



HTTP GET Method

In the area of web application request & response are two major keys.The property of HTTP will remains universal over any programming language & platform, but here i mainly focus on Java/J2EE development.It means the response are related to each other.Normally the request is sent by client machine( browser ) and the response is revert back (sent back) to the client .Again for the response we need a clear idea about the content type ( MIME Type, Please read MIME TYPE in other post for more about content Type).

Key Points to remember about GET-


1. It has no Body, where as POST has Body.

2. It is Idempotent.

3. It is the default method in HttpServlet.In the Http servlet life cycle it is the default method.i.e. doGet().

4. It is not secure, because the data send by request line (URL) will visible on the rowser.

5. The amount of data for send with request using get method  is very limited.



Below a HTML form like :-

<form action="servlet/MyServletTest" name="frmMyServlet" method="get">
        <input type="text" name="txtValue" />
        <input type="submit" value="Send Value" onclick="FunSendValue()"/>
    </form>

Note-

If method name will not mentioned then bydefault it will take GET method as default.But be aware about your servlet , there must be a doGet() method for your operation/request you are sending. The doGet() must be there inside your resource servlet.If no doGet() method found then it will generate an error - HTTP method GET is not Supported.

The doGet() method inside the servlet like below :-

MyServletTest.java

public class MyServletTest extends HttpServlet {      
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String paramVal=request.getParameter("txtValue");
       
       
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        out.println("  <BODY>");
        out.print("    This is ");
        out.print(this.getClass());
        out.println(", using the GET method");
        out.println("<h3> Your Value from JSP="+paramVal+"</h3>");       
        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }

   

}

Like doGet() , there is doPost(),doPut(),doTrace(),doDelete() method for HTTP Servlet.The doGet() and doPost() methods are call by server ( via service method) for handling HTTP GET and POST requests.By extending HttpServlet ,here i have overrides the doGet() method for handle the request with GET method.
One more things to know is all Links,URL are the type GET in nature.

By default all links are handled by GET Method.Example :-


 <a href="http://java.sun.com/j2ee?myval=I am Testing">Click here for

GET method Request</a> is a link with request type GET.


OR

 <a href="servlet/MyServletTest?txtValue=I am a Programmer">Click

here for GET method Request</a>


Note-

The term Idempotent ! It means GET can safely repeated.No need to change the request link but keep in mind that the HTTP GET and servlet doGet() methods are quite different.Let me clear that HTTP GET is
idempotent as per HTTP 1.1 Specification but servlet doGet() method is non-idempotent. It means you repeate the link again and again without changes inside servlet doGet() method, then it may generate error like "Bad Request".And one thing about idempotent is that , it does not mean that the same request has always same response/output and we donot mean that request has no side-effect.


Read more from sun/oracle