You are on page 1of 10

Eclipse sample coding:

1. Helloworld:
import
import
import
import
import
import

java.io.IOException;
java.io.PrintWriter;
javax.servlet.ServletException;
javax.servlet.http.HttpServlet;
javax.servlet.http.HttpServletRequest;
javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class HelloWorldServlet
*/
public class HelloWorldServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public HelloWorldServlet() {
super();
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("Hello World");
}
}

2. Hello world:

HOW TO CREATE A SERVLET WITH ECLIPSE


AND TOMCAT
7 May 2012 By Nithya Vasudevan 117,709 views 32 Comments

1 Environment Used

2 Setting up development environment

3 Project Description

4 Creating Dynamic Web Project

5 Creating Servlet

6 Run the Servlet


o

6.1 Method 1:

6.2 Method 2

Environment Used

JDK 6 (Java SE 6)

Eclipse Indigo IDE for Java EE Developers (3.7.1)

Apache Tomcat 6.x

Java EE 5 API (Servlet 2.5)

Setting up development environment


To install and configure Apache Tomcat in Eclipse IDE read this page .

Project Description

This example explains how to develop, deploy and run Servlet in Tomcat using Eclipse IDE.

We use Eclipse IDE for Java EE Developers which includes tools for creating Java EE and Web
applications.

Creating Dynamic Web Project


To create a Servlet we need to create a new Dynamic Web project which can be done in three ways,

Right click on Project Explorer -> New -> Dynamic Web Project

File menu -> New -> Dynamic Web Project

Click on the down arrow on New icon on toolbar -> Dynamic Web Project

Enter the project name as FirstServlet and make sure the Apache Tomcat v6.0 Target Runtime has been
selected with the Dynamic web module version as 2.5 and click on Finish button.

You will see the Dynamic web project in the Project Explorer view.

Creating Servlet

Right click on src or Project -> New -> Servlet

Enter the Java package name as com.theopentutorials.servlets

Enter the Class name as HelloWorldServlet

Click on Finish

In the doGet() method of your Servlet copy the following code.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20

package com.theopentutorials.servlets;
import
import
import
import
import
import

java.io.IOException;
java.io.PrintWriter;
javax.servlet.ServletException;
javax.servlet.http.HttpServlet;
javax.servlet.http.HttpServletRequest;
javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class HelloWorldServlet
*/
public class HelloWorldServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public HelloWorldServlet() {
super();
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("Hello World");

21
22
23
24
25
26

}
}

Run the Servlet


Method 1:
Deploying Dynamic Web Project
Now

we

need

to

deploy

the

Servlets

project

FirstServlet

on

server.

Deploying the project can be done in two ways,

Right click on the FirstServlet Dynamic Web project -> Run As -> Run On Server. Select the existing
Tomcat v6.0 Server at localhost and click Finish.

Right click on Tomcat v6.0 Server at localhost available in Servers view -> Add and Remove ->

Select the Servlets JAR file from the left pane and click Add-> and then Finish.
Start/Restart the Server
Right click on Tomcat v6.0 Server at localhost from Servers view and click on Start if it has not yet been
started.
Open any browser or Eclipse internal Web Browser and type the following URL for requesting Servlet
http://localhost:8080/FirstServlet/HelloWorldServlet

Method 2
This is simple and easy method and combines the three steps (deploying project, starting server and
requesting

Servlet)

of

method

in

single

step.

You can run the Servlet in many ways,

Use Ctrl + F11 to run the Servlet and select the existing Server and Finish.

Right click on editor -> Run As-> Run on Server and select the existing Server and Finish.

Right click on Servlet file in Project Explorer -> Run As-> Run on Server and select the existing Server
and Finish.

Eclipse

Internal

Web

Browser

will

display

the

following

output

3. Login with servlet:

Java Servlet login Example In Eclipse


Servlets On Jan 19, 2013 By Sivateja

et us discuss one simple login application using servlet and jsp, friends please refer previous articles if

you still have any doubts regarding strvlets flow

Directory Structure

Files Required

OnServletLogin.java

index.html

web.xml

index.html

1 <form action="login" method="post">


2 <table>
3 <tr>
4
<td><font face="verdana" size="2px">Name:</font></td>
5
<td><input type="text" name="userName"></td>
6 </tr>
7 <tr>
8
<td><font face="verdana" size="2px">Password:</font></td>
9
<td><input type="password" name="userPassword"></td>
10 </tr>
11 </table>
12
<input type="submit" value="Login">
13 </form>

web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

<?xml version="1.0" encoding="UTF-8"?>


<web-app xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>java4s.OnServletLogin</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>

OnServletLogin.java

1
2
3
4
5
6
7
8
9
10
11
12
13

package java4s;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class OnServletLogin extends HttpServlet
{
protected void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException

14 {
15
PrintWriter pw=res.getWriter();
16
res.setContentType("text/html");
17
18
String user=req.getParameter("userName");
19
String pass=req.getParameter("userPassword");
20
21
if(user.equals("java4s")&&pass.equals("java4s"))
22
pw.println("Login Success...!");
23
else
24
pw.println("Login Failed...!");
25
pw.close();
26
27 }
28
29 }

Output

You might also like