Search This Blog

Showing posts with label JEE. Show all posts
Showing posts with label JEE. Show all posts

Saturday, May 17, 2014

Simulate Java OutOfMemoryError

Simulate Java OutOfMemoryError
Here am simulating java OutOfMemoryError, lets discuss brief details about OutOfMemoryError.

Why OutOfMemoryError error will arise?
          OutOfMemoryError will arise when allotted Heap size of the Java virtual will be exceeds during runtime.

Where object will be saved in Java?
          Basically all java class objects will be saved during runtime in HeapMemory.

Sample Java Program to simulate OutOfMemoryError

In below program name is object of String, so name value will be saved to Heap space of JVM.
          Calling method1 infinite time will cause java.lang.OutOfMemoryError: Java heap space error, because below line in method1 is causing this error. If you comments below line you don’t get this error.
                         name+=";"+name;


package com.blogspot.microtechinfo;

public class JavaOutOfMemory {

            private static String  name="Marimuthu Rajagopal";
          private static int count=0;
         
          /**
           * @param args
           */
          public static void main(String[] args) {
                   // TODO Auto-generated method stub
                        while(true)
                   {
                             method1();
                   }
          }
          private static void method1()
          {
                  
                         name+=";"+name;
                   System.out.println("count="+ ++count);
                  
          }
         
}

 Output:
  
when count is 20 i will be getting  java.lang.OutOfMemoryError: Java heap space,It will vary based on machine.

count=1
count=2
count=3
count=4
count=5
count=6
count=7
count=8
count=9
count=10
count=11
count=12
count=13
count=14
count=15
count=16
count=17
count=18
count=19
count=20
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
          at java.util.Arrays.copyOfRange(Unknown Source)
          at java.lang.String.(Unknown Source)
          at java.lang.StringBuilder.toString(Unknown Source)
          at com.blogspot.microtechinfo.JavaOutOfMemory.method1(JavaOutOfMemory.java:21)
          at com.blogspot.microtechinfo.JavaOutOfMemory.main(JavaOutOfMemory.java:15)





Friday, March 18, 2011

Servlet 2.4 Specification Notes

Note while reading servlet 2.4 specification.

SRV.1.1 What is a Servlet?
A servlet is a JavaTM technology-based Web component, managed by a container,
that generates dynamic content. Like other Java technology-based components,
servlets are platform-independent Java classes that are compiled to platform-neutral
byte code that can be loaded dynamically into and run by a Java technology-enabled
Web server. Containers, sometimes called servlet engines, are Web server extensions
that provide servlet functionality. Servlets interact with Web clients via a
request/response paradigm implemented by the servlet container.

SRV.1.2 What is a Servlet Container?
The servlet container is a part of aWeb server or application server that provides the
network services over which requests and responses are sent, decodes MIME-based
requests, and formats MIME-based responses. A servlet container also contains and
manages servlets through their lifecycle.

All servlet containers must support HTTP as a protocol for requests and
responses, but additional request/response-based protocols such as HTTPS
(HTTP over SSL) may be supported.

Sequence of Servlet Request & Response
The following is a typical sequence of events:
1. A client (e.g., a Web browser) accesses a Web server and makes an HTTP request.
2. The request is received by the Web server and handed off to the servlet container.
The servlet container can be running in the same process as the host
Web server, in a different process on the same host, or on a different host from
the Web server for which it processes requests.
3. The servlet container determines which servlet to invoke based on the configuration
of its servlets, and calls it with objects representing the request and response.
4. The servlet uses the request object to find out who the remote user is, what
HTTP POST parameters may have been sent as part of this request, and other
relevant data. The servlet performs whatever logic it was programmed with,
and generates data to send back to the client. It sends this data back to the client
via the response object.
5. Once the servlet has finished processing the request, the servlet container ensures
that the response is properly flushed, and returns control back to the host
Web server.

Comparing Servlets with Other Technologies
In functionality, servlets lie somewhere between Common Gateway Interface (CGI)
programs and proprietary server extensions such as the Netscape Server API
(NSAPI) or Apache Modules.
Servlets have the following advantages over other server extension mechanisms:
• They are generally much faster than CGI scripts because a different process
model is used.
• They use a standard API that is supported by many Web servers.
• They have all the advantages of the Java programming language, including
ease of development and platform independence.
• They can access the large set of APIs available for the Java platform.

Servlet Interface

