Server-Side Programming in JSP and Servlets
1. Where JSP/Servlets Fit
Both are Java technologies that run on the server to generate dynamic web content:
Browser --HTTP--> Web Server (Tomcat)
|
v
JSP / Servlet container
(compiles & executes Java)
|
v
HTML / XML sent back as the response
- Servlet — a Java class that handles a request and writes the response programmatically (HTML inside Java code).
- JSP (JavaServer Pages) — HTML with embedded Java; JSP is internally translated into a servlet, so JSP = presentation-friendly servlet.
2. Servlet Life Cycle (javax.servlet.http.HttpServlet)
1. Loading - container loads the class (first request or on start-up)
2. Instantiation - new instance created
3. init() - called once; initialisation (config params, DB pools)
4. service() - called per request; dispatches to doGet()/doPost()
5. destroy() - called once before unloading; release resources
Web.xml / annotation mapping:
<servlet><servlet-name>h</servlet-name><servlet-class>HelloServlet</servlet-class></servlet>
<servlet-mapping><servlet-name>h</servlet-name><url-pattern>/hello</url-pattern></servlet-mapping>
or @WebServlet("/hello") (Servlet 3.0+).
3. Servlet Capabilities
- Reading form data —
req.getParameter("id"),getParameterValues(),getParameterNames(). - Session tracking —
HttpSession s = req.getSession(); cookies; URL rewriting; hidden form fields. - Redirect vs forward —
res.sendRedirect("url")(client, new request) vsRequestDispatcher.forward(req,res)(server-side, same request). - Filters, listeners, init parameters, connection pooling for production apps.
4. JSP — Elements and Life Cycle
JSP → translated → compiled servlet → same life cycle (jspInit(), _jspService(), jspDestroy()).
| Element | Syntax | Runs where |
|---|---|---|
| Declaration | <%! int count = 0; %> |
Class level (fields/methods) |
| Scriptlet | <% for(int i=0;i<5;i++){ ... } %> |
Inside _jspService() |
| Expression | <%= count %> |
Evaluated and printed to out |
| Directive | <%@ page import="java.util.*" %> |
Page/language/include/taglib settings |
| Action | <jsp:include page="x.jsp"/>, <jsp:forward>, <jsp:useBean> |
XML actions executed at runtime |
| Comment | <%-- hidden --%> |
Not sent to the client |
Implicit objects available in every JSP: request, response, out, session, application, config, pageContext, page, exception.
5. MVC with JSP + Servlet
BROWSER
| request
v
CONTROLLER (Servlet) -- decides flow, validates input
| sets bean/model data
v
MODEL (JavaBean / DAO) -- business logic + database
|
v
VIEW (JSP) -- renders HTML from the model --> back to browser
Servlet = controller, JSP = view, bean/DAO = model. This separation is the classic reason for using both together.
6. Advantages and Limitations
Advantages
- Full power of Java: OOP, multithreading, huge library, portability across platforms/servers.
- Multithreaded container — one instance serves many requests (efficient memory use).
- JSP keeps HTML designers and Java developers apart; automatic session management; rich tag libraries (JSTL) reduce scripting.
Limitations
- Servlets mix HTML with Java — hard to maintain presentation; JSP with too many scriptlets has the same problem (solved by JSTL/EL/MVC).
- Steeper learning curve; requires a Java container (Tomcat/Jetty) — heavier than plain PHP for small sites.
- Debugging compiled servlets, thread-safety care (instance variables shared across requests).
7. Exam Points
- Explain the servlet life cycle with methods — 5-7 marks (very frequent).
- Write a servlet / JSP that reads a form and generates a dynamic page — 10 marks.
- Differentiate JSP and servlet; explain JSP elements with syntax — 5-10 marks.
- Explain MVC architecture using JSP/Servlet/Bean — 5 marks.
- List JSP implicit objects — 3-5 marks.