Program 9:
Write an web application to show the servlet lifecycle
/servletlifecycle/WEB-INF/[Link]:
<web-app>
<servlet>
<servlet-name>LCDS</servlet-name>
<servlet-class>LifeCycleDemoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LCDS</servlet-name>
<url-pattern>/lifecycle</url-pattern>
</servlet-mapping>
</web-app>
/servletlifecycle/WEB-INF/classes/ [Link]:
import [Link];
import [Link];
import [Link].*;
public class LifeCycleDemoServlet implements Servlet {
private ServletConfig config;
// Initialization method
public void init(ServletConfig config) throws
ServletException {
[Link] = config;
[Link]("Servlet is being initialized using
init()");
}
// Service method - called for each request
public void service(ServletRequest request,
ServletResponse response)
throws ServletException, IOException {
[Link]("Request is being serviced using
service()");
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<html><body>");
[Link]("<h2>Hello from
LifeCycleDemoServlet</h2>");
[Link]("<p>This servlet demonstrates the life
cycle methods.</p>");
[Link]("</body></html>");
}
// Destruction method
public void destroy() {
[Link]("Servlet is being destroyed using
destroy()");
}
public ServletConfig getServletConfig() {
return [Link];
}
public String getServletInfo() {
return "LifeCycleDemoServlet version 1.0";
}
}Compilation:
Javac [Link]
Program 10:
Write an web application to greet the user
/wishex/[Link]:
<!DOCTYPE html>
<html>
<head>
<title>Greeting Form</title>
</head>
<body>
<h2>Enter your name</h2>
<form action="greet" method="get">
<label for="username">Name:</label>
<input type="text" id="username" name="username"
required>
<br><br>
<input type="submit" value="Greet Me">
</form>
</body>
</html>
/wishex/WEB-INF/[Link]:
<web-app>
<servlet>
<servlet-name>GS</servlet-name>
<servlet-class>GreetingServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GS</servlet-name>
<url-pattern>/greet</url-pattern>
</servlet-mapping>
</web-app>
/wishex/WEB-INF/classes/ [Link]:
import [Link];
import [Link];
import [Link].*;
public class GreetingServlet extends GenericServlet {
// Service method - called for each request
public void service(ServletRequest request,
ServletResponse response)throws ServletException, IOException
{
[Link]("Request is being serviced using
service()");
// Get the username from the request
String name = [Link]("username");
// Set response content type
[Link]("text/html");
// Generate dynamic response
PrintWriter out = [Link]();
[Link]("<html><body>");
[Link]("<h2>Hello, " + name + "!</h2>");
[Link]("<p>Welcome to the Greeting Servlet
Example.</p>");
[Link]("</body></html>");
}
Compilation:
Javac [Link]
Program 11:
Write an web application which uses servlet init parameters
/ loginex/[Link]:
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<h2>User Login</h2>
<form action="login" method="get">
<label for="username">Username:</label>
<input type="text" name="username" id="username"
required><br><br>
<label for="password">Password:</label>
<input type="password" name="password" id="password"
required><br><br>
<input type="submit" value="Login">
</form>
</body>
</html>
/loginex/WEB-INF/[Link]:
<web-app>
<servlet>
<servlet-name>LS</servlet-name>
<servlet-class>LoginServlet</servlet-class>
<init-param>
<param-name>drivername</param-name>
<param-value>[Link]</param-
value>
</init-param>
<init-param>
<param-name>url</param-name>
<param-
value>jdbc:oracle:thin:@localhost:1521:XE</param-value>
</init-param>
<init-param>
<param-name>username</param-name>
<param-value>system</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>Password123</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>LS</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>
/loginex/WEB-INF/classes/ [Link]:
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class LoginServlet extends HttpServlet {
private Connection conn;
public void init() throws ServletException {
try {
// Read init parameters from [Link]
ServletConfig config = getServletConfig();
String driver =
[Link]("drivername");
String dbUrl = [Link]("url");
String dbUser =
[Link]("username");
String dbPassword =
[Link]("password");
// Load JDBC driver
[Link](driver);
// Establish connection
conn = [Link](dbUrl, dbUser,
dbPassword);
[Link]("Connection created in
init()");
} catch (Exception e) {
throw new ServletException("Database connection
failed: " + [Link]());
}
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String username = [Link]("username");
String password = [Link]("password");
[Link]("text/html");
PrintWriter out = [Link]();
try {
// Prepare SQL query
String query = "SELECT COUNT(*) FROM users WHERE
username = ? AND password = ?";
PreparedStatement ps =
[Link](query);
[Link](1, username);
[Link](2, password);
ResultSet rs = [Link]();
[Link]();
int count = [Link](1);
if (count == 1) {
[Link]("<html><body>");
[Link]("<h2>Welcome, " + username +
"!</h2>");
[Link]("</body></html>");
} else {
[Link]("<html><body>");
[Link]("<h2 style='color:red;'>Incorrect
username or password.</h2>");
[Link]("<p><a href='[Link]'>Try
again</a></p>");
[Link]("</body></html>");
}
} catch (SQLException e) {
[Link]("<h2>Database error: " +
[Link]() + "</h2>");
}
}
public void destroy() {
// Close DB connection
try {
if (conn != null && ![Link]()) {
[Link]();
[Link]("Database connection closed
in destroy()");
}
} catch (SQLException e) {
[Link]("Error closing DB connection: "
+ [Link]());
}
}
}
Compilation:
Javac [Link]