java4all@1986 java. Powered by Blogger.

ASimple Counter Example or To known How many times the servlet is accessed?

>> Thursday, June 2, 2011

In this example we are going to know how we can make a program on counter,
which will keep track how many times the servlet has been accessed.
To make this program firstly we have to make one class SimpleCounterInServlet.
The name of the class should follow the naming convention.
Remember to keep the name of the class in such a way that it becomes easy to understand,
what the program is going to do just by seeing the class name.
After making a class define one variable counter which will keep record for how many times the servlet has been accessed.
Now use method either doGet() or doPost() to write a logic of the program.
Our program logic is simple. We have to just increment the value of the counter by 1.
To display the output use the method getWriter() method of the response object,
which will in turn return the object of the PrintWriter class. Now display the value of the counter.

Program:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class SimpleCounter extends HttpServlet{
int counter = 0;
public void doGet(HttpServletRequest request, HttpServletResponse
response)throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
counter++;
pw.println("At present the value of the counter is " + counter);
}
}
First time the output is 1,
Second time output is 2.....

Read more...

How to Display date in Servlet?

>> Wednesday, June 1, 2011

In this example we are going to show how we can display a current date and time on our browser.
It is very easy to display it on our browser by using the Date class of the java.util package.
As we know that the our servlet extends the HttpServlet and overrides the doGet() method which it inherits from the HttpServlet class.
The server invokes doGet() method whenever web server recieves the GET request from the servlet.
The doGet() method takes two arguments first is HttpServletRequest object and the second one is HttpServletResponse object and this method throws the ServletException.
Whenever the user sends the request to the server then server generates two obects, first is HttpServletRequest object and the second one is HttpServletResponse object.
HttpServletRequest object represents the client's request and the HttpServletResponse represents the servlet's response.
Inside the doGet(() method our servlet has first used the setContentType() method of the response object which sets the content type of the response to text/html.
It is the standard MIME content type for the Html pages. The MIME type tells the browser what kind of data the browser is about to receive.
After that it has used the method getWriter() of the response object to retrieve a PrintWriter object.
To display the output on the browser we use the println() method of the PrintWriter class.
The coding is shown below :
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class DisplayingDate extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException{
PrintWriter pw = response.getWriter();
Date today = new Date();
pw.println("<html>"+"<body><h1>Today Date is</h1>");
pw.println("<b>"+ today+"</b></body>"+ "</html>");
}
}
WEB.XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd"> -->

<web-app>
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>DateDisplay</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/DateDisplay</url-pattern>
</servlet-mapping>
</web-app>

The output will be the present date

Read more...

A simple and easily understandable example in servlet?

We should start understanding the servlets from the beginning.
Lets start by making one program which will just print the "Hello World" on the browser.
Each time the user visits this page it will display "Hello World" to the user on ClientMachine.

As we know that the our servlet extends the HttpServlet and overrides the doGet() method which it inherits from the HttpServlet class.
The server invokes doGet() method  whenever web server recieves the GET request from the servlet.
The doGet() method takes two arguments first is HttpServletRequest object and the second one is HttpServletResponse object and this method throws the ServletException.
Whenever the user sends the request to the server then server generates two obects, first is HttpServletRequest object and the second one is HttpServletResponse object.
HttpServletRequest object represents the client's request and the HttpServletResponse represents the servlet's response.
Inside the doGet(() method our servlet has first used the setContentType() method of the response object which sets the content type of the response to text/html. It is the standard MIME content type for the Html pages. After that it has used the method getWriter() of the response object to retrieve a PrintWriter object.  To display the output on the browser we use the println() method of the PrintWriter class.


Below is the a small simple program.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet{
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<html>");
pw.println("<head><title>Hello World</title></title>");
pw.println("<body>");
pw.println("<h1>Hello World</h1>");
pw.println("</body></html>");
}
}

WEB.XML

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd"> -->

<web-app>
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>


The outout of the above program is:
                                              

Read more...

Difference Between Generic Servlet And HttpServlet?

Difference Between GenericServlet and HttpServlet:

1.)Generic Servlets are protocol independent, where as HttpServlet  are protocol dependent.

2.)Generic Servlets are incompatible with HTTP methods,where as HttpServlets are compatible with HTTP Methods.
3.)GenericServlet will not give an option for developers to specify different types of request at client machine,where HTTPServlets are Developers friendly.
4.)For each and every request type(Get,Post,Put,..) GenericServlet will use same execution mechanism ,where as HTTPServlets will separate execution process for each and request type.

Read more...

FaceBook Login

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

Total Pageviews

STATCOUNTER

  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP