Stay with Java

Stay with Java Join this page to get in touch with all Java/J2EE technologies. For interview questions.

More concepts here on Core java, JDBC, JMS, Servlets, JSP, Struts, Spring, Hibernate and many......

What is try-with-resources statement in Java? In Java, the try-with-resources statement is a try statement that declares...
25/07/2020

What is try-with-resources statement in Java?

In Java, the try-with-resources statement is a try statement that declares one or more resources. The resource is as an object that must be closed after finishing the program. The try-with-resources statement ensures that each resource is closed at the end of the statement ex*****on.

You can pass any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable.

The following example writes a string into a file. It uses an instance of FileOutputStream to write data into the file. FileOutputStream is a resource that must be closed after the program is finished with it. So, in this example, closing of resource is done by itself try.

31/07/2015

What are the types of Session Tracking ?

Following are the popular ways of session tracking:

a.) URL rewriting: In this method of session tracking, some extra data is appended at the end of the URL, which identifies the session. This method is used for those browsers which do not support cookies or when the cookies are disabled by the user.

b.) Hidden Form Fields: This method is similar to URL rewriting. New hidden fields are embedded by the server in every dynamically generated form page for the client. When the form is submitted to the server the hidden fields identify the client.

c.) Cookies: Cookie refers to the small amount of information sent by a servlet to a Web browser. Browser saves this information and sends it back to the server when requested next. Its value helps in uniquely identifying a client.

d.) Secure Socket Layer (SSL) Sessions

30/07/2015

Which HTTP method is non-idempotent?

A HTTP method is said to be idempotent if it returns the same result every time. HTTP methods GET, PUT, DELETE, HEAD, and OPTIONS are idempotent method and we should implement our application to make sure these methods always return same result. HTTP method POST is non-idempotent method and we should use post method when implementing something that changes with every request.

For example, to access an HTML page or image, we should use GET because it will always return the same object but if we have to save customer information to database, we should use POST method. Idempotent methods are also known as safe methods and we don’t care about the repetitive request from the client for safe methods.

29/07/2015

What is MIME Type?

The “Content-Type” response header is known as MIME Type. Server sends MIME type to client to let them know the kind of data it’s sending. It helps client in rendering the data for user. Some of the mostly used mime types are text/html, text/xml, application/xml etc.

We can use ServletContext getMimeType() method to get the correct MIME type of the file and use it to set the response content type. It’s very useful in downloading file through servlet from server.

28/07/2015

What is different between web server and application server?

A web server responsibility is to handler HTTP requests from client browsers and respond with HTML response. A web server understands HTTP language and runs on HTTP protocol.
Apache Web Server is kind of a web server and then we have specific containers that can execute servlets and JSPs known as servlet container, for example Tomcat.

Application Servers provide additional features such as Enterprise JavaBeans support, JMS Messaging support, Transaction Management etc. So we can say that Application server is a web server with additional functionalities to help developers with enterprise applications.

17/07/2015

What will happen if we put a key object in a HashMap which is already there ?

If you put the same key again then it will replace the old mapping because HashMap doesn't allow duplicate keys. Same key will result in same hashcode and will end up at same position in bucket. Each bucket contains a linked list of Map.Entry object, which contains both Key and Value. Now Java will take Key object from each entry and compare with this new key using equals() method, if that return true then value object in that entry will be replaced by new value.

16/07/2015

Can you override private or static method in Java ?

You can not override private or static method in Java.

if you create similar method with same return type and same method arguments in child class then it will hide the super class method, this is known as method hiding.

Similarly you cannot override private method in sub class because it's not accessible there, what you do is create another private method with same name in child class.

15/07/2015

What will happen if you put return statement or System.exit () on try or catch block ? Will finally block execute?

finally block will execute even if you put return statement in try block or catch block but finally block won't run if you call System.exit() from try or catch.

06/07/2015

What is an Iterator and How do you traverse through a collection using its Iterator?

1. The Iterator interface is used to step through the elements of a Collection.

2. Iterators let you process each element of a Collection.

3. Iterators are a generic way to go through all the elements of a Collection no matter how it is organized.

4. Iterator is an Interface implemented a different way for every Collection.

To use an iterator to traverse through the contents of a collection, follow these steps:

1. Obtain an iterator to the start of the collection by calling the collections iterator() method.

2. Set up a loop that makes a call to hasNext(). Have the loop iterate as long as hasNext() returns true.

3. Within the loop, obtain each element by calling next().

ArrayList al = new ArrayList();
al.add("X");
al.add("Y");
al.add("Z");
Iterator itr = al.iterator();
while(itr.hasNext()) {
Object element = itr.next();
System.out.print(element + " ");
}

04/07/2015

What is servlet mapping?

Servlet mapping specifies the web container of which java servlet should be invoked for a url given by client. It maps url patterns to servlets. When there is a request from a client, servlet container decides to which application it should forward to. Then context path of url is matched for mapping servlets.
How is servlet mapping defined?

Servlets should be registered with servlet container. For that, you should add entries in web deployment descriptor web.xml. It is located in WEB-INF directory of the web application.
Entries to be done in web.xml for servlet-mapping:


milk
/drink/*


servlet-mapping has two child tags, url-pattern and servlet-name. url-pattern specifies the type of urls for which, the servlet given in servlet-name should be called. Be aware that, the container will use case-sensitive for string comparisons for servlet matching.
Syntax for servlet mapping as per servlet specification SRV.11.2:

A string beginning with a ‘/’ character and ending with a ‘/*’ suffix is used for path mapping.
A string beginning with a ‘*.’ prefix is used as an extension mapping.
A string containing only the ‘/’ character indicates the “default” servlet of the application. In this case the servlet path is the request URI minus the context path and the path info is null.
All other strings are used for exact matches only.
Rule for URL path mapping:

It is used in the following order. First successful match is used with no further attempts.

1. The container will try to find an exact match of the path of the request to the path of the servlet. A successful match selects the servlet.

2. The container will recursively try to match the longest path-prefix. This is done by stepping down the path tree a directory at a time, using the ’/’ character as a path separator. The longest match determines the servlet selected.

3. If the last segment in the URL path contains an extension (e.g. .jsp), the servlet container will try to match a servlet that handles requests for the extension. An extension is defined as the part of the last segment after the last ’.’ character.

4. If neither of the previous three rules result in a servlet match, the container will attempt to serve content appropriate for the resource requested. If a “default” servlet is defined for the application, it will be used.

03/07/2015

Constructor vs Instance block

Both instance block and constructor will be executed automatically for every object creation but instance block is first followed by constructor.

1. The main objective of constructor is to perform initialization of an object.

2. Other than initialization if we want to perform any activity for every object creation we have to define that activity inside instance block.

3. Both concepts having different purposes hence replacing one concept with another concept is not possible.

4. Constructor can take arguments but instance block can’t take any arguments hence we can’t replace constructor concept with instance block. Similarly we can’t replace instance block purpose with constructor.

Address

Hyderabad
500072

Website

Alerts

Be the first to know and let us send you an email when Stay with Java posts news and promotions. Your email address will not be used for any other purpose, and you can unsubscribe at any time.

Share