Servlet Programs
Q.1) Create a servlet for creating a filter to check whether a user is valid or not
import [Link].*;
import [Link].*;
import [Link].*;
public class UserValidationFilter implements Filter {
public void init(FilterConfig config) throws ServletException {}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws
IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
// Assuming user validation is done through session attribute
HttpSession session = [Link]();
String user = (String) [Link]("user");
if (user != null) {
[Link](request, response); // User is valid, continue the request
} else {
[Link]("[Link]"); // Redirect to login page if invalid
}
public void destroy() {}
Q.2) Create a servlet to access user info using Cookie
import [Link].*;
import [Link].*;
import [Link].*;
public class UserInfoCookieServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
Cookie[] cookies = [Link]();
String username = null;
if (cookies != null) {
for (Cookie cookie : cookies) {
if ([Link]().equals("username")) {
username = [Link]();
[Link]("text/html");
PrintWriter out = [Link]();
if (username != null) {
[Link]("<h1>Welcome, " + username + "</h1>");
} else {
[Link]("<h1>No user information found in cookies</h1>");
Q.3) Create a servlet to destroy user info by deleting cookie
import [Link].*;
import [Link].*;
import [Link].*;
public class DestroyUserInfoServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
Cookie cookie = new Cookie("username", ""); // Create a blank cookie
[Link](0); // Set the age of the cookie to 0, which will delete it
[Link](cookie);
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<h1>User info deleted from cookies</h1>");
}
}
Q.4) Create a servlet to access user info using Hidden form Field
import [Link].*;
import [Link].*;
import [Link].*;
public class HiddenFieldServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
String username = [Link]("username");
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<h1>Welcome, " + username + "</h1>");
Q.5) Create a servlet to access user info using UrlRewriting
import [Link].*;
import [Link].*;
import [Link].*;
public class URLRewritingServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
String username = [Link]("username");
// URL Rewriting
String encodedURL = [Link]("WelcomeServlet?username=" + username);
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<a href='" + encodedURL + "'>Go to Welcome Page</a>");
Q.6) Create a servlet to snoop all the information about current session
import [Link].*;
import [Link].*;
import [Link].*;
public class SessionInfoServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
HttpSession session = [Link]();
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<h1>Session Information</h1>");
[Link]("Session ID: " + [Link]());
[Link]("<br>Creation Time: " + new [Link]([Link]()));
[Link]("<br>Last Accessed Time: " + new [Link]([Link]()));
Q.7) Create a servlet to destroy the user info by invalidating session
import [Link].*;
import [Link].*;
import [Link].*;
public class InvalidateSessionServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
HttpSession session = [Link]();
[Link](); // Invalidates the session
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<h1>User session invalidated</h1>");
Q.8) Create a Listener using ServletContextListener to listen the event on creating and destroying
Context Parameters on a servlet
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
@WebListener
public class ContextListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) {
ServletContext context = [Link]();
[Link]("appStart", new Date());
[Link]("Context Initialized: " + [Link]("appStart"));
public void contextDestroyed(ServletContextEvent sce) {
ServletContext context = [Link]();
[Link]("Context Destroyed: " + [Link]("appStart"));
Q.9) Create a Listener using HttpSessionListener to listen the event on creating and destroying
session of a servlet
import [Link].*;
import [Link].*;
import [Link].*;
@WebListener
public class SessionListener implements HttpSessionListener {
public void sessionCreated(HttpSessionEvent se) {
HttpSession session = [Link]();
[Link]("Session Created: " + [Link]());
public void sessionDestroyed(HttpSessionEvent se) {
HttpSession session = [Link]();
[Link]("Session Destroyed: " + [Link]());
Q.10) Create a servlet that establishes a Database Connection successfully using JDBC Connection
and create a table in the database
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class DBConnectionServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
try {
// Establish DB connection
[Link]("[Link]");
Connection conn = [Link]("jdbc:mysql://localhost:3306/mydb", "root",
"password");
// Create a table
Statement stmt = [Link]();
String createTableSQL = "CREATE TABLE Employees (ID INT PRIMARY KEY
AUTO_INCREMENT, Name VARCHAR(255), Age INT)";
[Link](createTableSQL);
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<h1>Database Connection Successful & Table Created</h1>");
[Link]();
} catch (Exception e) {
[Link]();