R4R
Right Place For Right Person TM
 

R4R Java Servlet


R4R
provide basic Servlet Tutorials concept with Servlet Examples . Through R4R you can develop Servlet programming concept. R4R provide Servlet Interview Questions with answers.R4R provide Servlet Languages study materials in easy way.

Servlet Examples

2.1    Servlet Basic Example

Servlet Hot Topics

4.1    Servlet Basic

Servlet Hot Links

5.1    Servlet Basic Tutorials

 

JAVA SERVLET TECHNOLOGY

A Servlet is a Java programming language class used to extend the capabilities of servers that host applications accessed via a request-response programming model.  Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by Web servers. For such applications, Java Servlet technology defines HTTP-specific servlet classes. The javax.servlet and javax.http.servlet are two packages which provided interface and classes for writing servlets. All the servlet must implement servlet interface ,which defines life cycle method

When implementing a generic service, you can use or extend the GenericServlet class provided with the Java Servlet API. The HttpServlet class provides methods, such as doGet and doPost, for handling HTTP-specific services.

HTTP Servlet typically used to:

  1. Provide the dynamically contents which get the result from the server.

  2. Process are used to  store the data which is submitted by the HTML.

  3. Manage information about the state of a stateless HTTP. e.g. an online shopping car manages request for multiple concurrent customers.

GENERIC SERVLET CLASS CONTAINS FIVE METHODS
The generic servlet is the base class of servlet which contains five methods

1.init() method:-  Init() is called only once by the servlet container in its whole lofecycle the life of a servlet. The init() method takes a ServletConfig object that contains the initialization parameters and servlet's configuration and throws a ServletException if an exception has occurred.

2.Service() method :- This method defined as public void service as (ServletRequest req, ServletResponse res) which  throws ServletException, as IOException .If once  the servlet starting  getting the requests, the service() method is called by the servlet container to respond.

3.getservlet config():
- This method is defined as public ServletConfig getServletConfig() this method contain initialization and startup of the servlet and returns the Servlet Config object

4.getservlet info():-
This method is defined as public String getServletInfo() The information about the servlet is returned by this method like version, author etc. This method returns a string which should be in the form of plain text and not any kind of markup.

5.destroy():- This method is defined as public destroy().this method is called when want to close the servlet.

LIFE CYCLE OF SERVLET

The life cycle of a servlet is controlled by the container in which the servlet has been deployed. The life cycle of a servlet can be categorized into four parts:

  1. Loading and instantiation:-The servlet container loads the servlet during startup or when the first request is made .After loading of servlet container create the instance of the servlet

  2. Initialization:-When creating the instances,after that the servlet container calls the init() method .then passes the servlet initialization parameters to the init() method. init() method is called once through out the life cycle of servlet

  3. Servicing the request :-After successfully initialization ,servlet container  call the service method for servising any request. The service() method determines the kind of request and calls the appropriate method (doGet() or doPost()) for handling the request and sends response to the client using the methods of the response object

  4. The servlet Destroying :-If  the servlet is no longer required for servicing any request, the servlet container will  calls the destroy() method .

ADVANTAGES OF JAVA SERVLET
Java servlet provide various advantages to build a server side programming. Following are the advantages of java

  1. Portable:- As servlets are written in java and follow  APIs of java so they are very high portable over operating systems and serve implementations.

  2. Efficient:- As compared to CGI applications servlet is more efficient

  3. Extensibility:-The servlet API is designed in such a way that it can be easily extensible. As for now   the servlet API support Http Servlets, but in later  it can be extended for another type of servlet

  4. Safety:- as java is safe servlets are also safe ,garbage collection prevent the leakage of memory and exception handling throws the exceptions for the errors.

BASIC SERVLET STRUCTURE

import java.io.*;
import  javax.servlet.*;
import javax.servlet.http.*;
public class Servletname extends HttpServlet

{
	public void doGet (HttpRequest request,HttpResponse response) throws ServletException, IOException
	{
		//code for business logic
		//use request object to read client request
		//user response object to throw output back to the client
	}//close do get
}

Servlet life cycle include mainly four steps init()--->service()--->doGet() or doPost()--->destroy

A SERVLET PROGRAM (using its life cycle)


