Sunday, 18 August 2013

Creating a servlet program

·         Creating a servlet program

Prerequisite:
1. Download any server (Ex: Tomcat, Weblogic,..)
2. Download the jar files and configure
    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





·         Servlet is a java class, which resides in Tomcat(server), and tomcat is a server
Right click on our project i.e: simpleservletproject->new->servlet




 ·         Or if you unable to find servlet option, then choose other..and  we can find servlet option in Web


 ·         Click on next button


 ·         Here give the java package: Example: org.rajendra.improvejava, and class name is SimpleServlet(you can give any class name)

Here super class is HttpServlet, and click on Next button

·         Here I can enter the name of the servlet, it’s taken the default as the class name, and give the description name example: a simple servlet program





 ·         url mapping that is class name , click on Edit option





 ·         Click on Ok button
Now click on Next button


·    
·         Enable or disable the necessary methods, in this prog we don’t want contructor, doPost()
·         Click on finish button

·      
·         Write any statements in doGet() method
·         Now go to our package , and our program as per the below figure
·         Right click->run as->choose Run on Server
If you didn’t save it will ask you, click on Yes
It deploys inside the server


·         Click on Finish button


·         If you want you can use printwiter class to display in the webpage also
Write the PrinterWriter class reference and print the
import java.io.PrintWriter;





No comments:

Post a Comment