0% found this document useful (0 votes)
3 views39 pages

Java

The document outlines various Java programming tasks, including configuring Apache Tomcat in Eclipse, implementing session tracking techniques using JSP, and creating a JDBC application with JSP and Servlets. It also covers the use of Struts for creating web applications and demonstrates Hibernate Query Language (HQL) for database interactions. Each program includes code snippets and descriptions of their functionality.

Uploaded by

vani78987
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)
3 views39 pages

Java

The document outlines various Java programming tasks, including configuring Apache Tomcat in Eclipse, implementing session tracking techniques using JSP, and creating a JDBC application with JSP and Servlets. It also covers the use of Struts for creating web applications and demonstrates Hibernate Query Language (HQL) for database interactions. Each program includes code snippets and descriptions of their functionality.

Uploaded by

vani78987
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

PROGRAM - 1

Program 1:- Write a program to configure the Apache Tomcat in Eclipse.


Steps:
Add Apache Tomcat to
Eclipse Go to: Window →
Preferences
Navigate to: Server → Runtime Environments
Click Add
Choose Apache Tomcat v9.0 (or the version you downloaded)
Click Next
Browse to your Tomcat installation directory
Click Finish
OUTPUT :

NEHA CHAUDHARY 233155


PROGRAM - 2
Program 2:- Write a program to create URL-Rewriting, Cookies, Http
Session, and Hidden Field.
CODE:
[Link]

<html>
<head>
<title>Session Tracking Techniques</title>
</head>
<body>

<h2>Session Tracking Methods</h2>

<a href="[Link]">URL Rewriting</a><br><br>


<a href="[Link]">Cookies</a><br><br>
<a href="[Link]">Http Session</a><br><br>
<a href="[Link]">Hidden Field</a>

</body>
</html>

[Link]

<html>
<body>

<h3>URL Rewriting - Enter Name</h3>

<form action="[Link]" method="get">


Name: <input type="text" name="uname">
<input type="submit" value="Submit">
</form>

</body>
</html>

OUTPUT:

NEHA CHAUDHARY 233155


[Link]

<%
String name = [Link]("uname");
if(name == null) name = "Guest";
%>

<html>
<body>

<h3>Welcome <%=name%></h3>

<a href="[Link]?uname=<%=name%>">Click to Continue</a>

</body>
</html>

OUTPUT:

[Link]

<%@ page import="[Link]" %>

<%
String name = [Link]("uname");

if(name != null && ![Link]()){


String encodedName = [Link](name, "UTF-8");

Cookie c = new Cookie("username", encodedName);


[Link](60*60);
[Link](c);
}
%>

<html>
<body>

<h3>Enter Name (Cookie)</h3>

NEHA CHAUDHARY 233155


<form action="[Link]" method="post">

Name: <input type="text" name="uname" required>


<input type="submit" value="Save">
</form>

<br>
<a href="[Link]">Go to Next Page</a>

</body>
</html>

OUTPUT:

[Link]

<%@ page import="[Link]" %>

<%
String name = "No Cookie Found";

Cookie[] cookies = [Link]();

if(cookies != null){
for(Cookie c : cookies){
if("username".equals([Link]())){
name = [Link]([Link](), "UTF-8"); // FIX HERE
}
}
}
%>
<html>
<body>

<h3>Name stored in Cookie: <%=name%></h3>

NEHA CHAUDHARY 233155


</body>
</html>

OUTPUT:

[Link]

<html>
<head>
<title>Session Login</title>
</head>
<body>

<h3>Login Page (Http Session)</h3>


<form action="[Link]" method="post">
Enter your name:
<input type="text" name="uname" required><br><br>

Roll no.:
<input type="text" name="roll" required><br><br>

<input type="submit" value="LOGIN">


</form>

</body>
</html>

OUTPUT:

NEHA CHAUDHARY 233155


[Link]

<%
String name = [Link]("uname");
String roll = [Link]("roll");

if(name != null && roll != null){


[Link]("username", name);
[Link]("rollno", roll);
}
%>

<html>
<head>

<title>Session Output</title>
</head>
<body>

<h2>SESSION CREATED SUCCESSFULLY</h2>

<h3>
Welcome <%= [Link]("username") %> !
</h3>

<h3>
Your roll no. is : <%= [Link]("rollno") %>
</h3>

</body>
</html>

OUTPUT:

NEHA CHAUDHARY 233155


[Link]

<html>
<head>
<title>Student Form</title>
</head>
<body>

<h2>Student Information Form</h2>


<p>Please enter details carefully !</p>

<form action="[Link]" method="post">

Name :
<input type="text" name="uname" required><br><br>

Email :
<input type="email" name="email" required><br><br>

<input type="submit" value="SAVE">

</form>

</body>
</html>

OUTPUT:

NEHA CHAUDHARY 233155


[Link]

<%
String name = [Link]("uname");
String email = [Link]("email");

if(name == null) name = "Guest";


if(email == null) email = "";
%>

<html>
<head>
<title>Hidden Field Output</title>
</head>
<body>

<h3>
Welcome VIA Hidden Field, <%= name %>
</h3>

<form action="[Link]" method="post">


<input type="hidden" name="uname" value="<%= name %>">
<input type="hidden" name="email" value="<%= email %>">

<input type="submit" value="Continue">


</form>

</body>
</html>

OUTPUT:

NEHA CHAUDHARY 233155


PROGRAM 3

Program 3:- Write a program to create JSP with JSTL.

CODE :

Add jstl - [Link] to the lib

[Link]

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>


<%-- Essential JSTL Imports --%>

<%@ taglib uri="[Link] prefix="c" %>


<%@ taglib uri="[Link] prefix="fn" %>

<!DOCTYPE html>
<html>
<head>
<title>JSTL Program Results</title>
</head>
<body>

<h1>JSTL Program Results</h1>


<h3>Names List:</h3>

<ul>

<%--
We use ONLY the comma as a delimiter.
This ensures 'Vani Sharma' stays together as one item.
--%>

<c:forTokens items="${[Link]}" delims="," var="individualName">


<li>

<%-- fn:trim removes the extra space after the comma --%>
<c:out value="${fn:trim(individualName)}" />
</li>

</c:forTokens>
</ul>

<br>

<a href="[Link]">Go Back</a>

</body>
</html>

NEHA CHAUDHARY 233155


[Link]

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>
<head>
<title>Input Names</title>
</head>

<body>

<h2>Enter Names for the List</h2>


<form action="[Link]" method="POST">

<p>Type names separated by commas (e.g. Vani Sharma, Aditya Yadav):</p>

<textarea name="userNames" rows="5" cols="40">Vani Sharma, Aditya Yadav, Shruti


Kumari</textarea>

<br>
<br>

<input type="submit" value="Display List">

</form>

</body>
</html>

NEHA CHAUDHARY 233155


OUTPUT :

NEHA CHAUDHARY 233155


PROGRAM 4

Program 4:- Write a program to analyse the use of JDBC with JSP andServlet.

CODE :

[Link] package

[Link]

package [Link];

import [Link].*;

public class DBConnection {

public static Connection getConnection() {

Connection con = null;

try {
[Link]("[Link]");

con = [Link](

"jdbc:mysql://localhost:3306/student_db?useSSL=false&allowPublicKeyRetrieval=true&serverTimezo
ne=UTC",
"root",
"vani@233111"
);

[Link](" DATABASE CONNECTED");

} catch (Exception e) {
[Link](" DATABASE ERROR:");
[Link]();
}

return con;
}
}

NEHA CHAUDHARY 233155


[Link] package

[Link]

package [Link];

import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link];

public class StudentServlet extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {

String name = [Link]("name");


String email = [Link]("email");

try {

Connection con = [Link]();

PreparedStatement ps = [Link](
"INSERT INTO students(name,email) VALUES (?,?)"
);

[Link](1, name);
[Link](2, email);

int i = [Link]();

if(i > 0) {
[Link]([Link]() + "/[Link]");

} else {
[Link]().println("Insert failed");
}

if(con == null) {
[Link]().println(" Connection FAILED");
return;
} else {
[Link]().println(" Connection SUCCESS");
return;
}

} catch(Exception e) {
[Link]("text/html");
PrintWriter out = [Link]();

NEHA CHAUDHARY 233155


[Link]("<h2>Error: " + [Link]() + "</h2>");
[Link]();

}
}
}

[Link]

<%@ page import="[Link].*" %>

<html>
<body>

<h2>Student List</h2>

<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>