//servlet program using its life-cycle
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Testservlet extends HttpServlet
{
	int i;
	public void init() trows ServletException
	{
		i=0; //initializing i value
	}
	//incrementing i value in doGet method
	public void doGet(HttpServletRequest request, HttpServletReponse response)
		throws IOEXception ,ServletException
	{

	response.setContentType("text/html");
	PrintWriter out= resonse.getWriter():
		if (i==0)
		{ 
		out.println("<html>");
		out.prinln ("<head>");
		out.println ("<title> Hello world </title>");
		out.println ("</head>");
		out.println ("<body>");
		out.println ("<h1> value of i is initialized in init method </h1>"+"<h1>" + i + "</h1>"
		out.println("</body>");
		out.println ("</html>");

		}

		i = i+;
		if ((i == 10)
		{
			out.println("<html>");
			out.println("<head>");
			out.println ("<title> HEllo world</title>");
			out.println ("</head>");
			out.println ("<body>");
			out.println ("<h1> i's value reaches 10 hence calling destroy method to reset it </h1>' + "<h1>" + "</h1>");
			out.println ("</body>")
				out.println ("<html>");
			destroy(); //call destroy method if i=10
		}
            if (i<10) //display increment value of i
            {
				out.prinln("<html>");
				out.prinln("<head>");
				out.prinln("<title> Hello world </title>");
			    out.prinln("<head>");
				out.prinln("<body>");
    			out.prinln("<h1> value of i incremented in doGet </h1>"  + "<h1>" + i + "<h1>");
				out.prinln("</body>");
				out.prinln("</html>");
            }
}

public void destroy()//reset i value here

	{
		i=0;

	}
	}

  

RUNNING SERVLET
To run servlet first install and configure the server on your system. web server is required to install to run the servlet.

To compileAnd Run:
javac ExampServlet.java
Servlets can be called directly by typing their uniform resource locator (URL) into a browser's location window after you've started the server.
Servlets can also be invoked from an HTML form by specifying their URL in the definition for a Submit button, for example.
Servlets can be called by any program that can open an hypertext transfer protocol (HTTP) request.

What is web.xml file?
Web. xml file also called "web Deployment descriptor" allows us to configure our web application inside the Servlet containerHere we can specify the name of our web application, define our Java Servlets, specify initialization parameters for Servlets, define tag libraries, and a whole lot more. This file is placed inside the /WEB-INF folder. we use two of the features of 'web.xml' file; name of Servlet and the Servlet mapping to a URL. This will allow us to access our Servlet using a URL like /TestServlet.


<?xml version="1.0"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
		http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5"> 

	<servlet>
		<servlet-name>TestServlet</servlet-name>
		<servlet-class>servlets.TestServlet</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>TestServlet</servlet-name>
		<url-pattern>/TestServlet</url-pattern>
	</servlet-mapping>
</web-app>

To run a Servlet
Now we need to start Tomcat server. Type the following command at DOS prompt and hit Enter to start the Tomcat server:
C:\apache_tomcat_6.0.14\bin\startup Now open your browser and point to this address: http://localhost:8080/star/Testservlet. You should a get response like following image in your browser window:

SUMMARY
We began with the introduction to Java Servlets and learned that Serlvets are simple Java classes that implement javax.servlet.Servlet interface. In practice, we will most probably do that by extending javax.servlet.GenericServlet or javax.servlet.http.HttpServlet classes. By above discussions we get know that servlet is efficient and extensible as it store once .


Tolal:0 Click:
Show All Comments

Post Your Comments

Your Name:

Your Email ID :
Comments :
URL
  =* Enter SUM

New Updates

10:New Updates
Tutorials,examples and Interview Questions with answers
Posted By: Shashi     Posted Date:02.10.14

9:C# Tutorials,C# examples and C# Interview Questions with answers
R4R provide C# Tutorials,C# examples and C# Interview Questions with answers. Through R4R you can develop a small application and small programs.We cover C# Tutorials,C# examples and C# Interview Questions and answers for freshers as well experienced programmer.
Posted By: Shashi     Posted Date:11.17.13

8:Java/J2EE(Servlet,JSP,JNDI,Struts,Spring,Hibernate,EJB,JSF,JMS,Web-Services)
In this section R4R covers Java/J2EE(Servlet,JSP, JNDI, Struts, Spring, Hibernate, EJB,JSF,JMS,Web-Services) Tutorials with Examples.R4R provide Java/J2EE (Servlet,JSP, JNDI,Struts, Spring, Hibernate, EJB, JSF, JMS, Web-Services) Interview Questions with answers study materials for freshers and experienced.
Posted By: Shashi     Posted Date:11.13.13

7:CoreJava Tutorials , CoreJava Examples and CoreJava Interview Questions with answers
In this section R4R covers CoreJava Tutorials with CoreJava Examples. R4R covers CoreJava programming concept in easy way. R4R provide CoreJava Interview Questions with answers study materials for freshers and experienced.
Posted By: Shashi     Posted Date:11.04.13

6:Microsoft.net Technology ASP.NET,c# ,ADO.NET,WCF,WPF,Silverlight ,VB.net
R4R provides Microsoft Technologies(like ASP.NET,c# ,ADO.NET,WCF,WPF, Silverlight and VB.ne)Tutorials with Examples,programming concept and Interview Questions with answers study materials for freshers and experienced.
Posted By: Shashi     Posted Date:11.04.13

5:J2me Tutorials concept with Examples with Netbean IDE and J2ME Application Examples.
R4R cover basic J2me Tutorials concept with Examples and provide a way to develop J2me programming concept in easy way. R4R provide J2me Interview Questions with answers.R4R provide J2me Languages study materials with examples uing Netbean IDE and J2ME Application Examples.
Posted By: Shashi     Posted Date:10.30.13

4:J2me Tutorials concept with Examples ith netbean IDE and J2ME Application Examples.
R4R cover basic J2me Tutorials concept with Examples and provide a way to develop J2me programming concept in easy way. R4R provide J2me Interview Questions with answers.R4R provide J2me Languages study materials with examples uing Netbean IDE and J2ME Application Examples.
Posted By: Shashi     Posted Date:10.30.13

3:Java ebooks , Java tutorials , Java examples , Java interview questions and answers
R4R provides study materials for Java/j2EE technologies. R4R covers core java, advanced java, servlet, JSP, Struts, Spring, Hibernate, EJB, JSF, J2ME, ANT, JUnit and Some APIS .R4R also provides ebooks,tutorials,examples, interview questions and answers.
Posted By: Shashi     Posted Date:10.30.13

2:Learn basic C++ Tutorials with Examples , C++ Interview Questions with answers
R4R is a free E-learning website.You can learn basic C++ Tutorials with Examples , C++ Interview Questions with answers and also some hot topics.
Posted By: Shashi     Posted Date:10.30.13

1:C Tutorials with Example C subjective and objective interview questions and answers
R4R provide C Tutorials with Example and also provides C subjective and objective interview questions and answers.You can learn C in easy way.We cover basics of C here and give some examples.
Posted By: Shashi     Posted Date:10.30.13

R4R
R4R
R4R
R4R
R4R
R4R
R4R
R4R