Unit 5
Unit 5
Java Servlet
The Java Servlet API is a class library for Servlets. Servlets are Java
programs that run on the server and send response to the client. Two
packages are available for Servlet programmers: 1) javax.servlet and 2)
javax.servlet.http.
The first one contains basic classes and interfaces that we can use to write
Servlets from the scratch. The second package, javax.servlet.http, offers
more advanced classes and interfaces that extend classes and interfaces
from the first package. It is much more convenient to program using the
second package.
javax.servlet
Servlet class Hierarchy
• Many Web servers that are suitable for personal use or low-
traffic websites are offered for free or at extremely cheap
costs eg. Java servlet. However, the majority of commercial-
grade Web servers are rather expensive, with the notable
exception of Apache, which is free.
HTTP protocol
HTTP is a protocol which allows the fetching of resources,
such as HTML documents. It is the foundation of any data
exchange on the Web and it is a client-server protocol, which
HTTP protocol
Basic Features There are three basic features that make HTTP a simple
but powerful protocol:
The server and client are aware of each other only during a
current request.
Due to this nature of the protocol, neither the client nor the
browser can retain information between different requests across
the web pages.
HTTP Servlet
If you creating Http Servlet you must extend
javax.servlet.http.HttpServlet class, which is an
abstract class. Unlike Generic Servlet, the HTTP
Servlet doesn’t override the service()
method.
Instead it overrides one or more of the following
methods. It must override at least one method from
the list below:
doGet() – This method is called by servlet service
method to handle the HTTP GET request from
client. The Get method is used for getting
information from the server
• Using JSP, you can collect input from users through web page
forms, present records from a database or another source, and
create web pages dynamically.
• Compilation
• Initialization
• Execution
• Cleanup
Coding in Servlet
Coding in JSP
How to write a comment in a JSP page?
JSP Scripting elements
1. scriptlet tag
2. expression tag
3. declaration tag
JSP scriptlet tag
<html>
<body>
<% out.print(“Welcome to JSP"); %> <!-- scriptlet tag -->
</body>
</html
JSP expression tag
<html>
<body>
<%= "welcome to jsp" %>
</body>
</html>
JSP Declaration Tag
This Tag is used for declare the variables. Along with this,
Declaration Tag can also declare method and classes.
Jsp initializer scans the code and find the declaration tag
and initializes all the variables, methods, and classes.
• The concepts are simple here -- as you can see, you can drop
out of the scriptlets, write normal HTML, and get back into the
scriptlet.
1. page directive
2. include directive
3. taglib directive
Include Directive
• If I just copy the content of all files in the main JSP then I
have to edit it whenever there is a change in any of the
external file, instead we can include all files using
directive and edit the particular file whenever needed.
Also, by using include directive you can enhance the code
re-usability: Let’s say there is a certain code or data which
needs to be there in all the JSP pages of your application
then you can simply have that code/data in one file and
include the file in all the JSP pages.