<%
try {
[Link]("[Link]");
Connection con = [Link](
"jdbc:mysql://localhost:3306/student_db",
"root",
"password"
);

Statement stmt = [Link]();


ResultSet rs = [Link]("SELECT * FROM students");

while([Link]()) {
%>

<tr>
<td><%= [Link]("id") %></td>
<td><%= [Link]("name") %></td>
<td><%= [Link]("email") %></td>
</tr>

<%
}
} catch(Exception e) {
[Link](e);
}
%>

NEHA CHAUDHARY 233155


</table>
</body>
</html>

[Link]

<html>
<body>

<h2>Student Information Form</h2>


<form action="StudentServlet" method="post">
Name: <input type="text" name="name"><br><br>
Email: <input type="text" name="email"><br><br>
<input type="submit" value="Save">
</form>

</body>
</html>

[Link]

<%@ page language="java" contentType="text/html; charset=UTF-8"


pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>Student information saved successfully!</h2>
<a href="[Link]">Add New Student</a>

</body>
</html>

[Link]

<web-app>
<servlet>
<servlet-name>StudentServlet</servlet-name>
<servlet-class>[Link]</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>StudentServlet</servlet-name>
<url-pattern>/StudentServlet</url-pattern>
</servlet-mapping>

</web-app>
NEHA CHAUDHARY 233155
OUTPUT:

NEHA CHAUDHARY 233155


PROGRAM 5

Program 5: Write a program to create simple Struts.

CODE :

[Link]

public class HelloAction extends ActionSupport {

private String message = "Struts2 is awesome!";


public String getMessage()

{
return message;
}

@Override
public String execute()
{
return SUCCESS;

}
}

[Link]
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Hello</title>
<link rel="stylesheet" type="text/css" href="css/[Link]">
</head>
<body>
<div class="container">
<h2>Hello from Struts2!</h2>
<p> This response is coming from the <strong>HelloAction</strong> Java class using Struts
2 MVC.</p>
< You can use Struts 2 tags to access data sent from the action class:</p>
<p><strong>Example (no dynamic value yet):</strong></p>
<p>Hello, <s:property value="'World'" />!</p>
<hr>
<p>⬛ Want to go back to home page?</p>
<a href="[Link]" class="btn">Go to Home</a>
</div>
</body>
</html>

NEHA CHAUDHARY 233155


OUTPUT :

NEHA CHAUDHARY 233155


PROGRAM 6

Program 6:- Write a program to create Validation with Struts.

CODE :

[Link]

import [Link];

public class UserAction extends ActionSupport

{
private String name;
private int age;

// Getters and Setters


public String getName()

{
return name;
}
public void setName(String name)
{
[Link] = name;

}
public int getAge()
{
return age;

}
public void setAge(int age)
{
[Link] = age;
}

@Override
public String execute()
{
return SUCCESS;
}

@Override
public void validate()
{

NEHA CHAUDHARY 233155


if (name == null || [Link]().isEmpty())
{
addFieldError("name", "Name is required.");

}
if (age <= 0) {

addFieldError("age", "Please enter a valid age.");


}
}
}

[Link]

<%@ taglib uri="/struts-tags" prefix="s" %>


<html>
<head>
<title>User Form</title>
</head>
<body>
<h2>Enter User Details</h2>

<s:form action="user">
<s:textfield name="name" label="Name"/>
<s:textfield name="age" label="Age"/>
<s:submit value="Submit"/>

</s:form>
</body>
</html>

[Link]

<%@ taglib uri="/struts-tags" prefix="s" %>


<html>
<head>
<title>Success</title>
</head>
<body>
<h2>Form submitted successfully!</h2>
<p><strong>Name:</strong> <s:property value="name" /></p>
<p><strong>Age:</strong> <s:property value="age" /></p>
</body>
</html>

NEHA CHAUDHARY 233155


OUTPUT :

NEHA CHAUDHARY 233155


PROGRAM - 7

Program 7:- Write a program to implement HQL .

CODE :

[Link] package

[Link]

package [Link];

/**
* Hello world!
*/
public class App {
public static void main(String[] args) {
[Link]("Hello World!");
}
}

[Link]

package [Link];

import [Link];
import [Link];

public class HibernateUtil {

private static final SessionFactory factory;

static {
factory = new Configuration().configure().buildSessionFactory();
}

public static SessionFactory getSessionFactory() {


return factory;
}
}

[Link]

package [Link];

import [Link];
import [Link];

NEHA CHAUDHARY 233155


