Servlet, JSP, JDBC and Java Web
Technology Questions & Answers
1. What is Servlet? Explain its lifecycle.
Servlet is a Java program that runs on a web server and handles client requests and
responses.
Lifecycle:
1. Loading and Instantiation
2. init() method
3. service() method
4. destroy() method
Diagram:
Client → init() → service() → destroy()
Sample Code:
public class DemoServlet extends HttpServlet {
public void init(){}
protected void doGet(HttpServletRequest req,
HttpServletResponse res){}
public void destroy(){}
}
2. Show with block diagram how product list view is shown in browser.
Explanation:
Browser sends request to servlet. Servlet gets product data from database and forwards it
to JSP page. JSP generates HTML and browser displays products.
Diagram:
Browser → Servlet → Database → JSP → Browser
3. Methods defined by HttpServlet class.
Methods:
doGet() – Read data
doPost() – Insert data
doPut() – Update data
doDelete() – Delete data
POST Request Handling:
protected void doPost(HttpServletRequest req,
HttpServletResponse res){
String name=[Link]("name");
}
4. What is Filter? Explain lifecycle.
Filter intercepts request and response before reaching servlet.
Lifecycle:
1. init()
2. doFilter()
3. destroy()
Diagram:
Browser → Filter → Servlet → Response
5. Request and Response objects.
Request object contains client data.
Response object sends output to browser.
Important Methods:
getParameter()
getSession()
getWriter()
sendRedirect()
Code:
String user=[Link]("name");
[Link]().println(user);
6. Auth filter block diagram.
Authentication filter checks user login.
Diagram:
Browser → Auth Filter → Servlet
Invalid User → Login Page
7. What is JSP? Explain tags.
JSP is used to create dynamic web pages.
Tags:
<%! %> Declaration
<% %> Scriptlet
<%= %> Expression
<%@ %> Directive
8. Explain JSP lifecycle.
Steps:
1. Translation
2. Compilation
3. jspInit()
4. _jspService()
5. jspDestroy()
Diagram:
JSP → Servlet → Compile → Execute
9. Why JSP preferred for VIEW in MVC?
JSP is preferred because:
- Easy UI design
- Cleaner code
- Separates logic and presentation
- Better maintenance
10. Implicit objects of JSP.
Important Objects:
request
response
session
out
application
Example:
[Link]("Hello");
11. What is JSTL?
JSTL is Java Standard Tag Library used instead of Java code in JSP.
Tags:
<c:if>
<c:forEach>
<c:out>
<c:set>
12. How MVC ensures presentation focus?
Servlet handles business logic.
JSP handles presentation.
Model handles data.
Thus JSP focuses only on UI.
13. What is MVC?
MVC = Model + View + Controller
Diagram:
Browser → Controller → Model → View → Browser
14. What is JDBC?
JDBC connects Java with database.
Steps:
1. Load Driver
2. Create Connection
3. Create Statement
4. Execute Query
5. Close Connection
15. JDBC insert code.
PreparedStatement ps=[Link](
"insert into books values(1,'Java')");
[Link]();
16. JDBC count books.
ResultSet rs=[Link](
"select count(*) from books");
while([Link]()){
[Link]([Link](1));
}
17. What is Session?
Session stores user data on server.
CRUD:
setAttribute()
getAttribute()
removeAttribute()
18. What are Cookies?
Cookies are small files stored in browser.
Code:
Cookie c=new Cookie("user","Ram");
[Link](60*60);
19. CRUD in Cookies.
Create:
[Link](c);
Read:
[Link]();
Update:
new Cookie("name","Hari");
Delete:
[Link](0);
20. Difference between Session and Cookies.
Session:
- Stored in server
- More secure
Cookies:
- Stored in browser
- Less secure
21. What is State Management?
State management keeps user data between requests.
HTTP is stateless, so session and cookies are needed.
22. Hashing vs Encryption.
Hashing:
One-way conversion.
Used for passwords.
Encryption:
Two-way conversion.
Used for secure communication.
23. Why bcrypt for password?
bcrypt adds salt and strong hashing.
It protects passwords from hacking and brute-force attacks.
24. SQL analysis query.
SELECT category,
SUM(price) AS total_sales
FROM products
GROUP BY category;
25. Compare EL and Scripting tags.
EL:
- Simple
- Cleaner
- Easy maintenance
Scripting:
- Java code inside JSP
- Complex
26. Ternary operator in EL.
${marks >= 40 ? "Pass" : "Fail"}
27. Why Generics better than Object casting?
Advantages:
- Type safety
- No manual casting
- Reduces errors
28. Collection classes in Java.
ArrayList:
Fast access
LinkedList:
Fast insertion
HashMap:
Key-value pair storage
29. What is indexing?
Indexing improves search speed in database.
Advantages:
- Faster query
Tradeoff:
- More storage
- Slower insert/update
30. File upload to DB.
Steps:
1. Select file
2. Read file
3. Store in DB
Code:
Part file=[Link]("file");
Best Practices:
- Store file path in DB
- Validate file size/type