Introduction to Java Servlets and The Need for Dynamic Content
Faculty name: Ravula Kartheek M.Tech, (Ph. D)
Ravula Kartheek
https://ravulakartheek.blogspot.com/
Index
Faculty Name: Ravula Kartheek M.Tech, (Ph. D)
Assistant Professor in CSE of Data Science
Ravula Kartheek
https://ravulakartheek.blogspot.com/
1. Introduction to Java Servlets
Topic: The Need for Dynamic Content
Faculty Name: Ravula Kartheek M.Tech, (Ph. D)
Assistant Professor in CSE of Data Science
Ravula Kartheek
https://ravulakartheek.blogspot.com/
Continue…
Fig: Advantages of Servlet
Faculty Name: Ravula Kartheek M.Tech, (Ph. D)
Assistant Professor in CSE of Data Science
Ravula Kartheek
https://ravulakartheek.blogspot.com/
Continue…
Faculty Name: Ravula Kartheek M.Tech, (Ph. D)
Assistant Professor in CSE of Data Science
Ravula Kartheek
https://ravulakartheek.blogspot.com/
2. Java Servlet Technology
- Servlet can be described in many ways, depending on the context.
Faculty Name: Ravula Kartheek M.Tech, (Ph. D)
Assistant Professor in CSE of Data Science
Ravula Kartheek
https://ravulakartheek.blogspot.com/
3. Why Servlets?
Faculty Name: Ravula Kartheek M.Tech, (Ph. D)
Assistant Professor in CSE of Data Science
Ravula Kartheek
https://ravulakartheek.blogspot.com/
4. What can servlets do?
Faculty Name: Ravula Kartheek M.Tech, (Ph. D)
Assistant Professor in CSE of Data Science
Ravula Kartheek
https://ravulakartheek.blogspot.com/
5. Java Servlet API and Lifecycle
Java Servlet API
Interface – What to do?
Class – How to do it?
Faculty Name: Ravula Kartheek M.Tech, (Ph. D)
Assistant Professor in CSE of Data Science
Ravula Kartheek
https://ravulakartheek.blogspot.com/
Interfaces in jakarta.servlet package
| 2. ServletRequest |
3. ServletResponse | 4. RequestDispatcher |
5. ServletConfig | 6. ServletContext |
7. SingleThreadModel (deprecated now) | 8. Filter |
9. FilterConfig | 10. FilterChain |
11. ServletRequestListener | 12. ServletRequestAttributeListener |
13. ServletContextListener | 14. ServletContextAttributeListener |
| |
Faculty Name: Ravula Kartheek M.Tech, (Ph. D)
Assistant Professor in CSE of Data Science
Ravula Kartheek
https://ravulakartheek.blogspot.com/
Classes in jakarta.servlet package
1. GenericServlet | 2. ServletInputStream |
3. ServletOutputStream | 4. ServletRequestWrapper |
5. ServletResponseWrapper | 6. ServletRequestEvent |
7. ServletContextEvent | 8.ServletRequestAttributeEvent |
9. ServletContextAttributeEvent | 10. ServletException |
11. UnavailableException | |
Faculty Name: Ravula Kartheek M.Tech, (Ph. D)
Assistant Professor in CSE of Data Science
Ravula Kartheek
https://ravulakartheek.blogspot.com/
Interfaces in jakarta.servlet.http package
| 2. HttpServletResponse |
3. HttpSession | 4. HttpSessionListener |
5. HttpSessionAttributeListener | 6. HttpSessionBindingListener |
7. HttpSessionActivationListener | 8. HttpSessionContext (deprecated now) |
| |
Faculty Name: Ravula Kartheek M.Tech, (Ph. D)
Assistant Professor in CSE of Data Science
Ravula Kartheek
https://ravulakartheek.blogspot.com/
Classes in jakarta.servlet.http package
| 2. Cookie |
3. HttpServletRequestWrapper | 4. HttpServletResponseWrapper |
5. HttpSessionEvent | 6. HttpSessionBindingEvent |
7. HttpUtils (deprecated now) | |
Faculty Name: Ravula Kartheek M.Tech, (Ph. D)
Assistant Professor in CSE of Data Science
Ravula Kartheek
https://ravulakartheek.blogspot.com/
The Servlet Skeleton
Fig: Skeleton Structure
Faculty Name: Ravula Kartheek M.Tech, (Ph. D)
Assistant Professor in CSE of Data Science
Ravula Kartheek
https://ravulakartheek.blogspot.com/
The Servlet Life Cycle
Fig: Servlet Life Cycle
Faculty Name: Ravula Kartheek M.Tech, (Ph. D)
Assistant Professor in CSE of Data Science
Ravula Kartheek
https://ravulakartheek.blogspot.com/
Continue…
The init() Method
Initializes the servlet. It is the life cycle method of servlet and invoked by the web container only once.
Syntax:
public void init() throws ServletException
{
//Initialization code…
}
Faculty Name: Ravula Kartheek M.Tech, (Ph. D)
Assistant Professor in CSE of Data Science
Ravula Kartheek
https://ravulakartheek.blogspot.com/
Continue…
The service() Method
Provides response for the incoming request. It is invoked at each request by the web container.
Syntax:
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException
{
}
Faculty Name: Ravula Kartheek M.Tech, (Ph. D)
Assistant Professor in CSE of Data Science
Ravula Kartheek
https://ravulakartheek.blogspot.com/
Continue…
The doGet() Method
This method process normal request to URL form as shown below:
Syntax:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
//Servlet code
}
Faculty Name: Ravula Kartheek M.Tech, (Ph. D)
Assistant Professor in CSE of Data Science
Ravula Kartheek
https://ravulakartheek.blogspot.com/
Continue…
The doPost() Method
A post request results from an HTML form that specifically lists POST as the METHOD and it should be handled by doPost() method.
Syntax:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
//Servlet code
}
Faculty Name: Ravula Kartheek M.Tech, (Ph. D)
Assistant Professor in CSE of Data Science
Ravula Kartheek
https://ravulakartheek.blogspot.com/
Continue…
The destroy() Method
It is invoked only once and indicates that servlet is being destroyed. After the destroy() method is called, the servlet object is marked for garbage collection.
Syntax:
public void destroy()
{
//Finalization code…
}
Faculty Name: Ravula Kartheek M.Tech, (Ph. D)
Assistant Professor in CSE of Data Science
Ravula Kartheek
https://ravulakartheek.blogspot.com/
A Simple Welcome Servlet
import jakarta.servlet.ServletException;
import jakarta.servlet.http.*;
import java.io.*;
public class Demo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
pw.println("<html><body>");
pw.println("<h1>Welcome to the Servlet</h1>");
pw.println("</body></html>");
pw.close();
}
}
*DemoServlet project in Eclipse
Faculty Name: Ravula Kartheek M.Tech, (Ph. D)
Assistant Professor in CSE of Data Science
Ravula Kartheek
https://ravulakartheek.blogspot.com/
OUTPUT
Faculty Name: Ravula Kartheek M.Tech, (Ph. D)
Assistant Professor in CSE of Data Science
Ravula Kartheek
https://ravulakartheek.blogspot.com/
Working With Servlets
Faculty Name: Ravula Kartheek M.Tech, (Ph. D)
Assistant Professor in CSE of Data Science
Ravula Kartheek
https://ravulakartheek.blogspot.com/