java4all@1986 java. Powered by Blogger.

Example to display infomation about the server?

>> Thursday, June 2, 2011

In this program we are going to tell you how can a use servlet to display information about its server.
Firstly we will create a class in which there will be doGet() method which takes two objects as arguments,
first is request object and the second one is of response.
To display the name of the server you are using use the method getServerName() of the ServletRequest interface.
To display the server port number use the method getServerPort().
You can also use other methods of the ServletRequest interface like getProtocol() to display the protocol,
you are using and many more methods depending on your needs.

The code of the program is given below: 
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class SnoopingServerServlet extends HttpServlet{
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
PrintWriter pw = response.getWriter();
pw.println("The server name is " + request.getServerName()+"<br>");
pw.println("The server port number is " + request.getServerPort()+
"<br>");
pw.println("The protocol is " + request.getProtocol()+ "<br>");
pw.println("The scheme used is " + request.getScheme());
}
}

OUTPUT:
The server name is localhost <br>
The server port number is 8086<br>
The  portocol is HTTP/1.1 <br>
The scheme used is http.

Read more...

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

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...

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