TODAY JAVA SCHOOL

In java school, programming, design, computer general knowledge, web application, software, web services, social media, digital marketing, oops, concept of programming language, oops feature, console media, graphics medium, first programming, c, c ++ , Java, PHP, SQL, MySQL, HTML, HTML_5, J_query, JavaScript, Bootstrap, Framework, images with logos, examples, shared and explained.

https://www.amazon.in/b?node=26373545031&linkCode=ll2&tag=1234567801cdb-21&linkId=3b9882431b00409b44141e0344b35a15&language=en_IN&ref_=as_li_ss_tl

Breaking

Thursday, 16 April 2020

Step To Create Servlet Example

Steps to create a servlet example

There are given 6 steps to create a servlet example. These steps are required for all the servers.

The servlet example can be created by three ways:

1.    By implementing Servlet interface,
2.    By inheriting GenericServlet class, (or)
3.    By inheriting HttpServlet class
The mostly used approach is by extending HttpServlet because it provides http request specific method such as doGet(), doPost(), doHead() etc.
we are going to use apache tomcat server in this example. The steps are as follows:
1.    Create a directory structure
2.    Create a Servlet
3.    Compile the Servlet
4.    Create a deployment descriptor
5.    Start the server and deploy the project
6.    Access the servlet


1)Create a directory structures

The directory structure defines that where to put the different types of files so that web container may get the information and respond to the client.
The Sun Microsystem defines a unique standard to be followed by all the server vendors. Let's see the directory structure that must be followed to create the servlet.

example image of servlet
Fig: Step To Create Servlet Example


2)Create a Servlet

There are three ways to create the servlet.
1.    By implementing the Servlet interface
2.    By inheriting the GenericServlet class
3.    By inheriting the HttpServlet class
The HttpServlet class is widely used to create the servlet because it
provides methods to handle http requests such as doGet(), doPost,
 doHead() etc.
In this example we are going to create a servlet that extends the
 HttpServlet class. In this example, we are inheriting the HttpServlet
class and providing the implementation of the doGet() method.
 Notice that get request is the default request.

DemoServlet.java


     import javax.servlet.http.*;  
     import javax.servlet.*;  
    import java.io.*;  
   public class DemoServlet extends HttpServlet{  
  public void doGet(HttpServletRequest req,HttpServletResponse res)  
  throws ServletException,IOException  
{  
   res.setContentType("text/html");//setting the content type  
   PrintWriter pw=res.getWriter();//get the stream to write the data  
  
    //writing html in the stream  
     pw.println("<html><body>");  
     pw.println("Welcome to servlet");  
     pw.println("</body></html>");  
  
    pw.close();//closing the stream  
} }  
          


     3)Compile the servlet


For compiling the Servlet, jar file is required to be loaded.

Two ways to load the jar file

1.    set classpath
2.    paste the jar file in JRE/lib/ext folder


4)Create the deployment descriptor (web.xml file)

The deployment descriptor is an xml file, from which Web Container gets the information about the servet to be invoked.
The web container uses the Parser to get the information from the web.xml file. There are many xml parsers such as SAX, DOM and Pull.
There are many elements in the web.xml file. Here is given some necessary elements to run the simple servlet program.

web.xml file


    <web-app>  
     <servlet>  
   <servlet-name>sonoojaiswal</servlet-name>  
   <servlet-class>DemoServlet</servlet-class>  
   </servlet>  
  
   <servlet-mapping>  
   <servlet-name>sonoojaiswal</servlet-name>  
   <url-pattern>/welcome</url-pattern>  
    </servlet-mapping>  
  
    </web-app>  


5)Start the Server and deploy the project


To start Apache Tomcat server, double click on the startup.bat file under apache-tomcat/bin directory.


6) How to access the servlet

Open broser and write http://hostname:portno/contextroot/urlpatternofservlet

For example:

                                http://localhost:9999/demo/welcome   

No comments:

Post a Comment