Showing posts with label J2ee Interview Question. Show all posts
Showing posts with label J2ee Interview Question. Show all posts

Tuesday, 12 December 2017

HTTP Status Codes

    When browser requests a service from web server, it will return some status code. I have listed all HTTP Status codes as below,

  • 1xx -  Informational


100 - Continue - The server has received the request headers, and the client should proceed to send
 the request body

101 - Switching Protocols -  The Server switches protocol.


  • 2xx - Successful 

 200 - OK      -  This is a successful HTTP request

201 - Created  -  Request is completed, new resource is created.

202 - AcceptedThe request is accepted for processing, but the processing is not complete.

203 - Non-Authoritative Information -  The information in the entity header is from a local or third party copy, not from the original server.

204 - No Content  -  A status code and a header are given in the response, but there is no entity-body in the reply.

205 - Reset Content - Browser will clear the form data.

206 - Partial Content - The server is delivering only part of the resource due to a range header sent by the client

  •  3xx - Redirection


300 - Multiple Choices - A link list. The user can select a link and go to that location. Maximum five addresses  .

301 - Moved Permanently - Requested page permanently moved to new URL.

302 - Found - Requested page temporarily moved to new URL.

303 - See other - Requested page can be found some other URL.

304 - Not Modified - Requested has not modified.

305 - Use Proxy - The requested URL must be accessed through the proxy mentioned in the Location header.

306 - Unused - This code was used in a previous version. It is no longer used, but the code is reserved. 

307 - Temporary Redirection - The requested page has moved temporarily to a new URL

  • 4xx - Client error

400 - Bad Request - The Request can not be fulfilled because of some bad syntax.

401 - Unauthorized - The request has not been applied because it lacks valid authentication credentials for the target resource.

402 - Payment Required - Now you can not use this code.

403 - Forbidden - The request is a legal request, but the server is refusing to respond to it due to some factor.

404 - Page Not Found - Requested page is not found.

405 - Method Not Allowed - Method specified in the request is not allowed

406 - Not Acceptable - Generated server response not accepts the client.

407 - Proxy Authentication Required - The client must first authenticate itself with the proxy 

408 - Request Timeout - The Server time out waiting for the request

409 - Conflict - The request could not be completed because of a conflict.

410 - Gone - The requested page is no longer available.

411 - Length Required - The server refuses to accept the request without a defined Content-Length.

412 - Pre condition failed - The precondition given in the request evaluated to false by the server 

413 - Request Entity too large - The server will not accept the request, because the request entity is  too large.

414 -  Request URI too long - The server will not accept the request, because the url is too long. Occurs when you convert a "post" request to a "get" request with a long query information .

415 -  Unsupported Media Type - If media type is not supported, it won't accept the request.

416 - Request range not satisfiable - The client has asked for a portion of the file, but the server cannot supply that portion

417 - Exception failed - The server cannot meet the requirements of the Expect request-header field


  • 5xx - Server Error

500 - Internal Server Error - The request was not completed.  The server met an unexpected condition.

501 - Not Implemented - The server does not support the functionality required to fulfill the request. 

 502 - Bad Gateway - The server, while acting as a gateway or proxy, received an invalid response  from an inbound server it accessed while attempting to fulfill the request. 

503 - Service Unavailable - The server is currently unavailable

504 - Gateway Timeout - The server, while acting as a gateway or proxy, did not receive a timely response from an upstream server it needed to access in order to complete the request.

505 - HTTP Version not supported - The server does not support the HTTP protocol version used in the request.

Related Post : - 

Tuesday, 6 October 2015

What is PermGen in Java? How to solve the Java.Lang.OutOfMemoryError: PermGen Space

          The java.lang.OutOfMemoryError in Java is a subclass of java.lang.VirtualMachineError and JVM throws java.lang.OutOfMemoryError when it ran out of memory in heap. When you will try to create an object and there is not enough space in heap to allocate that object then you will get OutOfMemoryError.

         Types of OutOfMemoryError in Java

1) Java.lang.OutOfMemoryError: Java heap space

2) Java.lang.OutOfMemoryError: PermGen space

