import [Link].
IOException;
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/hello")
public class HelloWorldServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
[Link]().println("<h1>Hello World!</h1>");
}
}
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/calculate")
public class CalculatorServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException {
[Link]("text/html");
try {
double num1 = [Link]([Link]("num1"));
double num2 = [Link]([Link]("num2"));
String operator = [Link]("operator");
double result = 0;
switch (operator) {
case "add": result = num1 + num2; break;
case "subtract": result = num1 - num2; break;
case "multiply": result = num1 * num2; break;
case "divide":
if (num2 == 0) {
[Link]().println("Error: Division by zero.");
return;
}
result = num1 / num2;
break;
}
[Link]().println("<h1>Result: " + result + "</h1>");
} catch (Exception e) {
[Link]().println("<h1>Error: Invalid input.</h1>");
}
}
}
---
✅ Q2. What is Session Tracking?
1. 🔹 HTTP is stateless — it doesn’t remember users between requests.
2. 🔹
Session Tracking is a technique used to maintain user identity across multiple web pages
or requests.
---
✅ Session Tracking Techniques
1. Cookies
3. 🔹 Server assigns a unique session ID in the form of a cookie.
4. 🔹 Browser sends the cookie with each request.
5. 🔸 Not reliable if cookies are disabled.
---
2. Hidden Form Fields
6. 🔹
Session ID is sent as a hidden input field in a form.
Example:
<input type="hidden" name="sessionid" value="12345">
7. 🔸 Works only with forms — not with hyperlinks.
---
3. URL Rewriting
8. 🔹Appends session ID directly to the URL.
Example:
[Link]
9. 🔸
Works even if cookies are disabled,
but every URL must be dynamically generated.
---
4. HttpSession Object
10. 🔹
Most powerful and flexible method.
Servlets use HttpSession to store user data.
11. 🔹 Created using:
HttpSession session = [Link]();
---
✅ Important HttpSession Methods
12. setAttribute(name, value) – Add data to session
13. getAttribute(name) – Retrieve data
14. removeAttribute(name) – Delete a specific attribute
15. invalidate() – Destroy entire session
16. getId() – Get session ID
17. getCreationTime() – When session started
18. getLastAccessedTime() – When session was last used
19. setMaxInactiveInterval(seconds) – Set timeout
20. getMaxInactiveInterval() – Get session timeout
21. isNew() – Check if session is new
---
✅ Ways to End a Session
22. 🔸 removeAttribute() – Remove one item
23. 🔸 invalidate() – End session completely
24. 🔸 setMaxInactiveInterval() – Auto-expire session
25. 🔸 logout() – Log user out (Servlet 2.4+)
---
---
✅ Q1. Cookies in Servlet
1. 🔹 A cookie is a small piece of information maintained between multiple client requests to
help maintain state.
2. 🔹
Each cookie has a name and value, with optional attributes like comment, path, domain,
max age, version.
3. 🔹
Cookies are a client-side technique — stored in the browser and sent with every request
to the server.
---
✅ How Cookies Work
4. 🔹 Server treats every request as new by default. To retain state, it adds a cookie to the
response.
5. 🔹
That cookie is stored in the browser’s cache, and is automatically sent back with the next
request.
6. 🔹
Cookie flow:
➤ Browser → Initial request
➤ Server → Sends cookie with response
➤ Browser → Sends cookie in future requests
---
✅ Types of Cookies
7. 🔹 Non-Persistent Cookie: Valid for a single session only; deleted when browser is closed.
8. 🔹 Persistent Cookie: Valid across sessions; stored until user logs out or manually deleted.
---
✅ Advantages
9. 🔹 Simplest method for session tracking
10. 🔹 Managed on the client-side, reducing server load
---
✅ Disadvantages
11. 🔹 Won’t work if cookies are disabled in the browser
12. 🔹 Can store only textual information
---
✅ Using Cookies in Java (Servlets)
13. 🔹 Create Cookie:
Cookie ck = new Cookie("user", "sonoo jaiswal");
[Link](ck);
14. 🔹 Get Cookie:
Cookie ck[] = [Link]();
15. 🔹 Delete Cookie:
Cookie ck = new Cookie("user", "");
[Link](0);
[Link](ck);
---
✅ Important Methods
16. 🔹 addCookie(Cookie ck) – Adds a cookie to response
17. 🔹 getCookies() – Retrieves all cookies from request
18. 🔹 setMaxAge(int) – Sets cookie expiry
19. 🔹 getName(), getValue() – Get cookie details
---
JDBC acts as a standard API that allows a Java application to communicate with various
databases. The enterprise data, which is typically stored in a relational database, can be
accessed using the JDBC APIs. The process involves several key components:
* Application: This is the enterprise application itself, such as a Java servlet or applet, which
needs to communicate with a data source.
* JDBC API: This is the interface that allows the Java application to execute SQL statements
and retrieve results from the database. It includes important interfaces like Driver, Connection,
PreparedStatement, and ResultSet, as well as classes like DriverManager.
* JDBC Driver Manager: This component plays an important role by using database-specific
drivers to connect the application to the database.
* JDBC Drivers: These are client-side adapters that translate requests from the Java program
into a protocol that the database management system (DBMS) can understand. To
communicate with a specific data source, a corresponding JDBC driver is required.
Architectural Models
The interaction can follow two main architectural models:
* Two-Tier Model: In this model, the Java application communicates directly with the data
source.
* Three-Tier Model: Here, the application's queries are first sent to a middle tier of services,
which then sends the commands to the data source.