Professional Documents
Culture Documents
UNIT III
Syllabus
SERVER SIDE PROGRAMMING
Servlets: Java Servlet Architecture- Servlet Life Cycle- Form GET and POST actions-Session Handling-
Understanding Cookies- Installing and Configuring Apache Tomcat Web Server- DATABASE
CONNECTIVITY: JDBC perspectives, JDBC program example - JSP: Understanding Java Server Pages-
JSP Standard Tag Library (JSTL)-Creating HTML forms by embedding JSP code.
Servlets
Why use Servlets
• To develop a web application
• To deliver the application to anyone with a browser
What is a Servlet
▪ Servlet is a java program that runs inside JVM on the web server.
▪ It is used for developing dynamic web applications.
▪ Servlet do not have main() method. They are under the control of TOMCAT.
static and dynamic web page
▪ Static page remains same for all users
▪ A dynamic web page changes based on the request from client
Features of a Servlet
▪ Portable
o Platform independence
▪ Efficient and Scalable
o Simultaneous support for multiple requests using multithreading
▪ Robust
o Garbage collection, Exception handling.
Servlet API
▪ We need to use Servlet API to create servlets.
▪ Two packages will be used
o javax.servlet : Contains the classes to support generic servlet (protocol-
independent servlet)
▪ Must extend javax.servlet.GenericServlet class
o javax.servlet.http : contains classes to support http servlet.
▪ Must extend javax.servlet.http.HttpServlet class
HTTP Servlet
▪ HttpServlet class, which is an abstract class. The HTTP Servlet doesn’t override the
service() method.
▪ It must override at least one method from the list below:
doGet() , doPost(),doPut(),doDelete(), init() and destroy() and getServletInfo()
POST request : The following form will call doPost() method when the form is submitted.
<form action=”servlet” method=”post”>
</form>
➢ POST is used to send data to a server to create/update a resource.
GET POST
1) In case of Get request, only limited amount In case of post request, large amount of
of data can be sent because data is sent in data can be sent because data is 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.
HttpSession session=request.getSession();
o The above statement will create a session ID by TOMCAT server and then the session
id will be sent to the client.
getSession(false) vs getSession(true)
❑ getSession(true) will check whether a session already exists for the user. If yes, it will
return that session object else it will create a new session object and return it.
❑ getSession(false) will check existence of session. If session exists, then it returns the
reference of that session object, if not, this methods will return null.
session-config>
<session-timeout> 15 </session-timeout>
</session-config>
Setting session timeout for a specific session
session.setMaxInactiveInterval(20*60);
if the client does not make any requests on the session for 20 minutes, kill it.
Cookies
• Cookie is a little piece of data ( a name / value String pair) exchanged between the client
and server.
• Cookies are used to exchange session ID information between client and server.
• The Container does virtually all the cookie work with the below statement.
HttpSession s = request.getSession();
o To run servlets in Eclipse, we need TOMCAT server installed the on the system and
configured in the Eclipse IDE.
Step 1: Download and Install Eclipse IDE
Install Eclipse on Windows
o You would see two options on the right side (32 bit and 64 bit), click on 32 bit if you
system is 32 bit else click on 64 bit. This will download a zipped file on your system.
o To install Eclipse, unzip the downloaded file and copy the unzipped folder to the desired
location.
o Click on Add
o The forName() method of Class class is used to register the driver class. This
method is used to dynamically load the driver class.
o public static void forName(String className) throws ClassNotFoundException
o Class.forName("oracle.jdbc.driver.OracleDriver");
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2));
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String name;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE",
"webapp","webapp");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from user_details");
while(rs.next()) {
name = rs.getString(1);
System.out.println(name);
}
con.close();
}
catch (Exception e) {System.out.println(e.getMessage().toString());}
o JSP technology is used to create web application just like Servlet technology. It can be
Advantages of JSP
o Extension to Servlet
o Easy to maintain
o Less code than Servlet
▪ There are 9 jsp implicit objects. These objects are created by the web container that
are available to all the jsp pages.
Example of JSP
<html>
<body>
WT – UNIT III – R19 Page 18
<% out.print(2*5); %>
</body>
</html>
We can write JSP code inside a HTML form. Example is given below:
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="Welcome.jsp" method=”get”>
</form>
</body>
Welcome.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Page</title>
</head>
<body>
<%
String fname=(String)request.getParameter("FName");
out.print("Hello "+fname);
%>
</body>
</html>
Output
http://localhost:9999/JSP_Example/Welcome.jsp?FName=SVCK
We can write JSP code inside a HTML form. Example is given below:
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="Welcome.jsp" method=”post”>
</form>
</body>
Welcome.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
WT – UNIT III – R19 Page 20
<title>JSP Page</title>
</head>
<body>
<%
String fname=(String)request.getParameter("FName");
out.print("Hello "+fname);
%>
</body>
</html>
Output
http://localhost:9999/JSP_Example/Welcome.jsp
• Java provides various scripting elements that allow you to insert Java code from your
JSP code into the servlet
• Scripting elements in JSP must be written within the <% %> tags.
• The JSP engine will process any code you write within the pair of the <% and %> tags
2) Directives
• These tags are used to provide specific instructions to the web container when the page
is translated. It has three subcategories:
• Page: <%@ page ... %>
• Include: <%@ include ... %>
• Taglib: <%@ taglib ... %>
3) Declaration
• As the name suggests, it is used to declare methods and variables you will use in your
Java code within a JSP file.
• According to the rules of JSP, any variable must be declared before it can be used.
• <%! declaration; [ declaration; ]+ ... %>
• <%! int variable_value=62; %>
4) JSP Scriptlet tag
• The scriptlet tag allows writing Java code statements within the JSP page.
• This tag is responsible for implementing the functionality of _jspService() by scripting
the java code.
• <% JAVA CODE %>
• <% int cnt = 2; %>
• <% out.println(cnt); %>
5) JSP Expressions
• Expressions elements are responsible for containing scripting language expression,
which gets evaluated and converted to Strings by the JSP engine and is meant to the
output stream of the response.
• Hence, you are not required to write out.print() for writing your data.
• This is mostly used for printing the values of variables or methods in the form of output.
• <%= "A JSP based string" %>
JSTL
o JSTL stands for JSP Standard Tag Library.
o The JSP Standard Tag Library (JSTL) represents a set of tags to simplify the JSP
development.
o JSTL is the standard tag library that provides tags to control the JSP page behavior.
o JSTL tags can be used for iteration and control statements, internationalization, SQL
etc.
o For creating JSTL based application, we need to download the jstl.jar file.
1. Fast Development JSTL provides many tags that simplify the JSP.
2. Code Reusability We can use the JSTL tags on various pages.
3. No need to use scriptlet tag: It avoids the use of scriptlet tag.
JSTL Tags
Function tags The functions tags provide support for string manipulation and
string length. The URL for the functions tags
is http://java.sun.com/jsp/jstl/functions and prefix is fn.
Formatting tags The Formatting tags provide support for message formatting,
number and date formatting, etc. The URL for the Formatting tags
is http://java.sun.com/jsp/jstl/fmt and prefix is fmt.
XML tags The XML tags provide flow control, transformation, etc. The URL
for the XML tags is http://java.sun.com/jsp/jstl/xml and prefix is x.
SQL tags The JSTL SQL tags provide SQL support. The URL for the SQL
tags is http://java.sun.com/jsp/jstl/sql and prefix is sql.
The <c:import> is used to include the content of any resource either within server or outside the
server.
<html>
<head>
<title>Tag Example</title>
</head>
<body>
<c:import var="data" url="http://www.google.com"/>
<c:out value="${data}"/>
</body>
</html>
Java program
Files
Library files
Deployment Descriptor