JAVA SERVLETS Part B
JAVA SERVLETS Part B
Eclipse is an open-source ide for developing JavaSE and JavaEE (J2EE) applications. You can
download the eclipse ide from the eclipse website http://www.eclipse.org/downloads/.
Creating servlet example in eclipse ide, saves a lot of work to be done. It is easy and simple to
create a servlet example. Let's see the steps, you need to follow to create the first servlet
example.
For creating a dynamic web project click on File Menu -> New -> Project..-> Web -> dynamic
web project -> write your project name e.g. first -> Finish.
Page 1 of 61
Page 2 of 61
Page 3 of 61
2) Create the servlet in eclipse IDE:
For creating a servlet, explore the project by clicking the + icon -> explore the Java
Resources -> right click on src -> New -> servlet -> write your servlet name e.g. Hello ->
uncheck all the checkboxes except doGet() -> next -> Finish.
Page 4 of 61
Page 5 of 61
Page 6 of 61
3) add jar file in eclipse IDE:
Page 7 of 61
For adding a jar file, right click on your project -> Build Path -> Configure Build Path ->
click on Libraries tab in Java Build Path -> click on Add External JARs button -> select
the servlet-api.jar file under tomcat/lib -> ok.
Page 8 of 61
Page 9 of 61
Now servlet has been created, Let's write the first servlet code.
Page 10 of 61
4) Start the server and deploy the project:
For starting the server and deploying the project in one step, Right click on your project ->
Run As -> Run on Server -> choose tomcat server -> next -> addAll -> finish.
Page 11 of 61
Page 12 of 61
Now tomcat server has been started and project is deployed. To access the servlet write the url
pattern name in the URL bar of the browser. In this case Hello then enter.
Page 13 of 61
How to configure tomcat server in Eclipse ? (One time Requirement)
If you are using Eclipse IDE first time, you need to configure the tomcat server First.
For configuring the tomcat server in eclipse IDE, click on servers tab at the bottom side of the
IDE -> right click on blank area -> New -> Servers -> choose tomcat then its version ->
next -> click on Browse button -> select the apache tomcat root folder previous to bin ->
next -> addAll -> Finish.
Page 14 of 61
Page 15 of 61
Page 16 of 61
Page 17 of 61
Now tomcat7 server has been configured in eclipse IDE.
Page 18 of 61
Page 19 of 61
Creating Servlet in myeclipse IDE
1. Creating Servlet in myeclipse IDE
1. Create a web project
2. create a html file
3. create a servlet
4. start myeclipse tomcat server and deploy project
You need to follow the following steps to create the servlet in the myeclipse IDE. The steps are as
follows:
Page 20 of 61
Page 21 of 61
Page 22 of 61
Page 23 of 61
2) Create the html file:
As you can see that a project is created named first. Now let's explore this project.
Page 24 of 61
Page 25 of 61
For creating a html file, right click on WebRoot -> New -> html -> write your html file name e.g.
MyHtml.html -> Finish.
Page 26 of 61
As you can see that a html file is created named MyHtml.html. Now let's write the html code here.
Page 27 of 61
3) Create the servlet:
For creating a servlet click on File Menu -> New -> servlet -> write your servlet name e.g. Hello ->
uncheck all the checkboxes except doGet() -> next -> Finish.
Page 28 of 61
As you can see that a servlet file is created named Hello.java. Now let's write the servlet code here.
Page 29 of 61
Page 30 of 61
Now let's make the MyHtml.html file as the default page of our project. For this, open web.xml file and
change the welcome file name as MyHtml.html in place of index.jsp.
Page 31 of 61
Click on the source tab to see the source code.
Page 32 of 61
Now change the welcome file as MyHtml.html in place of index.jsp.
Page 33 of 61
Page 34 of 61
4) Start the server and deploy the project:
For starting the server and deploying the project in one step Right click on your project -> Run As ->
MyEclipse server application.
The default port of myeclipse tomcat is 8080, if you have installed oracle on your system, the port no.
will conflict so let's first change the port number of myeclipse tomcat server. For changing the port
number click on the start server icon at the left hand side of browser icon -> myeclipse tomcat ->
Configure server connector -> change the port number as 8888 in place of 8080 -> apply -> ok.
Page 35 of 61
Page 36 of 61
Now change the port number as 8888 in place of 8080 -> apply -> ok.
Page 37 of 61
Page 38 of 61
Now port number have been changed. For starting the server Right click on your project -> Run As ->
MyEclipse server application.
As you can see that default page of your project is open, write your name -> go.
Page 39 of 61
Page 40 of 61
Page 41 of 61
Creating a servlet in NetBeans IDE:
Page 42 of 61
Page 43 of 61
Page 44 of 61
Page 45 of 61
Page 46 of 61
Page 47 of 61
Page 48 of 61
Page 49 of 61
Page 50 of 61
Page 51 of 61
Page 52 of 61
ServletRequest Interface
1. ServletRequest Interface
2. Methods of ServletRequest interface
3. Example of ServletRequest interface
4. Displaying all the header information
An object of ServletRequest is used to provide the client request information to a servlet such as
content type, content length, parameter names and values, header information, attributes etc.
Page 53 of 61
Methods of ServletRequest interface
There are many methods defined in the ServletRequest interface. Some of them are as follows:
Method Description
public String getParameter(String name) is used to obtain the value of a parameter by name.
public String[] returns an array of String containing all values of given
getParameterValues(String name) parameter name. It is mainly used to obtain values of a
Multi select list box.
java.util.Enumeration returns an enumeration of all of the request parameter
getParameterNames() names.
public int getContentLength() Returns the size of the request entity data, or -1 if not
known.
public String getCharacterEncoding() Returns the character set encoding for the input of this
request.
public String getContentType() Returns the Internet Media Type of the request entity
data, or null if not known.
public ServletInputStream Returns an input stream for reading binary data in the
getInputStream() throws IOException request body.
public abstract String getServerName() Returns the host name of the server that received the
request.
public int getServerPort() Returns the port number on which this request was
received.
Example of ServletRequest to display the name of the user
In this example, we are displaying the name of the user in the servlet. For this purpose, we have
used the getParameter method that returns the value for the given request parameter name.
index.html
DemoServ.java
1. import javax.servlet.http.*;
2. import javax.servlet.*;
3. import java.io.*;
4. public class DemoServ extends HttpServlet{
5. public void doGet(HttpServletRequest req,HttpServletResponse res)
6. throws ServletException,IOException
7. {
Page 54 of 61
8. res.setContentType("text/html");
9. PrintWriter pw=res.getWriter();
10.
11. String name=req.getParameter("name");//will return value
12. pw.println("Welcome "+name);
13.
14. pw.close();
15. }}
RequestDispatcher in Servlet
1. RequestDispatcher Interface
2. Methods of RequestDispatcher interface
1. forward method
2. include method
3. How to get the object of RequestDispatcher
4. Example of RequestDispatcher interface
The RequestDispatcher interface provides the facility of dispatching the request to another
resource it may be html, servlet or jsp. This interface can also be used to include the content of
another resource also. It is one of the way of servlet collaboration.
Page 55 of 61
As you see in the above figure, response of second servlet is sent to the client. Response of the
first servlet is not displayed to the user.
As you can see in the above figure, response of second servlet is included in the response of the first
servlet that is being sent to the client.
Page 56 of 61
Example of using getRequestDispatcher method
1. RequestDispatcher rd=request.getRequestDispatcher("servlet2");
2. //servlet2 is the url-pattern of the second servlet
3. rd.forward(request, response);//method may be include or forward
In this example, we are validating the password entered by the user. If password is servlet, it will
forward the request to the WelcomeServlet, otherwise will show an error message: sorry
username or password error!. In this program, we are checking for hardcoded information. But
you can check it to the database also that we will see in the development chapter. In this
example, we have created following files:
index.html
Login.java
1. import java.io.*;
2. import javax.servlet.*;
3. import javax.servlet.http.*;
4.
Page 57 of 61
5.
6. public class Login extends HttpServlet {
7.
8. public void doPost(HttpServletRequest request, HttpServletResponse response)
9. throws ServletException, IOException {
10.
11. response.setContentType("text/html");
12. PrintWriter out = response.getWriter();
13.
14. String n=request.getParameter("userName");
15. String p=request.getParameter("userPass");
16.
17. if(p.equals("servlet"){
18. RequestDispatcher rd=request.getRequestDispatcher("servlet2");
19. rd.forward(request, response);
20. }
21. else{
22. out.print("Sorry UserName or Password Error!");
23. RequestDispatcher rd=request.getRequestDispatcher("/index.html");
24. rd.include(request, response);
25.
26. }
27. }
28.
29. }
WelcomeServlet.java
1. import java.io.*;
2. import javax.servlet.*;
3. import javax.servlet.http.*;
4.
5. public class WelcomeServlet extends HttpServlet {
6.
7. public void doPost(HttpServletRequest request, HttpServletResponse response)
8. throws ServletException, IOException {
9.
10. response.setContentType("text/html");
11. PrintWriter out = response.getWriter();
12.
13. String n=request.getParameter("userName");
14. out.print("Welcome "+n);
15. }
16.
17. }
web.xml
Page 58 of 61
1. <web-app>
2. <servlet>
3. <servlet-name>Login</servlet-name>
4. <servlet-class>Login</servlet-class>
5. </servlet>
6. <servlet>
7. <servlet-name>WelcomeServlet</servlet-name>
8. <servlet-class>WelcomeServlet</servlet-class>
9. </servlet>
10.
11.
12. <servlet-mapping>
13. <servlet-name>Login</servlet-name>
14. <url-pattern>/servlet1</url-pattern>
15. </servlet-mapping>
16. <servlet-mapping>
17. <servlet-name>WelcomeServlet</servlet-name>
18. <url-pattern>/servlet2</url-pattern>
19. </servlet-mapping>
20.
21. <welcome-file-list>
22. <welcome-file>index.html</welcome-file>
23. </welcome-file-list>
24. </web-app>
Page 59 of 61
Page 60 of 61
Page 61 of 61