Back to Top

Sunday, 8 July 2012

J2EE: SERVLET: doGet() vs. doPost()

doGet():

  • protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, java.io.IOException – is a method of Class HttpServlet. It is called by the server (via the service method) to allow a servlet to handle a client's GET request.
  • In doGet(), the client’s request parameters are appended to the URL and sent along with header information.
  • doGet() is not a secured process because the request parameters are clearly visible in the URL.
  • Since request parameters are sent along with header information, so only a limited amount of data should be sent.

Sunday, 8 July 2012 by Anijit Sarkar · 6 Read more »

Monday, 5 March 2012

JAVA: What is the superclass of Exception

java.lang.Throwable is the superclass of all the errors and exceptions in java.

Monday, 5 March 2012 by Anijit Sarkar · 5 Read more »

JAVA: Why Errors are Unchecked?


Error is unpredictable. It can occur at any point and recover is also quite impossible. So it is meaningless to declare such exception. That’s why, in java, error is unchecked.

by Anijit Sarkar · 5 Read more »

Thursday, 1 March 2012

J2EE: SERVLET: Class GenericServlet vs Class HttpServlet


·                    It is an abstract class which defines a generic and protocol independent servlet. Here, protocol independent means, by default, it doesn’t contain inherent support for any particular type of protocol, but it can be extended to provide implementation of any protocol.

·                    Class GenericServlet implements Interface Servlet and Interface ServletConfig  and it belongs to javax.servlet package.

·                    It offers simpler version of the servlet life cycle methods init() and destroy() and the methods of ServletConfig. That’s why, it makes writing a servlet easier.

·                    Here, the method service(ServletRequest req, ServletResponse res) is abstract, so the subclasses must override it. And this is also the reason why the GenericServlet is an abstract class.

Thursday, 1 March 2012 by Anijit Sarkar · 4 Read more »

Wednesday, 29 February 2012

J2EE: SERVLET: What is Servlet Interface?


Interface javax.servlet.Servlet is that interface which defines the ServletLife Cycle methods. So, all servlets must implement this interface, either directly or by extending a class which implements this interface. Instead of directly implementing the Servlet Interface, a servlet can also extend Class GenericServlet (which implements Servlet Interface), or can extend HttpServlet Class (which extends the GenericServlet class).


Wednesday, 29 February 2012 by Anijit Sarkar · 7 Read more »

Monday, 27 February 2012

J2EE: SERVLET: What is Servlet?


Servlet:

Servlet is a server-side (runs within a Web server), Java Programming Language Class which interacts with clients via request-response programming model (example, HTTP Request-Response model).

It can respond to any type of request, but, generally, it is used to extend the application hosted by Web Server by providing dynamic web content.

It has build-in threading capability and provides a secure environment for programming.

Monday, 27 February 2012 by Anijit Sarkar · 14 Read more »

Friday, 24 February 2012

JAVA: Why can’t enum extends any other class?

By default, all enums implicitly extend java.lang.Enum. And as we know, Java doesn't support multiple inheritance, an enum cannot extend any other class.

Friday, 24 February 2012 by Anijit Sarkar · 1 Read more »

Monday, 20 February 2012

J2EE: SERVLET: SERVLET API


   Java Servlet API is that Application Program Interface (API), which defines and describes the contracts between servlets and servers. That means, it contains the set of programs and standard instructions to develop a proper access between a servlet and the server. Servlet API is a part of the official J2EE API. This API contains 2 packages with the extension javax. They are as follows.

·        Package javax.servlet
·        Package javax.servlet.http

Monday, 20 February 2012 by Anijit Sarkar · 5 Read more »

Friday, 17 February 2012

J2EE: SERVLET: What happens to Session if the client intentionally turns off the cookies?


   If client intentionally turns off the cookies, that means, the client chooses not to join the session. In this situation, until the client turns on the cookies, getSession will return a different session on each request, and isNew will always return true.
Anijit Sarkar

Friday, 17 February 2012 by Anijit Sarkar · 1 Read more »

J2EE: SERVLET: Session Tracking in Servlets using Interface HttpSession


  • Session tracking in Servlets is done by using Interface HttpSession. It helps to identify a client throughout many page requests or visiting a website and to store details about that client.
  • Whenever a request arrives, it checks for the preexisting session object, to fetch details of the client from there, else, if its first request from a new client, its simple creates a session object for that client. These session objects are simple java objects.
  • HttpSession contains various methods to handle session object. HttpSession object lives in the server, and it automatically associated with the requester by internal mechanism like cookies or URL-rewriting
  • Whenever an application invokes or removes an object from a session, the session checks whether the object implemented the interface HttpSessionBindingListener or not. If yes, the servlet informs the object that it has been bounded to the session, if not, notifies unbound from the session. This alert goes after the binding methods complete.

by Anijit Sarkar · 2 Read more »

Saturday, 11 February 2012

INTERNET: HTTP vs. HTTPS


  •  HTTP stands for HyperText Transfer Protocol, whereas, HTTPS is HyperText Transfer Protocol Secure.
  • HTTP transmits everything as plan text, while HTTPS provides encrypted communication, so that only the recipient can decrypt and read the information.  Basically, HTTPS is a combination of HTTP and SSL (Secure Sockets Layer). This SSL is that protocol which encrypts the data.
  • HTTP is fast and cheap, where HTTPS is slow and expensive.
  • As, HTTPS is safe it’s widely used during payment transactions or any sensitive transactions over the internet. On the other hand, HTTP is used most of the sites over the net, even this blogspot sites also use HTTP.

Saturday, 11 February 2012 by Anijit Sarkar · 3 Read more »

Friday, 10 February 2012

INTERNET: HTTP Request and HTTP Response


  • HTTP is a stateless protocol. That means, It doesn’t contain any information regarding the client.
  • It works as a request-response protocol in the client-side computing model.
  • As it’s an Application Layer Protocol within the Framework Internet Protocol Suite, needs a Transfer Layer Protocol (usually Transmission Control Protocol) for data transmission.  
  • It carries request from Client-side (web browser) to the web server, known as HTTP Request. And again the response from the web-server to the browser, known as HTTP Response.


·        HTTP Request means transferring a request form the client side to the server.
·        It contains different types of HTTP Request Methods, which are used to indicate the type of the request.
·        It has URI which locate the destination.
·        It also has Protocol Version (Example: HTTP/1.1)
·        Then comes the Request Header, which contains information regarding the 
client environment like, Accept-Charset, Accept-Encoding, Authorization,etc.
·        Then there is a Message Body section, which contains actual information regarding the request.

Friday, 10 February 2012 by Anijit Sarkar · 8 Read more »

Thursday, 9 February 2012

INTERNET: Importance & Types of Session Tracking


  HTTP is a stateless protocol. That means it doesn’t contain any information regarding the client, where it’s coming from. So it’s hard for Web Server to know whether it’s a request from a new user or not. This behavior can cause many problems.
For example, suppose a user has logged into a site. Now he wants to go to a particular section which is on another page then he would be required to login again as the requested page would be a login-protected page and the Web Server doesn't know if the user requesting this page is the one who is already logged in or if it's a new user. Therefore, to overcome this problem, session tracking is introduced. So session tracking is the process to maintain the important information of a client as a session.

Thursday, 9 February 2012 by Anijit Sarkar · 1 Read more »

Monday, 6 February 2012

J2EE: Web Container vs Web Server vs Application Server

Web Container / Servlet Container / Servlet Engine :
  • In J2EE Architecture, a web container(also known as servlet container or servlet engine), is used to manage the components like servlets, JSP.
  • It provides a runtime environment to the components of J2EE.
  • When ever web server receive a request it forward it to web container which deals with it by instantiating, initializing and invoking Servlets and JSP pages. So,basically it controls the whole life cycle of servlet and JSP. 
  • It is a part of the web server.
  • Example: Apache Tomcat.

Monday, 6 February 2012 by Anijit Sarkar · 19 Read more »

Saturday, 4 February 2012

J2EE: SERVLET: Servlet Life Cycle

  Life cycle of a servlet starts when its first load in the memory space in web server and ends when unloaded from the server memory. we can divide this in 3 steps.


  • Server loads the servlet class.
  • Creates an instance for the servlet class.
  • Server initializes the instance by calling init() method.

Saturday, 4 February 2012 by Anijit Sarkar · 26 Read more »

Tuesday, 31 January 2012

JAVA: FileInputStream vs FileReader

Class FileInputStream:
  • It is use for reading stream of data from a file in the form of raw bytes(8-bit).
  • Very useful to read image.
  • It can also read character files.
Hierarchy of Class FileInputStream:
Hierarchy of Class FileInputStream in java


Tuesday, 31 January 2012 by Anijit Sarkar · 5 Read more »

Sunday, 29 January 2012

JAVA: Nested Class

 The class which is defined within another class is called nested class.
It can be divided into 2 types. They are -   static nested class & non-static nested class


Static Nested Class: 
  • Nested class which is declared static is static nested class.
  • Its methods & variables are associated with is mother/outer class.
  • It can be used only through object reference.
  • It is accessed using enclosing class name. Example: MotherClass.StaticNestedClass
  • It interacts with enclosing class or other class like any other top-level class.

Sunday, 29 January 2012 by Anijit Sarkar · 7 Read more »

Saturday, 28 January 2012

JAVA: Difference between throw & throws

throw is used to throw an exception in a program, explicitly.
Whereas,  throws is included in the method's declaration part, with a list of exceptions that the method can possible throw. It is necessary for all exceptions(except for Error & RuntimeException & their sub-classes).


Saturday, 28 January 2012 by Anijit Sarkar · 6 Read more »

Friday, 27 January 2012

JAVA: What are the differences between the String and StringBuffer classes?

String class :
  1. Once an object is created and initialized, it can't be changed because it is Immutable.
  2. String object can be created Implicitly and Explicitly.

StringBuffer class :
  1.  StringBuffer class is mutable(can be modified), length and content can be changed through certain method calls. 
  2. It creates object Explicitly.

Friday, 27 January 2012 by Anijit Sarkar · 2 Read more »

Wednesday, 25 January 2012

JAVA: What is the super class of all classes?

The class Object(java.lang.Object), is the super class of all classes in Java.

Wednesday, 25 January 2012 by Anijit Sarkar · 4 Read more »

Saturday, 21 January 2012

JAVA: What is the difference between error and an exception?

In Java, under Throwable class, there are 2 sub-classes named Exception and Error.

Exception class:
  •  It is used for exceptional conditions that user programs should catch.
  •  It's also the class that you will sub-class to create your own custom exception types.
  •  For example, NullPointerException will be thrown if you try using a null reference.

Saturday, 21 January 2012 by Anijit Sarkar · 2 Read more »

Thursday, 19 January 2012

JAVA: What is a Container?

  • Container is a component which can contain other components inside itself. It is also an instance of a subclass of  java.awt.Container, which extends java.awt.Component
  • An applet is a container. 
  • Containers include windows, frames, dialogs, and panels
  • As a Container itself  is a component, it can contain other containers.
  • Every container has a LayoutManager which helps to determines how different components are placed within the container.


Thursday, 19 January 2012 by Anijit Sarkar · 1 Read more »

Wednesday, 18 January 2012

JAVA: Checked and UnChecked Exception

In Java, there are lots of sub-classes under java.lang.Exception class. we can divide these sub-classes into 2 types on the basis of compile-time checking.
The types are - Checked Exception  & Unchecked Exception.

Checked Exception: 
  • Except RuntimeException( java.lang.RuntimeException) class, all sub-classes of Exception are checked exception.
  • These exceptions need to include in a method's throws list, because these are checked by the compiler during compile time if a method handles or throws these exception. 

Wednesday, 18 January 2012 by Anijit Sarkar · 1 Read more »

JAVA: Wrapper class

  Wrapper class is a special type of class that's  used to make primitive variables into objects, so that the primitives can be included in activities reserved for objects, like as being added to Collections, or returned from a method with an object return value. It simply wraps or encapsulates a single value for the primitive data types. These classes contain methods that provide basic functions such as class conversion, value testing & equality checks and the constructors of wrapper classes, allow objects can create and convert primitives to and from String objects.

     

by Anijit Sarkar · 10 Read more »

Tuesday, 17 January 2012

JAVA: Externalizable interface - uses.

1. Interface Externalizable gives a control over the serialization mechanism.

2. It has two methods readExternal and writeExternal, which are use to customize the serialization process.

Tuesday, 17 January 2012 by Anijit Sarkar · 1 Read more »

JAVA: Serialization-Common usage.

1. An object needs to be serialized, when it's need to be sent over the network.

2. If the state of an object is to be saved, objects need to be serialized.

by Anijit Sarkar · 3 Read more »

Java: What are the methods of interface Serializable?

 Interface Serializable has no methods, it's empty.

by Anijit Sarkar · 1 Read more »

JAVA: How to serialize an object to a file?


      By implementing an interface Serializable in the class whose instances are to be serialized.Then passing the instance to the ObjectOutputStream which is connected to a fileoutputstream. This will save the object to a file.

Example: 
   Now, we will see a practical implementation of serialization on an object and we will store the serialized data in a file.
Class SampleClass is a pojo class which implements java.io.Serializable . It contains four String fields, one of which is declared as transient. That means, we can't serialized this field.


Class SampleClass :

package com.interview.question.java;

public class SampleClass implements java.io.Serializable{
     
  /**
    *
    * Sample serialized class.
    *
    * @author http://interviewquestionjava.blogspot.com
    *
    **/
     
      private static final long serialVersionUID = 1L;

      private String strName;
     
      private String strAddress;
     
      private String strUsername;
     
     /**
       *
       * we declared the variable strPassword as transient.
       * So, it will be shield from serialization.
       *
       **/
      private transient String strPassword;

      public String getStrName() {
            return strName;
      }

      public void setStrName(String strName) {
            this.strName = strName;
      }

      public String getStrAddress() {
            return strAddress;
      }

      public void setStrAddress(String strAddress) {
            this.strAddress = strAddress;
      }

      public String getStrUsername() {
            return strUsername;
      }

      public void setStrUsername(String strUsername) {
            this.strUsername = strUsername;
      }

      public String getStrPassword() {
            return strPassword;
      }

      public void setStrPassword(String strPassword) {
            this.strPassword = strPassword;
      }
     
     

}

    Now,  Class SerializeObjectToFile, is used to create an object of SampleClass with some values.
Then we serialized that object to a file name sampleClass.ser.

Class SerializeObjectToFile :


package com.interview.question.java;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class SerializeObjectToFile {

/**
    *
    * This class serialized an Object of SampleClass
    * to a file named sampleClass.ser
    *
    * @author http://interviewquestionjava.blogspot.com
    *
    **/

 public static void main(String[] args) {
 
  SampleClass objSampleClass = new SampleClass();
 
  objSampleClass.setStrName("Ashoka Maurya");
  objSampleClass.setStrAddress("Magadha");
  objSampleClass.setStrUsername("Chakravartin");
  objSampleClass.setStrPassword("Ashokavadana");
 
  try 
  {
   FileOutputStream objFileOutputStream = new FileOutputStream("src/com/interview/question/java/sampleClass.ser");
   ObjectOutputStream objOutputStream = new ObjectOutputStream(objFileOutputStream);
   objOutputStream.writeObject(objSampleClass);
   objOutputStream.close();
   objFileOutputStream.close();
  
   System.out.println("SampleClass is serialized successfully..");
  } 
  catch (IOException e) 
  {
   e.printStackTrace();
  }
 

 }

}


Now, to deserialize this data back to an object please check the post Deserializing an Object from File

by Anijit Sarkar · 3 Read more »

Popular Posts

Subscribe via Email
Subscribe Java Interview Questions via Email
All Rights Reserved JAVA INTERVIEW QUESTIONS | Privacy Policy | Anijit Sarkar
blog links Web Directory Academics Blogs