Now we will discuss about PernGen space,

PermGen stands for Permanent Generation.

       Java applications are only allowed to use a limited amount of memory. The exact amount of memory your particular application can use is specified during application startup. To make things more complex, Java memory is separated into Young, Tenured and PermGen regions.
 
      The size of all these is set during the JVM launch. If you didn't set the sizes of these regions then platform specific default will be used(The default PermGen Space allocated is 64 MB for server mode and 32 MB for client mode).


Causes:

       The mainly the permanent generation consists of class declarations loaded and stored into PermGen. This includes the name and fields of the class, methods with the method bytecode, constant pool information, object arrays and type arrays associated with a class and Just In Time compiler optimizations.

        From the above paragraph, we can say that the main cause for  the java.lang.OutOfMemoryError: PermGen space is that either too many classes or too big classes are loaded to the permanent generation.


Solution:--

          As explained in above, this OutOfMemory error in java comes when Permanent generation of heap filled up.
         To fix this OutOfMemoryError in Java you need to increase heap size of  Perm space by using JVM option   "-XX:MaxPermSize".
         You can also specify initial size of Perm space by using    "-XX:PermSize" and keeping both initial and maximum Perm Space you can prevent some full garbage collection which may occur when Perm Space gets re-sized. Here is how you can specify initial and maximum Perm size in Java:

export JVM_ARGS="-XX:PermSize=64M -XX:MaxPermSize=256m"

Note:-
      In Java 8, PermGen area has been replaced by MetaSpace area, which is more efficient and is unlimited by default (or more precisely - limited by amount of native memory, depending on 32 or 64 bit jvm and OS virtual memory availability) .


Related Post :--
How to generate the StackOverflowError and OutOfMemoryError programmatically in Java  

Saturday, 28 June 2014

What is Dependency Injection in Spring ? Explain DI types and advantages with examples

             Dependency Injection (DI) in Spring is a design pattern used to achieve Inversion of Control (IoC) between classes and their dependencies. It allows objects to be loosely coupled by injecting the dependencies from the outside rather than creating them internally.

            The basic concept of the Inversion of Control pattern  is that you do not create your objects but describe how they should be created. You don't directly connect your components and services together in code but describe which services are needed by which components in a configuration file. A container (in the case of the Spring framework, the IOC container) is then responsible for hooking it all up. In a typical IOC scenario, the container creates all the objects, wires them together by setting the necessary properties, and determines when methods will be invoked.  
    
        There are two types of DI,
  • Setter Injection
  • Constructor Injection                                                                 

Setter Injection:-

    Spring framework will inject the dependency via a setter method.
XML configuration for setter Dependency Injection is below.

  <bean id="classBean" class="com.adnjava.ClassBean">
     <!-- setter injection using the nested <ref/> element -->
       <property name="studentBean"><ref bean="studentBean"/></property>
  </bean>
  <bean id="studentBean" class="com.adnjava.StudentBean"/> 

         The ClassBean java POJO class is as below,

 package com.adnjava;

  public class ClassBean {

       private StudentBean studentBean;

       public void setStudentBean(StudentBean studentBean){
           this.studentBean=studentBean;
       }

       public void getStudentBean(){
             return studentBean;
       }
 }

 Constructor Injection:-

          Here Spring uses the  Constructor and the arguments passed to it to determine the dependency. Rest all is same as setter injection.
     
      XML configuration for
Constructor Dependency Injection is below.


  <bean id="classBean" class="com.adnjava.ClassBean">
       <constructor-arg><ref bean="studentBean"/></constructor-arg>
      <!--OR you can use <constructor-arg ref="yetAnotherBean"/>-->
  </bean>
  <bean id="studentBean" class="com.adnjava.StudentBean"/>  

         The ClassBean java POJO class is as below,

 package com.adnjava;
  
  public class ClassBean {

         private StudentBean studentBean;

         public ClassBean(StudentBean studentBean){
              this.studentBean=studentBean;
         }

         public void getStudentBean(){
               return studentBean;
         }
  }


              Interface Injection:

             This is not implemented in Spring currently, but by Avalon. It’s a different type of DI that involves mapping items to inject to specific interfaces.

