0% found this document useful (0 votes)
2 views5 pages

Advance Java 15 Practical Programs

The document provides a series of Java programming examples demonstrating various concepts such as Servlets, JSP, JDBC, Struts, Hibernate, Spring, and Android development. Each example includes the object of the program, the code snippet, and the expected output. The examples cover basic functionalities like displaying messages, handling form data, session tracking, CRUD operations, and implementing design patterns.

Uploaded by

raovik713
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Advance Java 15 Practical Programs

The document provides a series of Java programming examples demonstrating various concepts such as Servlets, JSP, JDBC, Struts, Hibernate, Spring, and Android development. Each example includes the object of the program, the code snippet, and the expected output. The examples cover basic functionalities like displaying messages, handling form data, session tracking, CRUD operations, and implementing design patterns.

Uploaded by

raovik713
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Basic Servlet "Hello World"


Object: To create a simple Servlet that displays a welcome message.
Program:
import [Link].*;
import [Link].*;
import [Link].*;
public class HelloServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<html><body><h1>Hello from Advance Java
Servlet!</h1></body></html>");
}
}
Output: A web page displaying: Hello from Advance Java Servlet!

2. Handling Form Data (Servlet Request)


Object: To read user input from an HTML form using HttpServletRequest.
Program:
public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException
{
String name = [Link]("userName");
PrintWriter out = [Link]();
[Link]("Welcome, " + name);
}
Output: If user enters "John", the browser displays: Welcome, John
3. Session Tracking using Cookies
Object: To demonstrate state management in Servlets using Cookies.
Program:
Cookie ck = new Cookie("user", "Gemini");
[Link](ck);
// To retrieve
Cookie[] cookies = [Link]();
[Link]("Cookie Value: " + cookies[0].getValue());
Output: Cookie Value: Gemini

4. JSP Scripting Elements


Object: To use scriptlets, expressions, and declarations in a JSP page.
Program:
<%! int count = 0; %>
<html>
<body>
Current Date: <%= new [Link]() %>
<% count++; %>
<br>Page Hits: <%= count %>
</body>
</html>
Output: Displays the current system date and increments the hit counter on every refresh.
5. JSP Implicit Objects
Object: To use the request and session implicit objects in JSP.
Program:
<%
String id = [Link]();
String userAgent = [Link]("User-Agent");
%>
Session ID: <%= id %> <br>
Browser: <%= userAgent %>
Output: Displays the unique Session ID and the user's Browser details.

6. CRUD Operation using JDBC in Servlet


Object: To insert data into a MySQL database via a Servlet.
Program:
Connection con = [Link]("jdbc:mysql://localhost:3306/db", "root",
"pass");
PreparedStatement ps = [Link]("insert into students values(?,?)");
[Link](1, 101);
[Link](2, "Alice");
int i = [Link]();
[Link](i + " record inserted");
Output: 1 record inserted

7. Struts 2 "Action" Class


Object: To create a basic Struts 2 application with an Action class.
Program:
public class WelcomeAction {
public String execute() {
return "success";
}}
(Requires [Link] mapping)
Output: Forwards the user to [Link].
8. Sending Email via Java Mail API
Object: To send a simple text email using GMail SMTP.
Program:
[Link](message);
[Link]("Message sent successfully");
Output: Console displays: Message sent successfully

9. Hibernate (HB) Configuration & Persistence


Object: To save an object into the database using Hibernate.
Program:
Session session = [Link]();
Transaction t = [Link]();
Employee e1 = new Employee(1, "Bob");
[Link](e1);
[Link]();
Output: A new row is added to the Employee table in the database.

10. Hibernate Query Language (HQL)


Object: To fetch records from a table using HQL.
Program:
Query query = [Link]("from Employee where id=1");
List list = [Link]();
[Link]([Link](0));
Output: Displays the details of the Employee with ID 1.

11. Spring Dependency Injection (Setter Injection)


Object: To demonstrate IoC (Inversion of Control) using Spring.
Program:XML
<bean id="emp" class="[Link]">
<property name="name" value="Sarah" />
</bean>
Output: The Employee object is initialized with the name "Sarah" by the Spring Container.

12. Spring MVC "Hello World"


Object: To create a controller that returns a view name.
Program:
@Controller
public class HelloController {
@RequestMapping("/hello")
public String redirect() {
return "viewpage";
}}
Output: Accessing /hello loads [Link].
13. Spring Boot Starter App
Object: To create a REST endpoint using Spring Boot.
Program:
@RestController
@SpringBootApplication
public class Demo {
@GetMapping("/")
public String home() { return "Spring Boot is running!"; }
public static void main(String[] args) { [Link]([Link], args); }
}
Output: Browser displays: Spring Boot is running!

14. Android "Toast" Message


Object: To display a popup message in an Android activity.
Program:
public void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
[Link](this, "Welcome to Android", Toast.LENGTH_SHORT).show();
}
Output: A brief popup at the bottom of the screen saying "Welcome to Android".

15. Singleton Design Pattern


Object: To implement a Creational Design Pattern in Java.
Program:
class Singleton {
private static Singleton obj = new Singleton();
private Singleton() {}
public static Singleton getInstance() { return obj; }
}
Output: Ensures only one instance of the class is created throughout the application.

You might also like