Program 8
Program 8
Laboratory
1. Set Up Environment:
2. Create Project:
Open Eclipse.
File > New > Dynamic Web Project.
Name the project Program8 > Next > Next > Check web.xml deployment descriptor >
Finish
3. Create Servlet:
Java Resources > src/main/java -> Right-click -> New > Servlet.
Name the servlet GreetingServlet
Name the Java package & Class name > Next > Next > Check doPost & doGet (if
required in the program) > Finish
Place the generated code in the provided GreetingServlet.java File.
4. Create HTML:
main > webapp -> Right-click -> New > HTML File.
Name the File Name > Next > Finish
Place the generated code in the provided html File.
5. Configure web.xml:
6. Deploy Application:
Choose Apache Tomcat > Next > add the project > Finish.
This will guide you through creating a servlet that displays a greeting message and
accepts a username from the client.
Reference: https://www.javatpoint.com/creating-servlet-in-eclipse-ide
8. A program to display greeting message on the browser “Hello User Name”, “How
Are You?”, accept username from the client using servlet.
Program:
Servlet File:
package javaprogram;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
public class GreetingServlet extends HttpServlet {
public GreetingServlet() {
super();
// TODO Auto-generated constructor stub
}
web.xml File:
<servlet>
<servlet-name>GreetingServlet</servlet-name>
<servlet-class>javaprogram.GreetingServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GreetingServlet</servlet-name>
<url-pattern>/greet</url-pattern>
</servlet-mapping>
His configuration maps the servlet to the URL pattern "/greet", so when a client sends a
request to "/greet" with a parameter "username", the servlet will handle it.
To test the servlet, you can create a simple HTML form that submits the username to the
servlet:
HTML File:
<!DOCTYPE html>
<html>
<head>
<title>Greeting Form</title>
</head>
<body>
<form action="greet" method="get">
Username: <input type="text" name="username">
<input type="submit" value="Submit">
</form>
</body>
</html>
Save this HTML code as "greeting_form.html" and deploy it along with the servlet.
When you open the HTML file in a browser, it will display a form where you can enter a
username. Upon submitting the form, it will send a GET request to the servlet, which will
generate the greeting message and display it on the browser.
Remember to deploy your servlet and HTML file properly in a servlet container like
Apache Tomcat.
OUTPUT:
9. A servlet program to display the name, USN, and total marks by accepting student
detail.
Servlet File:
package javaprogram;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
public StudentDetailsServlet() {
super();
// TODO Auto-generated constructor stub
}
response.getWriter().println("<h1>Student Details</h1>");
response.getWriter().println("<p>" + detailsMessage + "</p>");
response.getWriter().println("</body></html>");
}
}
web.xml File:
<servlet>
<description></description>
<display-name>StudentDetailsServlet</display-name>
<servlet-name>StudentDetailsServlet</servlet-name>
<servlet-class>javaprogram.StudentDetailsServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>StudentDetailsServlet</servlet-name>
<url-pattern>/studentDetails</url-pattern>
</servlet-mapping>
This configuration maps the servlet to the URL pattern "/studentDetails", so when a client
sends a request to "/studentDetails" with parameters "name", "usn", and "totalMarks", the
servlet will handle it.
To test the servlet, you can create a simple HTML form that submits the student details to
the servlet:
HTML File:
<!DOCTYPE html>
<html>
<head>
<title>Student Details Form</title>
</head>
<body>
<form action="studentDetails" method="post">
Name: <input type="text" name="name"><br>
USN: <input type="text" name="usn"><br>
Total Marks: <input type="text" name="totalMarks"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Save this HTML code as "student_details_form.html" and deploy it along with the
servlet. When you open the HTML file in a browser, it will display a form where you can
enter student details. Upon submitting the form, it will send a POST request to the
servlet, which will generate the student details message and display it on the browser.
Remember to deploy your servlet and HTML file properly in a servlet container like
Apache Tomcat.
OUTPUT:
10. A Java program to create and read the cookie for the given cookie name as
“EMPID” and its value as “AN2356”.
Program:
package javaprogram;
import jakarta.servlet.*;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.*;
@WebServlet("/SetCookie")
package javaprogram;
import jakarta.servlet.*;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.*;
@WebServlet("/ReadCookie")
public class ReadCookieServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
<servlet>
<description></description>
<servlet-name>SetCookieServlet</servlet-name>
<servlet-class>javaprogram.SetCookieServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SetCookieServlet</servlet-name>
<url-pattern>/SetCookieServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<servlet-name>ReadCookieServlet</servlet-name>
<servlet-class>javaprogram.ReadCookieServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ReadCookieServlet</servlet-name>
<url-pattern>/ReadCookieServlet</url-pattern>
</servlet-mapping>
OUTPUT:
11. Write a JAVA Program to insert data into Student DATA BASE and retrieve info
based on particular queries(For example update, delete, search etc…).
Program:
package javaprogram;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
// Open a connection
Connection conn = DriverManager.getConnection(JDBC_URL, USERNAME,
PASSWORD);
public static void insertData(Connection conn, String name, int age, String major)
throws SQLException {
String query = "INSERT INTO Student (name, age, major) VALUES (?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setString(1, name);
pstmt.setInt(2, age);
pstmt.setString(3, major);
pstmt.executeUpdate();
pstmt.close();
}
System.out.println("ID: " + id + ", Name: " + studentName + ", Age: " + age +
", Major: " + major);
}
rs.close();
pstmt.close();
}
}
//
USE studentdb;
//
use studentdb;
select * from student;
OUTPUT:
12. A program to design the Login page and validating the USER_ID and PASSWORD
using JSP and DataBase.
Department of ISE, BIT Page 17
BIS402 Advanced Java
Laboratory
Program:
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<h2>Login Page</h2>
<form action="validate.jsp" method="post">
User ID: <input type="text" name="user_id"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Login">
</form>
</body>
</html>
Create validate.jsp:
try {
// Register JDBC driver
Class.forName("com.mysql.cj.jdbc.Driver");
// Open a connection
Connection conn = DriverManager.getConnection(JDBC_URL, USERNAME,
PASSWORD);
// Close connections
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
out.println("<p>Error: " + e.getMessage() + "</p>");
}
%>
</body>
</html>
In the Servers view, right-click your Tomcat server and select Start.
Access the Application:
OUTPUT: