SlideShare a Scribd company logo


Servlets are part of the Java2EE specification.
 Servlets are modules that run on the server,
enabling you to extend the server’s functionality.
 Servlets work within a Web server environment,
and they are a key component of server side
 Java development. Servlets are an effective
replacement for CGI scripts.


Can be deployed into distributed server
environments.
 Servlets are platform- and server-independent.
 Servlets are easy to develop and follow a
standard API.
 Servlets are extensible. Java Server Pages (JSP)
build on top of the Servlet API.
 A servlet is a server resource, providing access to
other server resources, such as other servlets,
EJBs, JSPs, JDBC, and so on.
Method

Description

GET

The client requests information from the given
URL.

HEAD

Similar to GET, except the body is not retrieved.

POST

Client adds info to URI (HTML forms)

PUT

Used to place documents on the Server.

DELETE

Client deletes resource of URI.
Status

Code Category

100s
200s
300s
400s
500s

Informational
Successful
Redirection
Request Error
Server Error




The Servlet API defines a standard interface for
handling request and response between the
browser and the Web server.
The Servlet API is composed of two packages:
 javax.servlet - javax.servlet.GenericServlet

 javax.servlet.http - javax.servlet.HttpServlet
A generic servlet handling a request
An HTTP servlet handling GET and POST requests
Can use ServletOutputStream or PrintWriter to send data
back to the client.
1. reference the stream from the Response parameter:
ServletOutputStream out =response.getOutputStream();
2. get a reference to the writer from the Response
parameter:
PrintWriter out = response.getWriter();
3. Then write the output to the stream:
out.println(“<HTML>Inside HTML</HTML>”);
4. Finally, close the writer:
out.close();



