Sunday, August 23, 2009

Servlet Example(free Source codes )

# Writing MyFirstServlet.java file

//Import needed Java packages
package MyFirstServlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

/**Example servlet showing First Hello World Servlet
*@author Nadishan Mayura
*/
// Create a class which inherits from GenericServlet or HttpServlet.
public class MyFirstServlet extends HttpServlet{

/**
* Override the service method.
* Here we handle the HTTP GET method by building a simple web page.
*/

public void doGet(HttpServletRequest req,HttpServletResponse res)throws
ServletException,IOException{
PrintWriter out=res.getWriter();// write the data of the response
out.println("Hello World!!!!!!!");
java.util.Date today=new java.util.Date();
out.println("The date & Time of Today is:"+today);
}
}

# Writing web.xml file

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">



<servlet>
<servlet-name>myfirstservlet</servlet-name>
<servlet-class>MyFirstServlet.MyFirstServlet</servlet-class>
</servlet>

<!-- servlet mappings start -->
<servlet-mapping>
<servlet-name>myfirstservlet </servlet-name >
<url-pattern>/hello </url-pattern>
</servlet-mapping>
<!-- servlet mappings end -->

</web-app>

No comments:

Post a Comment

Followers

Search