Advantages Of Dependency Injection:

  • Loosely couple code
  • Separation of responsibility
  • Configuration and code is separate.
  • Using configuration, you can provide implemented code  without changing the dependent code.
  • Testing can be performed using mock objects.

Related Post:--
Spring MVC workflow with example  
Spring MVC with Hibernate CRUD Example  
Spring Annotations  

Monday, 2 June 2014

Spring MVC workflow with example

    In the current topic, we will learn Spring MVC architecture and sample example.

Below is the architecture of Spring MVC with explanation.

Spring MVC workflow
Spring MVC workflow
       Before studying Spring,to understand the MVC architecture and advantages. So Spring framework is also depends upon the MVC Architecture. The MVC consists of three kinds of Objects i.e Model,View and Controller. Lets start explaining one by one.

1) Model
   
      It handles data processing and database works part. Model processes events sent by controller. After processing these events then it sends processed data to controller (thus, controller may reprocess it) or directly to view side.

2) View
 
       View prepares an interface to show data to the user. Controller or model tells view what to show to the user or client. Also view handles requests from user and informs controller.

3) Controller
    
        Let’s say controller is main class in mvc,  it processes every request, prepares other parts of the system like model and view. After processing data, will send response to the end user.
           
      Advantages of MVC Framework:
  1.  Re-usability of code.
  2.  Easy to maintain and enhance the project.
  3.  Attach multiple views to a model to provide different presentations(view/model decoupling)
  4.  Change the way a view responds to user input without changing its visual presentation (view/controller decoupling)
  5.  Parallel Development will be possible.
  6.  Easily divide the team according to their skill(e.g Presentation and Business logic is may developed by different persons.

Now start with Spring MVC workflow.

Step 1: First request will be received by DispatcherServlet
Step 2: DispatcherServlet will take the help of HandlerMapping and find the appropriate  Controller class name associated with the given request.
Step 3: So request transfer to the Controller, and then controller will process the request by executing appropriate methods and returns ModeAndView object (contains Model data and View name) back to the DispatcherServlet.
Step 4: Now DispatcherServlet send the model object to the ViewResolver to get the actual view page.
Step 5: Finally DispatcherServlet will pass the Model object to the View page to display the result.

 
            Front Controller has a very important role to play in the MVC Frameworks work flow. The front controller component in a MVC framework is responsible to capture the requests landing at the application and route them into the control of the MVC Framework. In Spring 3 MVC the DispatcherServlet acts as the Front Controller and is responsible to capture the request and route it into the frameworks control.

     Apart from capturing the requests, the DispatcherServlet is also responsible to initialize the frameworks components which are used to process the request at various stages.

         
DispatcherServlet Initialization Of Framework:

         During the application start up, the dispatcher servlet will initialize the following components in the framework.
  1. MultipartResolver
  2. LocaleResolver.
  3. ThemeResolver.
  4. HandlerMappings.
  5. HandlerAdapters.
  6. HandlerExceptionResolvers.
  7. RequestToViewNameTranslators.
  8. ViewResolvers.
                  
Advantages of Spring compared to other Frameworks:
  1. Spring is relatively light weight container in comparison to other J2EE containers. It does not use much memory and CPU cycles for loading beans, providing services like transaction control , AOP management, JDBC interaction.
  2. Spring makes it easy to develop J2EE application as it has built-in support for WEB MVC framework.
  3. Spring helps creating loosely coupled applications by DI.
  4. Spring has no dependency on any application servers.
  5. Spring creates objects lazily which helps in developing light weight applications.
  6. Spring supports aspect oriented programming to manage logging, transaction, security etc.
  7. Spring makes Database interaction (operations) easier as it has support for data access techniques such as DAO, JDBC, Hibernate, IBATIS, JDO etc.
  8. With the help of spring application code can be unit tested easily as Spring provides unit testing support classes.
  9. Spring does not need unique deployment steps.
  10. Spring configuration is done in standard XML format which easy to write and understand.
Thank you for visiting the blog.