Thursday 17 June 2010

Displaying Date

Steps:

Maintain Folder Structure


GoTo your folder DisplayDate
under src folder
create java file DisplayingDate.java

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>");
 }
}

Compile this program.
javac DisplayingDate.java

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>");
 }
}

Again compile:
javac DisplayingDate.java

We got errors that packages does not exist
package  javax.servlet.
package  javax.servlet.http.
Because we have not set the class path.
So set the class path
E:/DisplayDate/src>set classpath=C:\Program Files\Apache Software Foundation\Tomcat 8.0\lib\servlet-api.jar;
now compile the program
javac DisplayingDate.java

Now ok.
Go to WEB-INF folder
open web.xml file
<web-app>
 <servlet>
  <servlet-name>Hello</servlet-name>
  <servlet-class>DisplayDate</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>Hello</servlet-name>
 <url-pattern>/DateDisplay</url-pattern>
</servlet-mapping>
</web-app>

-->Save the file
Deploy your project (folder) DisplayDate into Servler
Go to apache tomcat server folder




Start the server:


Go to any browser:
and type your local host with port no
http://localhost:8080/





type user name and password; (at the time of installing tomcat server we should set the username and password.)
here my username is: root
password is root.










Tuesday 15 June 2010

How many ways to develop servlet programs

There are three ways to develop

1. Take Java class implementing javax.servlet.Servlet(Interface) and provide implementation to all the 5 methods of that interface

2. Take a java class extending from javax. servlet.GenericServlet(class) and provide implementation to public void service(-,-) method.

3. Take a Java class extending from javax.servlet.HttpServlet(class) and override one of the seven (7) doXXX(-,-) methods.

How many types of servlets?

There is a possibilities of developing 'n' types of servlets like httpServlet, ftpservlet, smtpServlet and etc,...for all these protocals are specific servlet classes.
GenericServlet is the super class contains common properties and logics. 

Monday 14 June 2010

Web Application Folder Structure






Work flow of Servlet

Work flow of Servlet

The server checks if the servlet is requested for the first time.
If yes, web container does the following tasks:
loads the servlet class.
instantiates the servlet class.

calls the init() method passing the ServletConfig object
else
calls the service() method passing request and response objects

The web container calls the destroy method when it needs to remove the servlet such as at time of stopping server or undeploying the project.

How web container handles the servlet request?
The web container is responsible to handle the request. Let's see how it handles the request.
maps the request with the servlet in the web.xml file.
creates request and response objects for this request
calls the service method on the thread

The public service method internally calls the protected service method
The protected service method calls the doGet method depending on the type of request.
The doGet method generates the response and it is passed to the client.
After sending the response, the web container deletes the request and response objects. The thread is contained in the thread pool or deleted depends on the server implementation.

What is written inside the public service method?
The public service method converts the ServletRequest object into the HttpServletRequest type and ServletResponse object into the HttpServletResponse type. Then, calls the service method passing these objects. Let's see the internal code:

public void service(ServletRequest req, ServletResponse res) 
        throws ServletException, IOException 
    { 
        HttpServletRequest request; 
        HttpServletResponse response; 
        try 
        { 
            request = (HttpServletRequest)req; 
            response = (HttpServletResponse)res; 
        } 
        catch(ClassCastException e) 
        { 
            throw new ServletException("non-HTTP request or response"); 
        } 
        service(request, response); 
    } 


What is written inside the protected service method?
The protected service method checks the type of request, if request type is get, it calls doGet method, if request type is post, it calls doPost method, so on. Let's see the internal code:

protected void service(HttpServletRequest req, HttpServletResponse resp) 
        throws ServletException, IOException 
    { 
        String method = req.getMethod(); 
        if(method.equals("GET")) 
        { 
            long lastModified = getLastModified(req); 
            if(lastModified == -1L) 
            { 
                doGet(req, resp); 
            }  
    .... 
    //rest of the code 
        } 
    } 



