Professional Documents
Culture Documents
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:
1 Environment Used
3 Project Description
5 Creating Servlet
6.1 Method 1:
6.2 Method 2
Environment Used
JDK 6 (Java SE 6)
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.
Right click on Project Explorer -> 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
Click on Finish
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
}
}
we
need
to
deploy
the
Servlets
project
FirstServlet
on
server.
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.
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
et us discuss one simple login application using servlet and jsp, friends please refer previous articles if
Directory Structure
Files Required
OnServletLogin.java
index.html
web.xml
index.html
web.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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