Professional Documents
Culture Documents
Servlet technology is used to create web application (resides at server side and generates
dynamic web page).
Java Servlets are programs that run on a Web or Application server and act as a middle layer
between a request coming from a Web browser or other HTTP client and databases or
applications on the HTTP server.
Using Servlets, you can collect input from users through web page forms, present records
from a database or another source, and create web pages dynamically.
Servlets Tasks:
Read the explicit data sent by the clients (browsers). This includes an HTML form on a Web
page or it could also come from an applet or a custom HTTP client program.
Read the implicit HTTP request data sent by the clients (browsers). This includes cookies,
media types and compression schemes the browser understands
Process the data and generate the results. This process may require talking to a database,
executing an RMI or CORBA call, invoking a Web service, or computing the response directly.
Send the explicit data (i.e., the document) to the clients (browsers). This document can be
sent in a variety of formats, including text (HTML or XML), binary (GIF images), Excel, etc.
Send the implicit HTTP response to the clients (browsers). This includes telling the browsers
or other clients what type of document is being returned (e.g., HTML), setting cookies and
caching parameters, and other such tasks.
1. HTTP
4. Container
6. Content Type
7. Introduction of XML
8. Deployment
1
1.HTTP (Hyper Text Transfer Protocol)
1. Http is the protocol that allows web servers and browsers to exchange data over the web.
4. It is stateless means each request is considered as the new request. In other words,
server doesn't recognize the user bydefault.
GET POST
1) In case of Get request, only limited In case of post request, large amount of
amount of data can be sent because data is data can be sent because data is sent in
sent in header. body.
2) Get request is not secured because data is Post request is secured because data is
exposed in URL bar. not exposed in URL bar.
2
5) Get request is more efficient and used Post request is less efficient and used
more than Post less than get.
Container
It provides runtime environment for JavaEE (j2ee) applications.
2. Multithreaded support
3. Object Pooling
4. Security etc.
Server
It is a running program or software that provides services.
1. Web Server
2. Application Server
Web Server
Web server contains only web or servlet container. It can be used for servlet, jsp, struts,
jsf etc. It can't be used for EJB.
Application Server
Application server contains Web and EJB containers. It can be used for servlet, jsp,
struts, jsf, ejb etc.
3
Content Type
Content Type is also known as MIME (Multipurpose internet Mail Extension) Type. It is a HTTP
header that provides the description about what are you sending to the browser.
images/jpeg video/quicktime So on
Reading input Data (or) Form Data from the Browser (or)
1. getParameter
Returns the value of a request parameter as a String, or null if the parameter does not
exist.
Request parameters are extra information sent with the request.
For HTTP servlets, parameters are contained in the query string or posted form data.
2. getParameterValues
Returns an array of String objects containing all of the values the given request
parameter has, or null if the parameter does not exist.
3. getParameterMap
4. getParameterNames
4
5. getQueryString
Returns the query string that is contained in the request URL after the path.
This method returns null if the URL does not have a query string.
test1.html
1. getParameter
<form action="servlet6">
First Name : <input type = 'text' name = 'param' /><br>
Last Name : <input type = 'text' name = 'param' /> <br>
Skills :
<input type='checkbox' name='skills' value="java"/>JAVA
<input type='checkbox' name='skills' value="android"/>ANDROID<br>
Gender
<input type='radio' name='gender' value="male" >MALE</input>
<input type='radio' name="gender" value='female' />FEMALE</br><br>
<input type = 'submit' value = 'sumbit' />
</form>
Web.xml
<web-app>
<servlet>
<servlet-name>s1 </servlet-name>
<servlet-class>sashi.kumar.Servlet1 </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s1 </servlet-name>
<url-pattern>/servlet1 </url-pattern>
</servlet-mapping>
<web-app>
Servlet1.java
5
package sashi.kumar;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.*;
2. getParameterMap
Servlet2.java
/* Name : Sashi
Name : Kumar */
6
for (String skill : skills)
out.println("Skill : " + skill);
/* Skill : java
Skill : android */
3. getParameterValues
4. getParameterNames
7
String name;
String values[];
while (names.hasMoreElements())
{
name = (String) names.nextElement(); // To find out Property Name
out.println("Name : " +name);
values = req.getParameterValues(name);
// Finding out values for the particular name
out.println("Values : " + Arrays.toString(values));
}
}
}
Output:
Name : skills
Values : [java, android]
Name : param
Values : [Sashi, Kumar]
Name : gender
Values : [male]
5. getQueryString
Output:
8
keysAndValues : [param=Sashi, param=Kumar, skills=java, skills=android, gender=male]
6. getinitParameter
Web.xml
<servlet>
<servlet-name>s6 </servlet-name>
<servlet-class>sashi.kumar.Servlet6 </servlet-class>
<init-param>
<param-name>p1 </param-name>
<param-value>1000 </param-value>
</init-param>
<init-param>
<param-name>p2 </param-name>
<param-value>oracle.jdbc.driver.OracleDriver</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>s6</servlet-name>
<url-pattern>/servlet6 </url-pattern>
</servlet-mapping>
Servlet6.java
9
{
PrintWriter out = res.getWriter();
String p1 = getInitParameter("p1");
String p2 = getInitParameter("p2");
/* ServletConfig config = getServletConfig();
String p1 = config.getInitParameter("p1");
String p2 = config.getInitParameter("p2");*/
out.println("P1 : " +p1);
out.println("P2 : " +p2);
}
}
Output:
P1 : 1000
P2 : oracle.jdbc.driver.OracleDriver
7. getinitParameterNames
Servlet6.java
package sashi.kumar;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
String name;
String value;
while (names.hasMoreElements())
{
10
name = (String)names.nextElement();
value = getInitParameter(name);
Output :
8. getInitParameter
web.xml
<servlet>
<servlet-name>s7 </servlet-name>
<servlet-class>sashi.kumar.Servlet7</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s7</servlet-name>
<url-pattern>/servlet7 </url-pattern>
</servlet-mapping>
<context-param>
<param-name>p3</param-name>
<param-value>Sashi Kumar </param-value>
</context-param>
<context-param>
<param-name>p4 </param-name>
<param-value>false</param-value>
</context-param>
Servlet7.java
String p3 = context.getInitParameter("p3");
String p4 = context.getInitParameter("p4");
Output:
P3 : Sashi Kumar
P4 : false
7. getinitParameterNames
Servlet9.java
Output:
13