public class HQLDemo {

public static void main(String[] args) {

Session session = [Link]().openSession();


Transaction tx = [Link]();

// insert fixed data


[Link](new User("Neha chaudhary"));
[Link](new User("Urvashi raheja"));
[Link](new User("Kritika verma"));

[Link]();
[Link]();

[Link]("Data inserted successfully!");


}
}

[Link]

package [Link];

import [Link].*;

@Entity
@Table(name="users")
public class User {

@Id
@GeneratedValue(strategy = [Link])
private int id;

private String name;

public User() {}

public User(String name) {


[Link] = name;
}

public String getName() {


return name;
}
}

NEHA CHAUDHARY 233155


[Link]

<hibernate-configuration>
<session-factory>

<property name="[Link].driver_class">[Link]</property>
<property
name="[Link]">jdbc:mysql://localhost:3306/hibernate_HQL</property>
<property name="[Link]">root</property>
<property name="[Link]">neha@233155</property>

<property name="[Link]">[Link].MySQL8Dialect</property>

<property name="[Link]">update</property>
<property name="show_sql">true</property>

<mapping class="[Link]"/>

</session-factory>
</hibernate-configuration>

[Link](dependencies)

<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>hibernate-core</artifactId>
<version>[Link]</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>

<dependency>
<groupId>[Link]</groupId>
<artifactId>[Link]-api</artifactId>
<version>2.2</version>
</dependency>
</dependencies>

NEHA CHAUDHARY 233155


OUTPUT:

NEHA CHAUDHARY 233155


PROGRAM - 8

Program 8:- Write a program to implement One-to-Many Mapping.

CODE :

inside package

[Link]

package [Link];

/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
[Link]( "Hello World!" );
}
}

[Link]

package [Link];

import [Link].*;
import [Link].*;

@Entity
@Table(name="department")
public class Department {

@Id
@GeneratedValue
private int id;

private String name;


@OneToMany(mappedBy="department", cascade=[Link])
private List<Employee> employees = new ArrayList<>();

public int getId() { return id; }

public String getName() { return name; }

public void setName(String name) { [Link] = name; }

public List<Employee> getEmployees() { return employees; }


}

NEHA CHAUDHARY 233155


[Link]

package [Link];

import [Link].*;

@Entity
@Table(name="employee")
public class Employee {

@Id
@GeneratedValue
private int id;

private String name;

@ManyToOne
@JoinColumn(name="department_id")
private Department department;

public void setName(String name) { [Link] = name; }


public void setDepartment(Department department) {
[Link] = department;
}
}

[Link]

package [Link];

import [Link].*;
import [Link];

public class MainApp {

public static void main(String[] args) {

Session session = new Configuration()


.configure()
.buildSessionFactory()
.openSession();

Transaction tx = [Link]();

Department dept = new Department();


[Link]("IT");

Employee e1 = new Employee();


[Link]("Neha chaudhary");
[Link](dept);

NEHA CHAUDHARY 233155


Employee e2 = new Employee();
[Link]("Arun kumar");
[Link](dept);

Employee e3 = new Employee();


[Link]("Vivek dhiman");
[Link](dept);

[Link]().add(e1);
[Link]().add(e2);
[Link]().add(e3);

[Link](dept);

[Link]();
[Link]();

[Link]("One-to-Many mapping done successfully!");


}
}

OUTPUT:

NEHA CHAUDHARY 233155


PROGRAM - 9

Program 9:- Write a program to implement Named Query.

CODE :

[Link] package

[Link]

package [Link];

import [Link];
import [Link];

import [Link];
import [Link];

public class MainApp {

public static void main(String[] args) {

Session session = [Link]().openSession();


[Link]();

// Insert data
Employee e1 = new Employee();
[Link]("Neha");
[Link](50000);

Employee e2 = new Employee();


[Link]("Chakshit");
[Link](30000);

Employee e3 = new Employee();


[Link]("Riya");
[Link](40000);

[Link](e1);
[Link](e2);
[Link](e3);

[Link]().commit();

// Fetch data
List<Employee> list = session

.createQuery("from Employee", [Link])

NEHA CHAUDHARY 233155


.list();

for (Employee e : list) {


[Link]("ID: " + [Link]()

+ ", Name: " + [Link]()


+ ", Salary: " + [Link]());
}

[Link]();

}
}

[Link] package

package [Link];

import [Link].*;

@Entity
@Table(name="Employee")