You may like the following posts:
Web Container

How to access your Web Application in other systems

I would like to make available to other to access my web application:

1. Once your web application is ready
2. Go to command prompt
3. type ifconfig (press enter)

Note down your Computer  IP Address. for example: IPv4 Address. . . . . . . . . . . : 192.168.0.112
Start your server to Run your Application. (Here I'm using Tomcat Server)





 So your local web portal link is:
Go to Any browser :
192.168.0.112:8080/ServRequest/
Syntax: your system ip address:port number/url-name


Wednesday 9 June 2010

Example program on Servlet Request


index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Servlet Request</title>
<form method="get" action="check">
Enter your name<input type="text" name="name"> <br>
<input type="submit" value="login">
</form>
</head>
<body>
</body>
</html>


Servlet Program:
//DemoReq1.java

import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class DemoReq1 extends HttpServlet
{
 public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
 {
  res.setContentType("text/html");
  PrintWriter pw=res.getWriter();
 
  String name=req.getParameter("name");//will return value
  pw.println("Namasthe  "+name);
  pw.close();
 }
}

web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
 <servlet-name>Raj</servlet-name>
 <servlet-class>DemoReq1</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>Raj</servlet-name>
 <url-pattern>/check</url-pattern>
</servlet-mapping>
</web-app>










rereree














You may like the following posts:

Tuesday 8 June 2010

What is 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-app> represents the whole application.
<servlet> is sub element of <web-app> and represents the servlet.
<servlet-name> is sub element of <servlet> represents the name of the servlet.
<servlet-class> is sub element of <servlet> represents the class of the servlet.
<servlet-mapping> is sub element of <web-app>. It is used to map the servlet.
<url-pattern> is sub element of <servlet-mapping>. This pattern is used at client side to invoke the servlet.


Monday 7 June 2010

Introduction to Servlets

There are two types of Web Resource programs:

1. Static Web resource Programs:
    It generates static web pages. Ex: html pages
2. Dynamic Web resource programs:
    It generates dynamic web pages.
    Ex:
    Servlet programs
    JSP programs
   ASP.net
   Etc.,


What is Web Application?
A Web application is a collection of web resource programs.

img file, java script file can not generate web pages directly but they act as helper programs to another web resource programs.

Web Application is a collection of static, dynamic and helper (Ex: image files) programs.



To Execute Java Apps we need JRE+JVM
To execute Applet programs we need Applet viewer
To execute Servlet programs we need Servlet Container or Web Container. Web Servers (Ex: Tomcat, WebLogic) supplies this Servlet, JSP containers.


Servlet Features:

Better performance: because it creates a thread for each request not process.
Portability: because it uses java language.
Robust: Servlets are managed by JVM so we don't need to worry about memory leak, garbage collection etc.
Secure: because it uses java language..


You may like the following posts:

Friday 4 June 2010

Prerequisite to learn Servlets


1. Download any server (Ex: Tomcat, Weblogic,..)
    How to download Tomcat Server
2. Download the jar files
    servlet-api.jar
3. JDK 7
4. Eclipse IDE for Java EE Developers (Juno 4.2)
5. import javax.servlet and javax.servlet.http packages

Table of Content:

1. Create Dynamic Web Project

2. Create Servlet class

3. Deploy the servlet

4. Run and test the servlet





How to download Tomcat Server

http://crunchify.com/step-by-step-guide-to-setup-and-install-apache-tomcat-server-in-eclipse-development-environment-ide/

For Command Prompt:
1. go https://tomcat.apache.org/download-80.cgi or

2. Go to Binary Distributions
3. Choose Core:
4. Click on 32-bit/64-bit Windows Service Installer (pgp, md5, sha1)
5. Once you download the Tomcat Server installer,click on to install

Go to Downloads in your PC


Click on apache-tomcat-..exe file

Next





HelloWorld servlet program