MIME – Multiple Internet Mail Extension
Identifies extension of each file in the
HTTPResponse
response.setContentType(“text/html”);
PrintWriter out = response.getWriter();
import java.io.*; import javax.servlet.*; import javax.servlet.http.*;
public class srvltJust extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse
res) throws ServletException, IOException {
res.setContentType(“text/html”);
PrintWriter out = res.getWriter();
out.println(“<HTML>”);
out.println(“<HEAD><TITLE>Servlet</TITLE></HEAD>”);
out.println(“<BODY>”);
out.println(“<H1>This is a just a Servlet!</H1>”);
out.println(“</BODY></HTML>”);
out.close();
Parameters
 public String getParameter(String name)
 public Enumeration getParameterNames()
 public String[] getParameterValues(String name)
Content
 public int getContentLength() - returns the length, in
bytes. -1 is returned if the length is not known.
 getContentType() - returns the request’s MIME type
of the content (null if it’s not known).
 getCharacterEncoding() - returns the name character
encoding style of the request.
Header Methods
 setDateHeader()
 setIntHeader()
 setContentType()
 setContentLength()
 addCookie()
 sendError()
Ways to manage session,
 Hidden form fields
 URL rewriting
 Persistent cookies
 Session tracking API
<input type=”hidden” name=”pageid” value=”5”>


public String getParameter(String name)
 public Enumeration getParameterNames()
 public String[] getParameterValues(String name)
http://myServer/servlets/srvltEmp?EMPID=1009&
DEPID=200
API for persistent cookie is,
javax.servlet.http.Cookie
To create a new cookie,
 Cookie cookie(String name, String value)

 Eg: Cookie cookie = new Cookie("ID", "123");

To get all available cookies,
 req.getCookies()

To send back the cookie name
 response.addCookie(cookie_name)


A servlet with getSession( ) method retrieves the current
HttpSession object
Eg: public HttpSession HttpServletRequest.getSession(boolean )



Set properties by,
public void HttpSession.setAttribute(String name, Object value)
Eg: session.setAttribute(“name”, id);



Get properties by,
public void HttpSession.setMaxInactiveInterval(int secs)


Get current session id by,
public String getId()
 Whether it is a new cookie or referenced,
public boolean isNew
 Start of a session
public long getCreationTime()
 Last session activity
public long getLastAccessedTime
 Session invalidating by,
public void invalidate()
 Removing attribute by,
public void removeAttribute(String name)
import java.io.*; import java.net.*; import java.util.*; import javax.servlet.*;
import javax.servlet.http.*;
public class srvltHoldAppID extends HttpServlet
{public void service(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
resp.setContentType(“text/html”);
PrintWriter out = res.getWriter();
String sAPPID` = “undefined”;
String[] sAPPID = req.getParameter(“APPID”);
if(sAPPID != null && sAPPID.length > 0) {
// Create session:
HttpSession session = req.getSession();
session.setAttribute(“APPID”, sAPPID);
}}}
ServletContext sc = this.getServletContext();
RequestDispatcher rd =
sc.getRequestDispatcher(“/srvltComplete”);
if (rd !=null) {
try {
rd.forward(req, res);
}
catch (Exception e) {
// Handle Exception
}
}
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String sSQL = “……….”
InitialContext ic = new InitialContext()
//Get a reference to the datasource
String dsName = “java:comp/env/jdbc/emplphone”
DataSource ds = (DataSource) ic.lookup(dsName)
conn = ds.getConnection() // Get a Connection
stmt = conn.createStatement()
rs = stmt.executeQuery(sSQL)
while( rs.next())
{out.println(rs.getString(1))}

More Related Content

What's hot

jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
Arulmurugan Rajaraman
 
Servlets
ServletsServlets
IIS
IISIIS
Java servlet life cycle - methods ppt
Java servlet life cycle - methods pptJava servlet life cycle - methods ppt
Java servlet life cycle - methods ppt
kamal kotecha
 
Lab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptLab #2: Introduction to Javascript
Lab #2: Introduction to Javascript
Walid Ashraf
 
Bootstrap
BootstrapBootstrap
Bootstrap
Jadson Santos
 
Introduction to java beans
Introduction to java beansIntroduction to java beans
Introduction to java beans
Hitesh Parmar
 
jQuery
jQueryjQuery
Servlet.ppt
Servlet.pptServlet.ppt
Servlet.ppt
VMahesh5
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
Kasun Madusanke
 
Apache tomcat
Apache tomcatApache tomcat
Apache tomcat
Shashwat Shriparv
 
Servlets
ServletsServlets
Servlets
ZainabNoorGul
 
Servlet life cycle
Servlet life cycleServlet life cycle
Servlet life cycle
Venkateswara Rao N
 
Servlet
Servlet Servlet
Servlet
Dhara Joshi
 
ASP.NET MVC Presentation
ASP.NET MVC PresentationASP.NET MVC Presentation
ASP.NET MVC Presentation
ivpol
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jspJafar Nesargi
 
Java Spring Framework
Java Spring FrameworkJava Spring Framework
Java Spring Framework
Mehul Jariwala
 

What's hot (20)

jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
Servlets
ServletsServlets
Servlets
 
IIS
IISIIS
IIS
 
Java servlet life cycle - methods ppt
Java servlet life cycle - methods pptJava servlet life cycle - methods ppt
Java servlet life cycle - methods ppt
 
Lab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptLab #2: Introduction to Javascript
Lab #2: Introduction to Javascript
 
Bootstrap
BootstrapBootstrap
Bootstrap
 
Java script
Java scriptJava script
Java script
 
Introduction to java beans
Introduction to java beansIntroduction to java beans
Introduction to java beans
 
Tomcat
TomcatTomcat
Tomcat
 
jQuery
jQueryjQuery
jQuery
 
Web forms in ASP.net
Web forms in ASP.netWeb forms in ASP.net
Web forms in ASP.net
 
Servlet.ppt
Servlet.pptServlet.ppt
Servlet.ppt
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
Apache tomcat
Apache tomcatApache tomcat
Apache tomcat
 
Servlets
ServletsServlets
Servlets
 
Servlet life cycle
Servlet life cycleServlet life cycle
Servlet life cycle
 
Servlet
Servlet Servlet
Servlet
 
ASP.NET MVC Presentation
ASP.NET MVC PresentationASP.NET MVC Presentation
ASP.NET MVC Presentation
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jsp
 
Java Spring Framework
Java Spring FrameworkJava Spring Framework
Java Spring Framework
 

Viewers also liked

Introduction to JSP
Introduction to JSPIntroduction to JSP
Introduction to JSPGeethu Mohan
 
Applet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java AppletsApplet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java Applets
amitksaha
 
Applet java
Applet javaApplet java
Applet java
Jorge Luis Tinoco
 
Java applets
Java appletsJava applets
Java appletslopjuan
 
Applet life cycle
Applet life cycleApplet life cycle
Applet life cyclemyrajendra
 
Data warehouse concepts
Data warehouse conceptsData warehouse concepts
Data warehouse concepts
obieefans
 
Java applets
Java appletsJava applets
Java Servlets
Java ServletsJava Servlets
Java ServletsNitin Pai
 
DATA WAREHOUSING
DATA WAREHOUSINGDATA WAREHOUSING
DATA WAREHOUSING
Rishikese MR
 
DATA WAREHOUSING
DATA WAREHOUSINGDATA WAREHOUSING
DATA WAREHOUSING
King Julian
 
Data warehouse architecture
Data warehouse architectureData warehouse architecture
Data warehouse architecturepcherukumalla
 
DATA WAREHOUSING AND DATA MINING
DATA WAREHOUSING AND DATA MININGDATA WAREHOUSING AND DATA MINING
DATA WAREHOUSING AND DATA MINING
Lovely Professional University
 
Introduction to Data Warehousing
Introduction to Data WarehousingIntroduction to Data Warehousing
Introduction to Data Warehousing
Jason S
 
Data Warehousing and Data Mining
Data Warehousing and Data MiningData Warehousing and Data Mining
Data Warehousing and Data Mining
idnats
 
Subversion - buenas prácticas
Subversion - buenas prácticasSubversion - buenas prácticas
Subversion - buenas prácticas
Iker Canarias
 
Introducción a Tomcat
Introducción a TomcatIntroducción a Tomcat
Introducción a Tomcat
Iker Canarias
 

Viewers also liked (16)

Introduction to JSP
Introduction to JSPIntroduction to JSP
Introduction to JSP
 
Applet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java AppletsApplet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java Applets
 
Applet java
Applet javaApplet java
Applet java
 
Java applets
Java appletsJava applets
Java applets
 
Applet life cycle
Applet life cycleApplet life cycle
Applet life cycle
 
Data warehouse concepts
Data warehouse conceptsData warehouse concepts
Data warehouse concepts
 
Java applets
Java appletsJava applets
Java applets
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
DATA WAREHOUSING
DATA WAREHOUSINGDATA WAREHOUSING
DATA WAREHOUSING
 
DATA WAREHOUSING
DATA WAREHOUSINGDATA WAREHOUSING
DATA WAREHOUSING
 
Data warehouse architecture
Data warehouse architectureData warehouse architecture
Data warehouse architecture
 
DATA WAREHOUSING AND DATA MINING
DATA WAREHOUSING AND DATA MININGDATA WAREHOUSING AND DATA MINING
DATA WAREHOUSING AND DATA MINING
 
Introduction to Data Warehousing
Introduction to Data WarehousingIntroduction to Data Warehousing
Introduction to Data Warehousing
 
Data Warehousing and Data Mining
Data Warehousing and Data MiningData Warehousing and Data Mining
Data Warehousing and Data Mining
 
Subversion - buenas prácticas
Subversion - buenas prácticasSubversion - buenas prácticas
Subversion - buenas prácticas
 
Introducción a Tomcat
Introducción a TomcatIntroducción a Tomcat
Introducción a Tomcat
 

Similar to Servlets

Java Web Programming [2/9] : Servlet Basic
Java Web Programming [2/9] : Servlet BasicJava Web Programming [2/9] : Servlet Basic
Java Web Programming [2/9] : Servlet Basic
IMC Institute
 
Http Server Programming in JAVA - Handling http requests and responses
Http Server Programming in JAVA - Handling http requests and responsesHttp Server Programming in JAVA - Handling http requests and responses
Http Server Programming in JAVA - Handling http requests and responses
bharathiv53
 
Jsp/Servlet
Jsp/ServletJsp/Servlet
Jsp/Servlet
Sunil OS
 
Request dispacther interface ppt
Request dispacther interface pptRequest dispacther interface ppt
Request dispacther interface ppt
Taha Malampatti
 
Bt0083 server side programing
Bt0083 server side programing Bt0083 server side programing
Bt0083 server side programing
Techglyphs
 
Web Technologies - forms and actions
Web Technologies -  forms and actionsWeb Technologies -  forms and actions
Web Technologies - forms and actionsAren Zomorodian
 
J2EE : Java servlet and its types, environment
J2EE : Java servlet and its types, environmentJ2EE : Java servlet and its types, environment
J2EE : Java servlet and its types, environment
joearunraja2
 
Web Technologies -- Servlets 4 unit slides
Web Technologies -- Servlets   4 unit slidesWeb Technologies -- Servlets   4 unit slides
Web Technologies -- Servlets 4 unit slides
Sasidhar Kothuru
 
1 java servlets and jsp
1   java servlets and jsp1   java servlets and jsp
1 java servlets and jsp
Ankit Minocha
 
Advance Java Practical file
Advance Java Practical fileAdvance Java Practical file
Advance Java Practical file
varun arora
 
GWT Web Socket and data serialization
GWT Web Socket and data serializationGWT Web Socket and data serialization
GWT Web Socket and data serialization
GWTcon
 
Ajax
AjaxAjax
Ajax
Yoga Raja
 
Java Servlets.pdf
Java Servlets.pdfJava Servlets.pdf
Java Servlets.pdf
Arumugam90
 
HTTP Whiteboard - OSGI Compendium 6.0 - How web apps should have been! - R Auge
HTTP Whiteboard - OSGI Compendium 6.0 - How web apps should have been! - R AugeHTTP Whiteboard - OSGI Compendium 6.0 - How web apps should have been! - R Auge
HTTP Whiteboard - OSGI Compendium 6.0 - How web apps should have been! - R Auge
mfrancis
 
Java servlets
Java servletsJava servlets
Java servletslopjuan
 
RESTEasy
RESTEasyRESTEasy

Similar to Servlets (20)

Java Web Programming [2/9] : Servlet Basic
Java Web Programming [2/9] : Servlet BasicJava Web Programming [2/9] : Servlet Basic
Java Web Programming [2/9] : Servlet Basic
 
Servlets intro
Servlets introServlets intro
Servlets intro
 
Http Server Programming in JAVA - Handling http requests and responses
Http Server Programming in JAVA - Handling http requests and responsesHttp Server Programming in JAVA - Handling http requests and responses
Http Server Programming in JAVA - Handling http requests and responses
 
Jsp/Servlet
Jsp/ServletJsp/Servlet
Jsp/Servlet
 
Request dispacther interface ppt
Request dispacther interface pptRequest dispacther interface ppt
Request dispacther interface ppt
 
Servlets
ServletsServlets
Servlets
 
Bt0083 server side programing
Bt0083 server side programing Bt0083 server side programing
Bt0083 server side programing
 
Web Technologies - forms and actions
Web Technologies -  forms and actionsWeb Technologies -  forms and actions
Web Technologies - forms and actions
 
J2EE : Java servlet and its types, environment
J2EE : Java servlet and its types, environmentJ2EE : Java servlet and its types, environment
J2EE : Java servlet and its types, environment
 
Web Technologies -- Servlets 4 unit slides
Web Technologies -- Servlets   4 unit slidesWeb Technologies -- Servlets   4 unit slides
Web Technologies -- Servlets 4 unit slides
 
1 java servlets and jsp
1   java servlets and jsp1   java servlets and jsp
1 java servlets and jsp
 
Advance Java Practical file
Advance Java Practical fileAdvance Java Practical file
Advance Java Practical file
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
GWT Web Socket and data serialization
GWT Web Socket and data serializationGWT Web Socket and data serialization
GWT Web Socket and data serialization
 
Ajax
AjaxAjax
Ajax
 
servlets
servletsservlets
servlets
 
Java Servlets.pdf
Java Servlets.pdfJava Servlets.pdf
Java Servlets.pdf
 
HTTP Whiteboard - OSGI Compendium 6.0 - How web apps should have been! - R Auge
HTTP Whiteboard - OSGI Compendium 6.0 - How web apps should have been! - R AugeHTTP Whiteboard - OSGI Compendium 6.0 - How web apps should have been! - R Auge
HTTP Whiteboard - OSGI Compendium 6.0 - How web apps should have been! - R Auge
 
Java servlets
Java servletsJava servlets
Java servlets
 
RESTEasy
RESTEasyRESTEasy
RESTEasy
 

Recently uploaded

CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 

Recently uploaded (20)

CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 

Servlets

  • 1.
  • 2.  Servlets are part of the Java2EE specification.  Servlets are modules that run on the server, enabling you to extend the server’s functionality.  Servlets work within a Web server environment, and they are a key component of server side  Java development. Servlets are an effective replacement for CGI scripts.
  • 3.  Can be deployed into distributed server environments.  Servlets are platform- and server-independent.  Servlets are easy to develop and follow a standard API.  Servlets are extensible. Java Server Pages (JSP) build on top of the Servlet API.  A servlet is a server resource, providing access to other server resources, such as other servlets, EJBs, JSPs, JDBC, and so on.
  • 4. Method Description GET The client requests information from the given URL. HEAD Similar to GET, except the body is not retrieved. POST Client adds info to URI (HTML forms) PUT Used to place documents on the Server. DELETE Client deletes resource of URI.
  • 6.   The Servlet API defines a standard interface for handling request and response between the browser and the Web server. The Servlet API is composed of two packages:  javax.servlet - javax.servlet.GenericServlet  javax.servlet.http - javax.servlet.HttpServlet
  • 7. A generic servlet handling a request
  • 8. An HTTP servlet handling GET and POST requests
  • 9. Can use ServletOutputStream or PrintWriter to send data back to the client. 1. reference the stream from the Response parameter: ServletOutputStream out =response.getOutputStream(); 2. get a reference to the writer from the Response parameter: PrintWriter out = response.getWriter(); 3. Then write the output to the stream: out.println(“<HTML>Inside HTML</HTML>”); 4. Finally, close the writer: out.close();
  • 10.   MIME – Multiple Internet Mail Extension Identifies extension of each file in the HTTPResponse response.setContentType(“text/html”); PrintWriter out = response.getWriter();
  • 11. import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class srvltJust extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType(“text/html”); PrintWriter out = res.getWriter(); out.println(“<HTML>”); out.println(“<HEAD><TITLE>Servlet</TITLE></HEAD>”); out.println(“<BODY>”); out.println(“<H1>This is a just a Servlet!</H1>”); out.println(“</BODY></HTML>”); out.close();
  • 12.
  • 13.
  • 14. Parameters  public String getParameter(String name)  public Enumeration getParameterNames()  public String[] getParameterValues(String name) Content  public int getContentLength() - returns the length, in bytes. -1 is returned if the length is not known.  getContentType() - returns the request’s MIME type of the content (null if it’s not known).  getCharacterEncoding() - returns the name character encoding style of the request.
  • 15. Header Methods  setDateHeader()  setIntHeader()  setContentType()  setContentLength()  addCookie()  sendError()
  • 16. Ways to manage session,  Hidden form fields  URL rewriting  Persistent cookies  Session tracking API
  • 17. <input type=”hidden” name=”pageid” value=”5”>  public String getParameter(String name)  public Enumeration getParameterNames()  public String[] getParameterValues(String name)
  • 19. API for persistent cookie is, javax.servlet.http.Cookie To create a new cookie,  Cookie cookie(String name, String value)  Eg: Cookie cookie = new Cookie("ID", "123"); To get all available cookies,  req.getCookies() To send back the cookie name  response.addCookie(cookie_name)
  • 20.  A servlet with getSession( ) method retrieves the current HttpSession object Eg: public HttpSession HttpServletRequest.getSession(boolean )  Set properties by, public void HttpSession.setAttribute(String name, Object value) Eg: session.setAttribute(“name”, id);  Get properties by, public void HttpSession.setMaxInactiveInterval(int secs)
  • 21.  Get current session id by, public String getId()  Whether it is a new cookie or referenced, public boolean isNew  Start of a session public long getCreationTime()  Last session activity public long getLastAccessedTime  Session invalidating by, public void invalidate()  Removing attribute by, public void removeAttribute(String name)
  • 22. import java.io.*; import java.net.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class srvltHoldAppID extends HttpServlet {public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { resp.setContentType(“text/html”); PrintWriter out = res.getWriter(); String sAPPID` = “undefined”; String[] sAPPID = req.getParameter(“APPID”); if(sAPPID != null && sAPPID.length > 0) { // Create session: HttpSession session = req.getSession(); session.setAttribute(“APPID”, sAPPID); }}}
  • 23. ServletContext sc = this.getServletContext(); RequestDispatcher rd = sc.getRequestDispatcher(“/srvltComplete”); if (rd !=null) { try { rd.forward(req, res); } catch (Exception e) { // Handle Exception } }
  • 24. Connection conn = null; Statement stmt = null; ResultSet rs = null; String sSQL = “……….” InitialContext ic = new InitialContext() //Get a reference to the datasource String dsName = “java:comp/env/jdbc/emplphone” DataSource ds = (DataSource) ic.lookup(dsName) conn = ds.getConnection() // Get a Connection stmt = conn.createStatement() rs = stmt.executeQuery(sSQL) while( rs.next()) {out.println(rs.getString(1))}