Professional Documents
Culture Documents
07/02/12
Subject Name:
Web Technology & Application Development
(WTAD)
Subject Code: 640002
Servlet Basics,
Basic Servlet Structure,
Servlets Generating text/plain and text/html
content,
Packaging Servlets,
The Servlet Life-Cycle
7-Feb-12
Benefits
Saves time
Response time increases
It is scalable
JVM
Servlet 1
thread
Servlet 2
www.bipinrupadiya.blogspot.com
Bipin S Rupadiya
07/02/12
Benefits
Efficient
Convenient
Powerful
Portable
Secure
Inexpensive
Servlet Basics,
Servlet Implementation
javax.servlet
javax.servlet.http
10
doGet
doPost
HttpServletRequest
HttpServletResponse
11
www.bipinrupadiya.blogspot.com
12
Bipin S Rupadiya
07/02/12
A Servlet response
Example
response.setContentType("text/html");
response.setContentType("application/vnd.ms-excel");
response.setContentType("image/jpeg");
Etc
13
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class ServletDemo extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML><HEAD><TITLE>WTAD</TITLE></HEAD> <body>");
String str="<h1>welcome to </h1>";
str=str+"<br>www.bipinrupadiya.blogspot.com";
out.println(str);
out.println("</body> </html>");
}
}
14
web.xml structure
Application Name
WEB-INF
</servlet>
classes
<servlet-mapping>
FileName.class
<servlet-name>MyName</servlet-name>
<url-pattern>/my-address</url-pattern>
</servlet-mapping>
web.xml
Mr. Bipin Rupadiya
15
Resultant URL
Any identifier for servlet name
But both must be identical
<servlet-name>bipin1984</servlet-name>
<servlet-class>oet</servlet-class>
http://hostname/appName/my-address
http://localhost:8081/bipin/wtad/bsr.co.in
<servlet-name>bipin1984</servlet-name>
<url-pattern>/wtad/bsr.co.in</url-pattern>
</servlet-mapping>
<web-app>
16
Example of web.xml
<web-app>
<servlet>
Protocol
Custom URL for servlet
Web Application name
Host / domain name
17
www.bipinrupadiya.blogspot.com
18
Bipin S Rupadiya
07/02/12
Output
Packaging Servlets
19
20
Example
Application Name
WEB-INF
classes
Package
FileName.class
web.xml
Mr. Bipin Rupadiya
21
22
Output
init
www.bipinrupadiya.blogspot.com
24
destroy
doGet, doPost
service
23
Bipin S Rupadiya
07/02/12
1. Servlet Initialisation
In this first stage, the servlets constructor is
1. Servlet Initialisation
2. Servlet Execution
3. Servlet Destruction
to a database.
Mr. Bipin Rupadiya
25
2. Servlet Execution
3. Servlet Destruction
27
www.bipinrupadiya.blogspot.com
28
Example
Thanks . . .
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class LotteryNumber extends HttpServlet {
private int[] numbers = new int[10];
public void init() throws ServletException {
for(int i=0; i<numbers.length; i++) {
numbers[i] = randomNum();
} }
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException,
IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML><HEAD><TITLE>Bipin Rupadiya</TITLE></HEAD><BODY><OL>");
for(int i=0; i<numbers.length; i++) {
out.println(" <LI>" + numbers[i]);
}
out.println("</OL></BODY></HTML>");
}
private int randomNum() {
return((int)(Math.random() * 100));
}}
29
26
Contact:
Bipin S. Rupadiya
(MCA, PGDCA, BCA)
30