This site "java learning concept" is very useful for programming languages like JAVA,PYTHON,C,C++, Data Structures etc.Java learning Concept also provides technical materials , which are related to latest technologies. We provides very easy lectures to our viewers from beginning to advance level.We focus logical content instead of theoretical content.Our coding lecture are just fun and hope you will enjoy.

Monday, 23 September 2019

SERVLETS IN JAVA

Servlets In java Programming

SERVLETS IN JAVA:

Introduction:

 Servlet is a  basically server side programming concept in java, in servlets we don't need to compile our program like core java applications because we run servlets on the server like tomcat,weblogic,jbose server etc. Servlet interface is present in javax.servlet package.
In servlet we don't need to install any software manually like JDK in core java because server vandors provide the support  in the form of jar files like servlet-api.jar in tomcat server.
By using servlet we can create web applications  as well as enterprise application by using servlets,so it's very simple and easy to use in our program.

                   interface Servlet
                  {
                       public void init(ServletConfig config);
                       public void service(ServletRequest req, ServletResponse res);
                       public void destroy();
                       public String getServletInfo();
                       public servletConfig getServletConfig();


                  }

 We have Three ways to create servlet in java.


1.  By implements Servlet interface
2. By extends GenricServlet
3. By extnds HttpServlet

1By implements Servlet interface :

 Inside  the Servlet interface there are five abstract methods are available ,we need to provide the implementation for all following  five methods weather we are using or not otherwise we can't run our application. Even though we write the logic in service() method  only. So that it is not recommended to use , it's increase the length of the code and create performance problems

ex:          class MyServlet implements Servlet
             {
                   public void init(Servletconfig  config)
                  {
                         // to Initialization servlet 
                  }

                   public void service(ServletRequest req, ServletResponse res)
                  {
                           // To write business logic
                       
                  }



                 public void destroy()
                {
                      // to destroy the servlet

                }

                   public String getServletInfo(){
                         
                 }

                     public servletConfig getServletConfig()
                    {

                    }

          }

2. By extends GenricServlet: 

There is an another way  to create servelet in java. In GenricServlet we don't need to provide implementation to all the five methods, we need to provide implementation only service() method where we can write our business logic. It is also known as adapter class in java and it is a abstract class as well because one abstract service method present So we can't create object of GenricServlet class.

GenricServlet is a protocol independent so that if there is a  HTTP request or SMTP request same service() method will be executed. So it's not recommanded to use .

 abstract class MyServlet extends GenricServlet
{
                      public void service(ServletRequest req, ServletResponse res)
                     {
                           // To write business logic
                       
                     }


}

3. By extends HttpServlet : 

HttpServlet is a best and generally  use to create servlet in  java. In the  HttpServlet class there  is no abstract method present but still it is a abstract class because partially implementation of methods present in HttpServlet class we can't create an object of HttpServlet class.
HttpServlet class is protocol dependent , which means we can send only HTTP type of request to the client browser  and every time different methods will be executed.
There are following requests we can send to the client browser.
  1. doGet()
  2. toPost()
  3. toDelete()
  4. doTrace()
  5. doPut()
  6. doHead()
  7. doOption()
Example: 
package com.kl;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class FirstServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
    public FirstServlet() {
        super();
     
    }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws Exception {


response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("Hello My First Servlet ");

}


}

How to set up :  
If you want to run your servlet we need a server like tomcat and others. server is a mandatory for servlets.

Download Eclipse IDE:

Click for download

Open your  Eclipse IDE and create your first servlet program in eclipse ..






















Add caption

Output
:
Hello My First Servlet




4 comments:

  1. Hello! Great article and thank You for Providing Such a Unique and valuable information for your readers. I really appreciate it. You can also visit UK Receipt app for more Verd Tech related information and knowledge

    ReplyDelete
    Replies
    1. Hey,
      thanks for appreciate my work . I am always looking forward to learn stuff,I will definitely follow your points,I am a student so please keep supporting me to learn things. please keep sharing my post as much as you can.

      Delete
  2. I agree with a lot of the points you made in this article. If you are looking for the serp api, then visit SERP House. I appreciate the work you have put into this and hope you continue writing on this subject.

    ReplyDelete
  3. Thanks a lot for appreciate my work. I am always looking forward yo learn new stuff, thanks for your kind suggestion I will definitely follow your points. Please kindly share my post as much as you can.

    ReplyDelete

Adbox