hai friend.. grand welcome to you......happy x-mas........ visit my web site

Saturday, January 10, 2009

How to write the Servlet program?


How to write the Servlet program?

here is the simple steps to write the servlet program .just follow it .

step 1:

create a folder or directory and give any name…generally we name the directory or folder name as application name.

here I consider my application name as helloSevlet. Hence I name my directory or folder name as helloSevlet.

Step 2:

Create three more directory (directory or folder both are same) in parent directory helloSevlet. Name them as

i)src

ii) resources

iii)WEB-INF

note that the name of the directory WEB-INF must be in capital I have shown.

The directory h ierarchy as viewed as follows.

Some important points to remember:

We generally put all .java files in src directory which are considered as source files.

We generally put all html ,jsp, and any supported files in resources directory.

WEB-INF directory is maily gives the web information which is discussed in next step.

Step 3:

Now consider the WEB-INF directory.

Create one directory and name it as classes .

This classes directory will have all .classes files which we got from the .java files when they compiled in the src .

The WEB-INF directory also consists of one XML file called web.xml which is called as deployment descriptor or configuration file.

Now the total directory hirerchey is as follows

Code:

Login.html.(this login.html file is placed in resources folder).

New Document

New Document

method="GET" >

username

password




Code:

Login.java(this login.java file is placed in src directory)

import javax.servlet.http.*;

import javax.servlet.*;

import java.io.*;

public class login extends HttpServlet

{

public void doGet(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException

{

res.setContentType("text/html");

PrintWriter out=res.getWriter();

String s=req.getParameter("username");

String s1=req.getParameter("pwd");

out.println("u r name is:"+s);

out.println("u r password is:"+s1);

}

}

Code:

Web.xml(this web.xml file is placed in WEB-INF)

login

login

login

/login

resources/login.html

Compilation:

I placed my application directory in c: drive.the compilation is as follows.

C:\helloServlet\src>javac login.java –d ../web-inf\classes (enter)

the above command will produce the login.class file and it is placed in classes directory of WEB-INF directory.

Creating war file:

To create war file the following command should be followed.

C:\helloServlet>jar -cfv helloServlet.war . (enter)

Final step:

Copy the helloServlet.war file from the helloServlet directory and place it in webapp directory of your tomcat server.

Then run the tomcat server. Open IE browser and type the url as followed in the address bar.

http://localhost:8080/helloServlet/... It will run ………..

No comments:

Post a Comment

 
have a good time.................