The Servlet interface is the central abstraction of the Java Servlet API. All servlets
implement this interface either directly, or more commonly, by extending a class
that implements the interface. The two classes in the Java Servlet API that implement
the Servlet interface are GenericServlet and HttpServlet. For most purposes,
Developers will extend HttpServlet to implement their servlets.

SRV.2.1 Request Handling Methods
The basic Servlet interface defines a service method for handling client requests.
This method is called for each request that the servlet container routes to an instance
of a servlet.
The handling of concurrent requests to a Web application generally requires
that the Web Developer design servlets that can deal with multiple threads executing
within the service method at a particular time.
Generally the Web container handles concurrent requests to the same servlet
by concurrent execution of the service method on different threads.

SRV.2.1.1 HTTP Specific Request Handling Methods
The HttpServlet abstract subclass adds additional methods beyond the basic
Servlet interface that are automatically called by the service method in the
HttpServlet class to aid in processing HTTP-based requests. These methods are:
• doGet for handling HTTP GET requests
• doPost for handling HTTP POST requests
• doPut for handling HTTP PUT requests
• doDelete for handling HTTP DELETE requests
• doHead for handling HTTP HEAD requests
• doOptions for handling HTTP OPTIONS requests
• doTrace for handling HTTP TRACE requests
Typically when developing HTTP-based servlets, a Servlet Developer will
only concern himself with the doGet and doPost methods. The other methods are
considered to be methods for use by programmers very familiar with HTTP programming.

SRV.2.1.2 Additional Methods
The doPut and doDelete methods allow Servlet Developers to support HTTP/1.1
clients that employ these features. The doHead method in HttpServlet is a specialized
form of the doGet method that returns only the headers produced by the doGet
method. The doOptions method responds with which HTTP methods are supported
by the servlet. The doTrace method generates a response containing all instances of
the headers sent in the TRACE request.

SRV.2.1.3 Conditional GET Support
The HttpServlet interface defines the getLastModified method to support conditional
GET operations. A conditional GET operation requests a resource be sent only if
it has been modified since a specified time. In appropriate situations, implementation
of this method may aid efficient utilization of network resources.

Thursday, November 19, 2009

Servlet Question and Answer

When I will prepare for technical interview, I have used to think about hints and opt technical terms.

So I have started preparing question while reading books. I will ask question my self and redo the answer until I get opt technical words.

This post all are my own preparation if any one find its use full I really deserve for that.

Visitors of my blogspot kindly add your valuable comments to improve my post.

Note:

If u find any answer is wrong let me know. So I can redefine the same.

  1. What is Servlet?
    • Servlet is a java program, It will run inside the web container
    • Servlet extends request/respond –oriented servers such as java oriented web server.
  2. Different way to session tracking?
    • Cookies
    • URL re-writings
    • Http session tracking
    • Hidden form
    • Session
  3. What mechanism is used by servlet container to maintain the session?
    • Session
    • Cookies
    • URL re-writing
  4. Difference between GET and POST?

No.

Get

Post

1

Data are submitted part of URL

Data are submitted inside body of servlet request.

2

Query max length is 255 character

No Limitation

3

Its not secure

Its secure.

  1. Session
    • Session is a object is used to track the information about the user with web application across multiple HTTP Request (State less).
    • Session is stored on web server.
  2. Servlet Mapping
    • Mapping defines the association between the URL pattern and Servlet
    • Mapping will be define in web.xml
    • Mapping is used to map the particular request to Servlet.
  3. Servlet Context:
    • It’s a servlet Object that contains information about web application and container.
    • Each application has only one servlet Context Object.
    • Uses for set the attribute that other servlet in the same context can use.
  4. Can we use constructer instead of Init():
    • Yes we can use constructer in Servlet
    • But we wont get servlet specific feature
    • So we can’t use servletcontext and servletconfig object in constructer.
  5. Life cycle method of the servlet:
    • Javax.servlet.Servlet interface providing 3 life cycle methods are
    • Public void init (ServletConfig config)- will be called when instance is created.
    • Public void service(ServletRequest req,ServletResponse res)-Every request is handled by service method only.
    • Public void distory()-when object is de-allocated from JVM.
  6. Pre Initialization of a servlet:
    • Process of loading servlet before any request comes in is called pre loading/pre initialization.
    • We have to include the element priority for every servlet in web.xml
  7. Difference between HttpServlet and Generic Servlet:

