Lecture 3.3
Lecture 3.3
DEPARTMENT : CSE
Bachelor of Engineering (Computer Science
& Engineering)
PROJECT BASED LEARNING IN JAVA WITH LAB
(22CSH-359/22ITH-359)
TOPIC OF PRESENTATION:
Configuring project using servlet, Servlet
Config and Servlet Mapping (CO 5)
DISCOVER . LEARN .
EMPOWER
Lecture Objectives
2
Creating Servlet Example in
Eclipse
3
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.
•Create a Dynamic web project
•create a servlet
•add servlet-api.jar file
•Run the servlet
1) Create the dynamic web project:
For creating a dynamic web project click on File Menu -> New -> Project..-> Web ->
dynamic web project -> write your project name e.g. first -> Finish.
https://www.javatpoint.com/creating-servlet-in-eclipse-ide
ServletConfig and ServletContext
7
ServletConfig interface
8
Demo for using ServletConfig
9
Demo for using ServletConfig (Contd.).
import java.io.*;
import javax.servlet.*;
import
javax.servlet.http.*;
public class Second
extends HttpServlet {
String homeName;
ServletConfig config;
11
Demo for using ServletConfig (Contd.).
Web.xml
<web-app>
<servlet>
<servlet-name>Second</servlet-name>
<servlet-class>Second</servlet-class>
<init-param>
<param-name>homeName</param-name>
<param-value>Welcome to www.simple.com</param-value>
</init-param>
12
Database Example for using ServletConfig
• The init() method can also be used to perform set up operation such as
setting up a database connection
<web-app>
<servlet>
<servle
t-
name>
DBCon
figPara
mServl
et</
servlet-
name>
<servle
t-
class>
DBCon
figPara
mServl
et</
servlet- 14
class>
Discussion
15
ServletContext interface
• There is one context per "web application" per Java Virtual Machine
16
Servlet Context Methods
17
Demo for using ServletContext
• Use of ServletContext for web application initialization: Suppose
there is a need to include a contact email of webmaster or an admin
on few web pages of a website
• In the web.xml:
<servlet>
<servlet-name>ContextParamServlet</servlet-name>
<servlet-class>ContextParamServlet</servlet-class>
</servlet>
<context-param>
<param-name>Email</param-name>
<param-value>[email protected]</param-value>
</context-param>
18
Checkpoint
19
Demo for using ServletContext
• Use of ServletContext for web application initialization: Suppose
there is a need to include a contact email of webmaster or an admin
on few web pages of a website
• In the web.xml:
<servlet>
<servlet-name>ContextParamServlet</servlet-name>
<servlet-class>ContextParamServlet</servlet-class>
</servlet>
<context-param>
<param-name>Email</param-name>
<param-value>[email protected]</param-value>
</context-param>
20
Servlet Chaining
21
Servlet Chaining: RequestDispatcher Interface
1. RequestDispatcher.forward(request,response)
2. RequestDispatcher.include(request,response)
22
Servlet Chaining: forward (request,response)
ServletContext ctx=getServletContext();
RequestDispatcher
dis=ctx.getRequestDispatcher(“/servlet/AnotherServl
et”);
dis.forward(request,response);
23
Servlet Chaining: forward (request, response)
Response Generation
24
Servlet Chaining: include (request, response)
ServletContext ctx=getServletContext();
RequestDispatcher
dis=ctx.getRequestDispatcher(“/servlet/AnotherServl
et”);
dis.include(request,response);
25
Servlet Chaining: include (request, response)
26
Demo for Servlet Chaining
• SecondServlet
– SecondServlet.java - Extracts the username value which is set in FirstServlet
27
Summary:
Video Lectures :
https://youtu.be/ewiOaDitBBw
Reference Links:
1. http://docs.oracle.com/cd/B31017_01/web.1013/b28959/sess ions.htm
2. http://www.tutorialspoint.com/jsp/jsp_cookies_handling.htm
3. http://www.javatpoint.com/sonoojaiswal/servletconfig
4. https://www.javatpoint.com/creating-servlet-in-eclipse-ide
THANK YOU