JAVA SLIP SETUP-1
JAVA SLIP SETUP-1
Eclipse IDE for Enterprise Java Developers To write and run Java EE apps
2. Go to Window → Preferences
4. ✔️
If you see "Apache Tomcat v9.0" (or any version) listed = Already configured
2. Click Add...
3. Select:
○ Click Next
4. Browse and select the Tomcat folder you extracted (e.g., C:\apache-tomcat-9.0.X)
5. Click Finish
2. Click "No servers are available. Click this link to create a new server..."
5. Click Finish
✅ Done! Now you can right-click → Run on Server to run servlet programs.
If you see output in browser, and Eclipse Console shows logs like:
arduino
CopyEdit
Starting Tomcat...
Server startup in XXXX ms
PROGRAM
SLIP 4B
B) Write a SERVLET program that provides information about a HTTP request from a
client, such as IP address and browser type. The servlet also provides information
about
the server on which the servlet is running, such as the operating system type, and the
names of currently loaded servlets.
🧱 Step 1: Prerequisites
Tool Needed?
5. Click Finish
3. Package: com.exam
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
// Client Info
String ip = request.getRemoteAddr();
String browser = request.getHeader("User-Agent");
// Server Info
String os = System.getProperty("os.name");
out.println("<html><body>");
out.println("<h2>Client Info</h2>");
out.println("<p>IP Address: " + ip + "</p>");
out.println("<p>Browser Type: " + browser + "</p>");
out.println("<h2>Server Info</h2>");
out.println("<p>Operating System: " + os + "</p>");
out.println("<p><strong>Loaded Servlets:</strong></p><ul>");
while (servlets.hasMoreElements()) {
out.println("<li>" + servlets.nextElement() + "</li>");
}
out.println("</ul>");
out.println("</body></html>");
}
}
<servlet>
<servlet-name>HttpInfoServlet</servlet-name>
<servlet-class>com.exam.HttpInfoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HttpInfoServlet</servlet-name>
<url-pattern>/httpinfo</url-pattern>
</servlet-mapping>
URL in browser:
bash
CopyEdit
http://localhost:8080/HttpInfoServletProject/httpinfo
3.
You’ll see IP, browser type, OS info, and loaded servlets. 🎉
✅ Libraries Required:
● No extra libraries needed — just Tomcat (comes with servlet-api.jar)
JSP SETUP:
✅ Project Setup
1. Create a Dynamic Web Project
○ Name: JSPShoppingMall
CopyEdit
shopping1.jsp
shopping2.jsp
bill.jsp
2.
🔹 Page 1: shopping1.jsp
jsp
CopyEdit
<%@ page language="java" contentType="text/html" %>
<%
String item1 = request.getParameter("item1");
String qty1 = request.getParameter("qty1");
🔹 Page 2: shopping2.jsp
jsp
CopyEdit
<%@ page language="java" contentType="text/html" %>
<%
String qty2 = request.getParameter("qty2");
if(qty2 != null){
int price2 = 50;
int total2 = Integer.parseInt(qty2) * price2;
session.setAttribute("total2", total2);
}
%>
🔹 Page 3: bill.jsp
jsp
CopyEdit
<%@ page language="java" contentType="text/html" %>
<%
Integer total1 = (Integer) session.getAttribute("total1");
Integer total2 = (Integer) session.getAttribute("total2");
if(total1 == null) total1 = 0;
if(total2 == null) total2 = 0;
<h2>Shopping Bill</h2>
<table border="1">
<tr><th>Item</th><th>Price</th></tr>
<tr><td>Electronics</td><td><%= total1 %></td></tr>
<tr><td>Books</td><td><%= total2 %></td></tr>
<tr><td><b>Total</b></td><td><b><%= grandTotal %></b></td></tr>
</table>
Step-by-step:
1. Right-click on your project → Run As → Run on Server
Start with:
bash
CopyEdit
http://localhost:8080/JSPShoppingMall/shopping1.jsp
2.
3. Enter quantity, go next, continue
JDBC SETUP:
sql
CopyEdit
CREATE TABLE Emp (
ENo INT PRIMARY KEY,
EName VARCHAR(50),
Sal DOUBLE
);
2. In Eclipse:
PROGRAM
SLIP 9A
Write a Java Program to create a Emp (ENo, EName, Sal) table and insert
record into it. (Use PreparedStatement Interface)
✅ Step-by-Step: How to Create a Java File in Eclipse and
Run It
○ Click Finish
4. Click Finish
String query = "INSERT INTO Emp (ENo, EName, Sal) VALUES (?, ?, ?)";
PreparedStatement ps = con.prepareStatement(query);
ps.setInt(1, eno);
ps.setString(2, ename);
ps.setDouble(3, sal);
if (result > 0)
System.out.println("Record inserted successfully!");
else
System.out.println("Insertion failed.");
con.close();
sc.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
○ Select mysql-connector-j-8.x.x.jar
SOCKET SETUP:
3. Click Finish
3. Click Finish
3. In the dialog:
○ Name: ChatServer (or whatever your class is)
4. Click Finish
public ChatServer() {
setTitle("Server Chat");
setSize(400, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
try {
ServerSocket serverSocket = new ServerSocket(1234);
Socket socket = serverSocket.accept();
chatArea.append("Client connected\n");
String line;
while ((line = in.readLine()) != null) {
chatArea.append("Client: " + line + "\n");
}
socket.close();
serverSocket.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public ChatClient() {
setTitle("Client Chat");
setSize(400, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
try {
Socket socket = new Socket("localhost", 1234);
chatArea.append("Connected to server\n");
String line;
while ((line = in.readLine()) != null) {
chatArea.append("Server: " + line + "\n");
}
socket.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
SLIP-2B
B) Write a java program in multithreading using applet for drawing
flag.
🔧 BEFORE YOU START:
// Top - Saffron
g.setColor(Color.ORANGE);
g.fillRect(110, y, 100, 20);
// Middle - White
g.setColor(Color.WHITE);
g.fillRect(110, y + 20, 100, 20);
// Bottom - Green
g.setColor(Color.GREEN);
g.fillRect(110, y + 40, 100, 20);
}
Task Tool
Want me to check if your system already has Java 8 or help switch if it doesn’t?
4o
You said:
in notepad i can create both files na
ChatGPT said:
Yes, absolutely! ✅
You can create both .java and .html files using Notepad. Here's how to do it step by
step:
java
CopyEdit
import java.applet.*;
import java.awt.*;
g.setColor(Color.ORANGE);
g.fillRect(110, y, 100, 20);
g.setColor(Color.WHITE);
g.fillRect(110, y + 20, 100, 20);
g.setColor(Color.GREEN);
g.fillRect(110, y + 40, 100, 20);
}
CopyEdit
FlagApplet.java
3. 🔸 Select "All Files" in the Save As Type dropdown
🔸 Add .java manually
🔸 Save it in a simple folder (e.g., C:\JavaApplet\)
html
CopyEdit
<html>
<body>
<applet code="FlagApplet.class" width="400" height="300"></applet>
</body>
</html>
2.
Save the file as:
CopyEdit
FlagApplet.html
3. 🔸 Again, choose "All Files"
🔸 Add .html manually
🔸 Save it in the same folder as your .java file (e.g., C:\JavaApplet\)
cd C:\JavaApplet
javac FlagApplet.java
appletviewer FlagApplet.html
HIBERNATE SETUP
SLIP-10A
Write a java Program in Hibernate to display “Hello world” message.
✅ Step 1: Download Required Files
4. Click Finish.
xml
CopyEdit
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- No real DB, just in-memory config -->
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
<property name="hibernate.connection.driver_class">org.h2.Driver</property>
<property name="hibernate.connection.url">jdbc:h2:mem:test</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password"></property>
</session-factory>
</hibernate-configuration>
✅ This is a dummy in-memory config for “Hello World” only — we don’t need a real
DB yet.
java
CopyEdit
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
// Create SessionFactory
SessionFactory factory = cfg.buildSessionFactory();
// Open Session
Session session = factory.openSession();
// Close resources
session.close();
factory.close();
}
}
csharp
CopyEdit
Hello World from Hibernate!