No.

Http Servlet

Generic Servlet

1

Parrent Class for Http Servlet is Generic Servlet

Parent class of HttpServlet is Servlet

2

Http Servlet support doget(),dopost(),doHead methods

Service method will handle all the request.

  1. Difference between ServletContext and ServletConfig:

No.

Servlet Context

Servlet Config

1

Set of methods support to communicate with servlet container

Its specific to the single servlet.

2

Uses for set the attribute that other servlet in the same context can use.

Its used to initialize the servlet specific value.

3

It’s created when servlet container created.

It’s created when specific class is loaded by container.

  1. Advantages and selling point of Servlet?

    • Servlet handle the multiple requests and synchronize the request.
    • This allows servlet to support online real time conferencing.
    • Servlet forward the request to the other servlet.
    • So servlet can be used to balance the load among several servers that mirror the same content.
  1. what are the Information servletRequest object contain:
    • parameter passed by the client
    • name of the remote host
    • the protocol
  2. Type of constraints send on the client : by servlet Response interface

    • Content length
    • Content type
    • Servlet Writer, Servlet output stream and a writer through which the servlet can send the data.

  1. Context Initialization Parameter:
    • It’s specified in web.xml under the element.
    • This is a parameter for the whole application not specific for Servlet/JSP.
    • It will be only accessed by servlet context object.

  1. Servlet Chaining:

    • Two are more servlet co-operate in servicing a single request.

  1. Difference between Servlet Context and Page Context?
    • ServletContext –Gives information about Servlet container
    • PageContext-Gives information about request
  2. Difference:
    • Request.getRequestDispacher()-Need to give relative path.
    • Context.getRequestDispacher()-Need to give absolute path.
  3. Difference:
    • RequestDispacher()-Direct with request and Response object.
    • sendRequest()-direct with new request and Response object.

Sunday, September 20, 2009

History of web application Development:

Initial Development:

  • Before 90's no one used the Internet
  • only the American army is used the Internet for sharing information between two terriotory.
  • In 1995 commercialisation of the Internet began.
  • Initially the web pages are static only.
  • Static page are not specific to particular customer.
  • In later the Dynamic web pages are changed the way to modern web world.
  • Dynamic content are specifically targeted for a particular customer.
  • Initially all the dynamic web pages are developed using CGI(Common Cateway Interface) technology.

Intermediate Development:

  • In 1995 Java enabled Applet changed the face of web pages more elegant.
  • JVM enabled Browser can display the applet in browser.
  • Using applet we can get the user input and based on the user input display the data.
  • In 1997 sun micro system released Java servlet technology ,that enabled very quick response to client as well as new dimension to the web technology.
  • Servlet has more efficient execution model than CGI because they are multi thread instead of requesting new process for each request.
  • Later JSP was introduced by sun microsystem for easy of web development.

Saturday, September 5, 2009

How to Include Inteface in JSP?

When i was started working as a TL.I have to give the technical assistance to my subordinate.
So one day he asked my how to include interface in JSP and access the interface static value.
I gave the answer as,


1. It’s possible to include Interface in jsp use bean class tag.
• No because java cants instances the object for the interface so jsp use bean will throws the error.
• Eg:
2. It’s possible to include with out constructer java Class file in jsp use bean class tag.
• No, because its can’t create object for the with out constructer class java.
• Its throw error.
3. How to access Interface static variable in Jsp file?
• Its possible, there is two possible for that
• Way1: we have to import the javaclass which has implemented the java interface
• Create the object for that interface implemented class, using the reference variable we can access the interface final variable.
• Acc_Cheque_Detail acd=new Acc_Cheque_Detail();acd.RECEIVED
• Way2: we have to create interface reference variable assign the implemented the class objects.
• Using that interface reference variable we can access the interface final variable.

Sunday, July 12, 2009

Why Servlet Filter??

Here i am interesting to describe about Servlet filter.
First, We have to understand why we are going for servlet filter.
Reason is very simple.
If you want to redirect or Bi -pass each and every request of the web application through one way direction In J2EE based application.
I think you guys are under stand things.

The Java Servlet specification version 2.3 introduces a new component type, called a filter. A filter dynamically intercepts requests and responses to transform or use the information contained in the requests or responses. Filters typically do not themselves create responses, but instead provide universal functions that can be "attached" to any type of servlet or JSP page.

I hope soon come with more about Filter.