public class Employee {

@Id

@GeneratedValue(strategy = [Link])

private int id;

private String name;


private double salary;

// getters and setters


public int getId() { return id;
}
public void setId(int id) { [Link] = id;
}

public String getName() { return name;


}
public void setName(String name) { [Link] = name;
}

public double getSalary() {

NEHA CHAUDHARY 233155


return salary;
}

public void setSalary(double salary) { [Link] = salary; }


}

[Link] package

[Link]

package [Link];

import [Link];
import [Link];

public class HibernateUtil {

private static final SessionFactory factory;

static {
factory = new Configuration().configure().buildSessionFactory();
}

public static SessionFactory getSessionFactory() {


return factory;
}
}

NEHA CHAUDHARY 233155


OUTPUT:

NEHA CHAUDHARY 233155


PROGRAM - 10

Program 10:- Write a program to create a Login Page using Spring MVC and ORM

CODE :

[Link] package

[Link]

package [Link];

import [Link];
import [Link];
import [Link].*;

@Controller
public class LoginController {

@RequestMapping("/")
public String showLogin() {
return "login";
}

@PostMapping("/login")
public String login(@RequestParam("name") String name,
@RequestParam("rollno") String rollno,
Model model) {

[Link]("name", name);
[Link]("rollno", rollno);

return "success";
}
}

[Link]

<html>
<body>

<h2>Login Page</h2>

<form action="login" method="post">


Enter Name: <input type="text" name="name"/><br><br>
Roll No: <input type="text" name="rollno"/><br><br>

NEHA CHAUDHARY 233155


<input type="submit" value="Login"/>
</form>

</body>
</html>

[Link]

<html>
<body>

<h2>WELCOME</h2>

Name: ${name} <br>


Roll No: ${rollno}

</body>
</html>

[Link]

<beans xmlns="[Link]
xmlns:context="[Link]
xmlns:mvc="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]

<context:component-scan base-package="[Link]"/>

<mvc:annotation-driven/>

<bean class="[Link]">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>

</beans>

NEHA CHAUDHARY 233155


OUTPUT:

NEHA CHAUDHARY 233155


PROGRAM - 11

Program 11:- Write a program to create a Spring Boot.

CODE :

[Link] package

package [Link];

import [Link];
import [Link];

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {


[Link]([Link], args);
}
}

[Link]

package [Link];

import [Link];
import [Link];

@RestController
public class HelloController {

@GetMapping("/")
public String hello() {
return "Hello from Spring Boot! ";
}
}

NEHA CHAUDHARY 233155


OUTPUT:

NEHA CHAUDHARY 233155


PROGRAM - 12

Program 12:- Write a program to design a Login Page using Android.

CODE:

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent" android:gravity="center_vertical" android:orientation="vertical"
android:padding="16dp">
<!-- Username EditText -->

<EditText android:id="@+id/editTextUsername" android:layout_width="match_parent"


android:layout_height="60dp" android:hint="Username" android:inputType="text" />

<!-- Password EditText -->


<EditText
android:id="@+id/editTextPassword" android:layout_width="match_parent" android:layout_height="60dp"
android:hint="Password" android:inputType="textPassword" />

<!-- Login Button -->


<Button android:id="@+id/buttonLogin"
android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Login" />

</LinearLayout>
1. Adding Functionality:
package [Link];
import [Link]; import [Link];

import [Link]; import [Link]; import [Link];

import [Link]; public class MainActivity extends AppCompatActivity {

private EditText editTextUsername, editTextPassword;

private Button buttonLogin;

@Override
protected void onCreate(Bundle savedInstanceState) { [Link](savedInstanceState);
setContentView([Link].activity_main);
// Initialize UI elements

editTextUsername = findViewById([Link]); editTextPassword =


findViewById([Link]); buttonLogin = findViewById([Link]);

// Set a click listener for the login button

[Link](new [Link]() {
@Override public void onClick(View view) {
// Retrieve entered username and password

NEHA CHAUDHARY 233155


String username = [Link]().toString(); String password =
[Link]().toString();
// Implement authentication logic here

if ([Link]("Admin") && [Link]("123")) {


// Successful login
[Link]([Link], "Login successful", Toast.LENGTH_SHORT).show();
} else {
// Failed login
[Link]([Link], "Invalid username or password", Toast.LENGTH_SHORT).show();
}
}
});
}
}
Output:-

NEHA CHAUDHARY 233155

You might also like