Advance Java Imp Question For Exam
Advance Java Imp Question For Exam
Web Application ek aisi software program hoti hai jo Internet ke through browser mein
chalti hai. Iska matlab tum ise Chrome, Firefox jaise browser mein open karke use kar sakte
ho bina kuch install kiye.
Example Use
3. Auto update hoti hai – User ko kuch update nahi karna padta.
3. Security ka risk hota hai – Agar sahi se secure na kiya gaya ho.
Definition (पररभाषा):
Dynamic Web Host ek aisa hosting environment hota hai jahan dynamic websites ko host
kiya jata hai — yaani ki aisi websites jinme content real-time mein update hota hai, aur
user ke action ke hisaab se badalta hai.
Yeh static websites se alag hoti hain jisme content fix hota hai. Dynamic web hosting server-
side scripting (jaise PHP, JSP, ASP.NET, Servlet, etc.) ko support karti hai.
Labh (Advantages):
• Content user ke action ke according update hota hai.
• Real-time data show karta hai (e.g., orders, messages, etc.)
• Database ke sath easily connect hota hai.
• Admin panel se content easily update kiya ja sakta hai.
Nuksan (Disadvantages):
• Static hosting se thoda costly hota hai.
• Zyada complex hota hai develop karne mein.
• Server pe zyada load padta hai.
• Hosting aur development dono mein technical knowledge chahiye.
Saar (Conclusion):
Dynamic Web Hosting un websites ke liye jaruri hai jahan par content baar-baar change
hota hai, aur users se interaction hota hai. Isme backend programming aur database
integration ka use hota hai.
IP Address (Internet Protocol Address) ek unique number hota hai jo kisi bhi device ko
internet ya local network par identify karne ke liye use hota hai.
Har device (jaise mobile, computer, server) jab internet se connect hoti hai, toh usse ek IP
address diya jata hai, jisse wo dusri devices ke saath data exchange kar sake.
Example:
CopyEdit
192.168.1.1
172.16.0.1
Note:
• Class A, B, C – Ye sab normal communication ke liye use hote hain.
• Class D – Multicast ke liye hota hai.
• Class E – Reserved hota hai, normal use ke liye nahi.
Port ek virtual point hota hai jahan se ek computer ya device dusre network device ke saath
communicate karta hai.
Ek IP address ke andar multiple ports ho sakte hain, jisse ek hi IP pe alag-alag services chal
sakti hain.
Example:
• Port 80 → HTTP (Website)
• Port 443 → HTTPS (Secure Website)
• Port 21 → FTP
• Port 25 → SMTP (Email sending)
Port Range:
• 0 to 1023 → Well-known ports (System services)
• 1024 to 49151 → Registered ports (User services)
• 49152 to 65535 → Dynamic/private ports (Temporary)
Saar (Conclusion):
• IP Address computer ka internet ID hota hai.
• IP ko 5 classes mein divide kiya gaya hai, har ek ka apna use hota hai.
• Ports help karte hain ek IP ke through multiple services chalane mein.
Ye specially JSP (Java Server Pages) aur Servlets ko support karta hai. Iska development
Apache Software Foundation ne kiya tha.
Saar (Summary):
Tomcat ek Java-based web server hai jo Servlet aur JSP ko run karta hai. Ye Java web apps
develop aur test karne ke लिए lightweight aur simple solution hai.
JDBC (Java Database Connectivity) ek Java API hai, jo Java applications ko relational
databases (jaise MySQL, Oracle, SQL Server, etc.) ke saath interact karne ka tareeka provide
karta hai. JDBC ka use database connection, data retrieval, data manipulation, aur
transaction management ke liye hota hai.
JDBC ka main purpose hai Java program ko database se connect karna, queries chalana, aur
results ko handle karna.
1. DriverManager: Yeh class hai jo JDBC driver ko load karti hai aur appropriate driver
ko select karti hai.
2. Connection: Isse Java program aur database ke beech connection establish hota hai.
3. Statement: SQL queries ko database me execute karne ke liye use hota hai.
4. ResultSet: Database se data ko retrieve karne ke baad result ko store karta hai.
5. SQLException: Errors ko handle karne ke liye exception class hoti hai.
JDBC driver ek software component hota hai jo Java application ko database ke saath
connect karne mein madad karta hai. JDBC driver database ke specific implementation ke
hisaab se different hote hain. Yeh driver Java program aur database ke beech
communication enable karta hai.
JDBC Driver के Types (Types of JDBC Drivers):
1. Complex Error Handling: SQL exceptions ko handle karna complicated ho sakta hai,
specially large systems mein.
2. Performance Issues: Agar driver properly optimized na ho, toh performance issues
ho sakte hain.
3. Database-specific Code: Kuch drivers database-specific hote hain, jo portability ko
limit karte hain.
4. Memory Overhead: JDBC driver ka proper installation aur configuration memory
consume karta hai, jo large applications mein problem ban sakta hai.
Saar (Summary):
JDBC Java applications ko relational databases ke saath interact karne ki ability deta hai.
JDBC Driver ek essential component hai jo database se connection establish karta hai aur
queries ko execute karta hai. JDBC ka use data retrieval, insertion, update, aur deletion ke
liye hota hai. JDBC ka main advantage hai database ke saath direct communication, lekin
thode performance aur compatibility issues bhi ho sakte hain.
CRUD ek acronym hai jo Create, Read, Update, aur Delete operations ko represent karta
hai. Yeh operations kisi bhi relational database me data ko manage karne ke liye use hote
hain. JDBC mein in operations ko perform karne ke liye Statement aur PreparedStatement
ka use kiya jata hai.
1. Create
• Purpose: New record ko database mein insert karne ke liye.
• SQL Command: INSERT INTO
• Example:
java
CopyEdit
String query = "INSERT INTO Users (username, email) VALUES ('john',
'[email protected]')";
Statement stmt = connection.createStatement();
stmt.executeUpdate(query);
2. Read
• Purpose: Database se data read karne ke liye.
• SQL Command: SELECT
• Example:
java
CopyEdit
String query = "SELECT * FROM Users";
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
System.out.println("Username: " + rs.getString("username"));
}
3. Update
• Purpose: Existing record ko update karne ke liye.
• SQL Command: UPDATE
• Example:
java
CopyEdit
String query = "UPDATE Users SET email = '[email protected]' WHERE username =
'john'";
Statement stmt = connection.createStatement();
stmt.executeUpdate(query);
4. Delete
• Purpose: Database se record ko delete karne ke liye.
• SQL Command: DELETE
• Example:
java
CopyEdit
String query = "DELETE FROM Users WHERE username = 'john'";
Statement stmt = connection.createStatement();
stmt.executeUpdate(query);
JDBC mein 3 tarah ke statements hote hain jo SQL queries ko execute karte hain:
1. Statement
o Purpose: Yeh simple SQL queries execute karne ke liye use hota hai.
o Use Case: Jab SQL query static hoti hai (jo compile time pe fix hoti hai).
o Example:
java
CopyEdit
Statement stmt = connection.createStatement();
String query = "SELECT * FROM Users";
ResultSet rs = stmt.executeQuery(query);
2. PreparedStatement
o Purpose: Yeh precompiled SQL queries execute karne ke liye use hota hai.
o Use Case: Jab SQL query dynamic ho aur usme parameters (variables) use ho
rahe ho.
o Advantage: SQL injection attacks se bachata hai aur performance ko improve
karta hai.
o Example:
java
CopyEdit
String query = "INSERT INTO Users (username, email) VALUES (?, ?)";
PreparedStatement pstmt = connection.prepareStatement(query);
pstmt.setString(1, "john");
pstmt.setString(2, "[email protected]");
pstmt.executeUpdate();
3. CallableStatement
o Purpose: Yeh stored procedures ko execute karne ke liye use hota hai.
o Use Case: Jab aapko database ke stored procedures ko call karna ho.
o Example:
java
CopyEdit
String query = "{call insertUser(?, ?)}";
CallableStatement cstmt = connection.prepareCall(query);
cstmt.setString(1, "john");
cstmt.setString(2, "[email protected]");
cstmt.execute();
Saar (Summary):
• JDBC Architecture ek multi-layer architecture hai jo Java application ko database ke
saath interact karne ki suvidha deta hai.
• CRUD operations (Create, Read, Update, Delete) ko JDBC ke through perform kiya
jata hai jo relational databases ke saath interaction ko simplify karta hai.
• JDBC Statements (Statement, PreparedStatement, aur CallableStatement) alag-alag
scenarios mein SQL queries ko execute karne ke liye use hote hain.
PreparedStatement aur CallableStatement zyada secure aur efficient hote hain.
Servlets web applications िे backend िा हहस्सा होते हैं और ये Java EE (Enterprise Edition) िे
part होते हैं। जब भी हम dynamic content (जैसे database से data fetch िरना, form data
process िरना, etc.) web page पर हदखाना चाहते हैं, तो हम servlet िा उपयोग िरते हैं।
• Server-side technology: Servlet server par execute hota hai, iska matlab hai ki yeh
client ke machine pe nahi, balki web server pe run karta hai.
• Platform independent: Java ke inherent feature ki wajah se servlet platform
independent hota hai, yani yeh kisi bhi operating system par run kar sakta hai jahan
Java Virtual Machine (JVM) install ho.
• Fast performance: Servlet ka performance fast hota hai kyunki yeh server ke andar
directly execute hota hai, bina kisi external resource ke.
• Persistence: Servlet ka object create karte waqt ek persistent connection hota hai, jo
web server ke saath connected rehta hai, aur client se multiple requests handle kar
sakta hai.
• Extensibility: Servlet ko extend kiya ja sakta hai aur custom functionality add ki ja
sakti hai, jaise request handling, sessions, etc.
Servlet ka primary function yeh hai ki yeh client se aayi request ko handle karta hai aur
response ko process kar ke client tak bhejta hai. Iska structure kuch is tarah hota hai:
1. Request handling: Client se jo bhi request aati hai, us request ko servlet process karta
hai.
2. Business Logic: Servlet apne business logic ko execute karta hai, jaise database se
data fetch karna, data validate karna, etc.
3. Response generation: Process hone ke baad servlet output ko HTTP response ke roop
mein client tak bhejta hai, jise browser display karta hai.
Servlet ka life cycle kuch stages mein divide hota hai. Ye stages hain:
1. Loading and Instantiation: Jab bhi client servlet ko request karta hai, to servlet
container servlet class ko load karta hai aur uska object instantiate karta hai.
2. Initialization (init() method): Servlet ke object ke create hone ke baad init() method
call hota hai. Yeh method ek baar servlet ke initialization ke dauran execute hota hai.
Yahan pe servlet apne initial configurations perform karta hai.
3. Request Handling (service() method): Jab client request bhejta hai, to servlet
container service() method ko call karta hai. Yeh method client ki request ko process
karta hai aur response generate karta hai.
4. Destruction (destroy() method): Jab servlet ko destroy kiya jata hai, ya server
shutdown hota hai, to destroy() method call hota hai. Ismein servlet apna clean-up
work karta hai jaise resource deallocation, closing connections, etc.
Servlet Life Cycle Diagram:
rust
CopyEdit
Client Request ---> Servlet Container ---> service() Method ---> Response to Client
|
init() (One-time initialization)
|
destroy() (Servlet destroy after use)
Servlet Example:
Servlet ka ek basic example dekhte hain, jo HTTP request ko process karega aur response
dega:
java
CopyEdit
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
// Initialization method
public void init() throws ServletException {
// Initialization code
}
// Cleanup method
public void destroy() {
// Cleanup code
}
}
Explanation:
• init() method: Servlet ke initialization ke time pe yeh method ek baar call hota hai.
• doGet() method: Jab client GET request bhejta hai, to yeh method call hota hai.
Ismein response generate hota hai, jo client ko bheja jata hai.
• destroy() method: Jab servlet ko terminate karna hota hai, to yeh method call hota
hai.
Servlet Configuration (web.xml):
Web application ke configuration ke liye web.xml file ka use hota hai jisme servlet ka
mapping define kiya jata hai.
xml
CopyEdit
<web-app>
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
Is configuration ke through servlet container ko bataya jata hai ki HelloServlet class ko /hello
URL pattern pe map karna hai.
1. Performance: Servlet fast performance provide karta hai kyunki yeh server-side pe
execute hota hai.
2. Platform independence: Java ke nature ki wajah se servlet kisi bhi platform par run
ho sakta hai.
3. Multithreading support: Servlet ko multithreaded environment mein chalaya ja sakta
hai, jo better performance provide karta hai.
Servlet के िक
ु साि (Disadvantages):
1. Statefulness: Servlet inherently stateless hota hai, isliye agar state management ki
zarurat ho to session management ka use karna padta hai.
2. Complexity: Servlet-based applications ko develop karne mein complex logic aur
code structure ho sakta hai, khaas kar agar application bada ho.
Conclusion:
Servlet Java ka ek powerful tool hai jo web applications ke liye dynamic content generate
karne mein madad karta hai. Yeh client-server communication ko efficiently handle karta hai
aur web application development mein important role play karta hai.
JSP िा main advantage yeh hai ki, ismein HTML code aur Java code ko alag-alag manage
kiya ja sakta hai, jo application ko modular banata hai. Iska main focus web developers ko
HTML aur Java code ko efficiently combine karne mein madad karna hai.
JSP ko generally servlets ke alternate ke roop mein use kiya jata hai. JSPs ko compile karke
servlets mein convert kiya jata hai aur fir servlet container mein execute hota hai.
JSP Architecture:
JSP ka architecture simple hai aur usmein kuch important components shamil hote hain.
Yeh components JSP page ko request aur response ke cycle mein process karte hain.
Components of JSP Architecture:
1. JSP Page: Yeh wo page hai jo user ko interact karne ke liye diya jata hai. JSP page
HTML aur Java code ka combination hota hai. Yeh .jsp file extension ke saath save
hota hai.
2. JSP Container (Web Container): Yeh container JSP pages ko process karta hai. Iska
kaam hota hai JSP pages ko servlets mein convert karna aur unhe client ke request ke
response mein execute karna. Yeh container servlet engine (jaise Apache Tomcat) ke
part hota hai.
3. Servlet Engine: JSP container internally servlet engine ko use karta hai. Jab bhi JSP
page pe request aati hai, servlet engine JSP ko servlet mein convert karta hai aur fir
usse execute karta hai.
4. JavaBeans: JSP pages mein Java beans ka use hota hai jisme data store hota hai.
JavaBeans reusable components hote hain jo data ko transfer karte hain aur business
logic ko manage karte hain.
5. Tag Libraries: JSP mein tag libraries ka use hota hai jo specific tasks ko perform karne
ke liye pre-defined tags provide karti hai. JSTL (JavaServer Pages Standard Tag Library)
ek common tag library hai jo JSP mein use hoti hai.
6. Client: Client browser hai jo JSP page ko request karta hai. Yeh web page ko browser
mein display karta hai.
JSP Architecture Diagram:
pgsql
CopyEdit
Client Request ---> JSP Page (contains HTML and Java code) ---> JSP Container
| |
| Servlet Engine (convert JSP to Servlet)
| |
V V
Response from Servlet to Client (HTML content)
JSP Example:
jsp
CopyEdit
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>JSP Example</title>
</head>
<body>
<h1>Welcome to JSP Example</h1>
<p>The current time is: <%= new java.util.Date() %></p>
</body>
</html>
Explanation:
• <%@ page %> Directive: Yeh page ko configure karta hai, jaise language (java) aur
content type.
• <%= %> Scriptlet: Ismein Java code embed hota hai jo dynamic content generate
karta hai. Is example mein current date ko display kiya gaya hai.
1. Directive Tags: Yeh JSP page ki configuration ke liye use hote hain. Example: <%@
page %>, <%@ include %>, <%@ taglib %>.
2. Action Tags: Yeh request ko manipulate karte hain aur output ko generate karte hain.
Example: <jsp:include>, <jsp:forward>.
3. Scripting Tags: Yeh Java code ko embed karte hain. Example: <%= %>, <% %>, <%! %>.
1. Easy to use: JSP ka use karna bahut simple hai, kyunki HTML aur Java code ko
combine kiya ja sakta hai.
2. Reusability: JavaBeans aur tag libraries ka use karke code ko reuse kiya ja sakta hai.
3. Server-side processing: JSP server-side pe process hota hai, jo client ko dynamic
content provide karta hai.
JSP के िक
ु साि (Disadvantages):
1. Complexity in large applications: Agar JSP pages complex ho jaate hain, to unko
manage karna thoda mushkil ho sakta hai.
2. Performance: Bahut zyada scripting tags aur complex business logic JSP pages mein
likhna performance ko affect kar sakta hai.
Conclusion:
JSP Java ka ek powerful tool hai jo dynamic web pages generate karne mein madad karta
hai. Iska architecture simple hai aur yeh easy-to-use hai, lekin large-scale applications mein
isse handle karna thoda complex ho sakta hai. JSP ka life cycle clearly define hai, jo request
handling, initialization, response generation aur cleanup ke stages ko cover karta hai.
Question – 9 JSP में Page Context क्या है and JSP-Directive kya dono ka
िणयि कीजिए ?
Answer - JSP में Page Context क्या है?
Page Context एि implicit object है जो JSP pages ke andar available hota hai. Yeh object
page ke scope mein hota hai aur isse page ki configuration aur metadata ke baare mein
information milti hai. Page context ka use page ke environment ko configure karne ke liye
hota hai, jaise ki request, response, session, application scope, etc.
JSP में page context ka object pageContext naam se available hota hai. Yeh object kuch
important properties aur methods ko represent karta hai jo page ke execution environment
ko define karte hain.
JSP-Directive क्या है ?
JSP Directive ek special tag hota hai jo JSP page ki behavior ko configure karta hai. Directive
ka use JSP page ko specify karne ke liye kiya jaata hai ki us page ka execution kaise hoga. Yeh
page ke global settings ko define karta hai, jo page ke liye apply hoti hain.
JSP directives ko <%@ directive %> tag ke andar likha jaata hai. JSP mein teen types ke
directives hote hain:
1. Page Directive (<%@ page %>):
o Page directive ka use page ki properties ko define karne ke liye hota hai, jaise
contentType, language, import, session, buffer size, etc.
Attributes:
o language: Page ka language set karta hai. Default: java.
o contentType: Response content type set karta hai. Example: text/html.
o import: External Java classes ko import karne ke liye use hota hai.
o session: Session tracking ko enable ya disable karta hai. Default: true.
o buffer: Output buffer size define karta hai.
Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" session="false"
%>
2. Include Directive (<%@ include %>):
o Include directive ka use external files ko include karne ke liye hota hai. Yeh
compile time pe include hota hai, jo page ko compile karte waqt file ko insert
karta hai.
Syntax:
<%@ include file="filename" %>
Example:
<%@ include file="header.jsp" %>
Yahaan header.jsp ko current page mein include kiya gaya hai.
3. Taglib Directive (<%@ taglib %>):
o Taglib directive ka use custom tag libraries ko include karne ke liye hota hai.
Yeh JSP page mein custom tags ko use karne ki facility deta hai.
Syntax:
<%@ taglib uri="taglib_uri" prefix="prefix_name" %>
Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
Is example mein JSTL core tag library ko import kiya gaya hai aur prefix c set kiya gaya hai.
JSP-Directive के लाभ:
1. Global Configuration: Yeh page ki global configuration set karne mein madad karta
hai, jo puri page ko affect karta hai.
2. Code Reusability: Include directive ka use code ko reusable banata hai, jise baar-baar
include kiya ja sakta hai.
3. Custom Tag Usage: Taglib directive ka use custom tags ko easily manage karne aur
reuse karne ke liye hota hai.
Conclusion:
• Page Context JSP page ka ek implicit object hai jo page ke environment aur scope ko
handle karta hai. Yeh page ke runtime data ko manage karta hai.
• JSP Directives ko use karke hum JSP page ki behavior aur configuration set karte hain.
Yeh page, include, aur taglib directives ki form mein hote hain, jo page ko modular,
reusable aur flexible banate hain.
• Request Parameters: Yeh client ke form data ya URL ke query parameters ko retrieve
karta hai.
• HTTP Headers: Yeh HTTP request ke headers ko access karne ki suvidha deta hai.
• Request Attributes: Yeh request-specific attributes ko store aur retrieve karta hai jo
ek request cycle ke dauran store hote hain.
• Session Handling: Yeh session data ko manage karne ke liye use hota hai, agar session
tracking enabled ho.
• Request URI and URL: Yeh client ke request ki URI aur URL ko fetch karne ki suvidha
deta hai.
• Response Status: Yeh server ki taraf se client ko send hone wale status codes ko set
karta hai (e.g., 200 OK, 404 Not Found).
• Output Content: Yeh HTML, JSON, XML, ya kisi bhi format mein data ko client ko send
karne ke liye content set karta hai.
• Redirect: Yeh client ko ek different URL par redirect karne ke liye use hota hai.
• Cookies: Yeh client ke browser mein cookies ko set karne aur read karne ke liye use
hota hai.
1. setContentType(String type): Yeh method response content type ko set karta hai,
jaise text/html ya application/json.
2. getWriter(): Yeh method response output stream ko access karta hai, jisme client ko
response bheja jaata hai.
3. sendRedirect(String location): Yeh method client ko kisi aur URL par redirect karta hai.
4. setStatus(int statusCode): Yeh method HTTP status code ko set karta hai (e.g., 404,
500).
5. addCookie(Cookie cookie): Yeh method client ke browser mein cookie set karta hai.
Example:
<%
response.setContentType("text/html");
response.getWriter().println("<h1>Hello, World!</h1>");
%>
Yahaan response.setContentType("text/html") se response type set kiya ja raha hai aur
response.getWriter().println() se HTML content client ko bheja ja raha hai.
Request और Response Implicit Objects का उपयोग:
• Request object ko server pe aaye request ke details (jaise form data, headers,
session) ko access karne ke liye use kiya jaata hai.
• Response object ko client ko response bhejne ke liye use kiya jaata hai, jaise content
type set karna, status code set karna, redirect karna, etc.
Example of Using Both Request and Response:
<%
String name = request.getParameter("name");
if (name != null) {
response.setContentType("text/html");
response.getWriter().println("<h1>Welcome, " + name + "!</h1>");
} else {
response.sendRedirect("error.jsp");
}
%>
Is example mein:
1. Request object se name parameter retrieve kiya gaya hai.
2. Agar name parameter available hai, toh Response object se HTML content bheja
jaata hai.
3. Agar name parameter null hai, toh client ko error.jsp par redirect kiya jaata hai.
Conclusion:
• Request Implicit Object client ke request details ko handle karta hai, jaise form data,
parameters, headers, etc.
• Response Implicit Object server ke response ko client tak bhejne ka kaam karta hai,
jaise content type set karna, output bhejna, status codes set karna, ya redirect karna.
Yeh dono objects JSP pages mein bahut hi important hote hain kyunki inka use data ko
process karne aur client-server communication ko manage karne mein hota hai.
Question – 11 What is AJAX? Explain advantages and disadvantages
of AJAX यह कैसे कायय करता है ?
Answer - AJAX (Asynchronous JavaScript and XML) क्या है?
AJAX ek technique hai jo web applications mein dynamic content ko load karne ke liye use
hoti hai bina page ko reload kiye. Iska main purpose web page ko dynamically update karna
hai, jisse user ko smooth aur responsive experience milta hai. AJAX ka use client-side pe
JavaScript aur server-side pe data fetch karne ke liye hota hai. AJAX ka use kisi bhi
asynchronous request ko handle karne ke liye hota hai, jisme data background mein fetch
hota hai aur page reload nahi hota.
AJAX ka full form hai Asynchronous JavaScript and XML, lekin aajkal AJAX mein data ko
XML ke alawa JSON, HTML, ya plain text mein bhi send kiya ja sakta hai.
1. JavaScript Call: Jab user kisi action (button click, form submit, etc.) karta hai, to
JavaScript function call hota hai.
2. AJAX Request: JavaScript ek request bhejta hai server ko (HTTP GET, POST, etc.) using
XMLHttpRequest object.
3. Server Processing: Server request ko process karta hai aur response send karta hai.
Yeh response XML, JSON, HTML, ya plain text mein ho sakta hai.
4. AJAX Response: Jab response server se milta hai, to JavaScript response ko handle
karta hai aur HTML DOM ko dynamically update kar leta hai.
5. Page Update: Server se response milne ke baad, page bina reload kiye hi update ho
jata hai.
AJAX की काययप्रणाली:
• Step 1: User koi action perform karta hai (jaise form submit).
• Step 2: JavaScript se XMLHttpRequest object create kiya jata hai.
• Step 3: Request server ko send ki jati hai (asynchronous).
• Step 4: Server se response aata hai (e.g., JSON, XML, etc.).
• Step 5: JavaScript response ko handle karta hai aur page ko update kar leta hai bina
page reload kiye.
<!DOCTYPE html>
<html>
<head>
<title>AJAX Example</title>
<script>
function loadData() {
var xhr = new XMLHttpRequest(); // Create new XMLHttpRequest object
xhr.open("GET", "data.txt", true); // Request to server (data.txt)
xhr.onload = function() {
if (xhr.status == 200) {
document.getElementById("result").innerHTML = xhr.responseText; // Display
server response
}
};
1. Browser Compatibility: Purani browsers AJAX ko support nahi karte hain, isliye
compatibility ka issue ho sakta hai.
2. SEO Issues: AJAX se content dynamically load hota hai, jisse search engines ko
content ko index karne mein dikkat ho sakti hai.
3. Complexity: AJAX implementation complex ho sakti hai, especially jab data ko handle
karna ho ya error handling karni ho.
4. JavaScript Dependency: Agar user ka browser JavaScript disable ho, to AJAX ka
functionality kaam nahi karegi.
5. Security Issues: AJAX requests server se sensitive data fetch karte hain, jisme proper
security measures lene ki zaroorat hoti hai.
1. Live Search: Jab user search box mein type karta hai, to search results ko dynamically
fetch kiya jata hai bina page reload kiye.
2. Form Validation: Form submit karne se pehle server pe data validate karna.
3. Real-time Data Updates: Chat applications aur live sports scores mein real-time
updates dikhana.
4. Social Media Updates: Facebook, Twitter jaise platforms pe likes, comments update
karna bina page reload kiye.
Conclusion:
AJAX ek powerful technique hai jo web applications ko fast aur responsive banata hai. Yeh
dynamic content loading ko enable karta hai bina puri page ko reload kiye. AJAX ke fayde
bhi hain aur kuch limitations bhi, jaise browser compatibility aur SEO issues. Lekin, proper
implementation ke saath yeh powerful web development mein ek important tool hai.
1. Session Creation: Jab ek user web application ko pehli baar access karta hai, to ek
session create hota hai. Yeh session ek unique session ID assign karta hai jo server par
store hoti hai.
2. Session Tracking: Web server user ki activity ko track karta hai. Jab user kisi page pe
navigate karta hai, to uski session ID ke through server identify karta hai ki yeh user
kaun hai aur kis state mein hai.
3. Session Expiry: Jab session ka time limit khatam ho jata hai, to session automatically
expire ho jata hai. Agar user ko session active rakhna ho to server ko refresh karna
padta hai.
4. Session Termination: Jab user browser ko band karta hai, ya logout karta hai, ya
session expiry time ke baad, session terminate ho jata hai.
Session Tracking:
Session tracking ka matlab hai ek user ke request aur response ke dauran uski information
ko maintain karna, taki user ki activity ko track kiya ja sake. Web applications stateless hote
hain, matlab har request independent hoti hai, aur server ko har new request par user ki
pehchaan nahi hoti. Isliye session tracking ka use kiya jata hai, jisme user ko ek unique
session ID assign ki jati hai.
Session Tracking Techniques:
1. Cookies:
o Cookies ek small text file hota hai jo client ke browser mein store hota hai.
Isme session ID ko store kiya jata hai, jisse user ki session ko identify kiya jata
hai.
o Example: Jab user login karta hai, server ek cookie create karta hai jo session
ID store karta hai. Agla request browser se session ID bhejta hai aur server user
ko identify karta hai.
2. URL Rewriting:
o URL rewriting mein session ID ko URL ke through pass kiya jata hai. Jab user ek
link click karta hai, to session ID URL mein add hota hai.
o Example: http://example.com/welcome.jsp;jsessionid=12345
3. Hidden Form Fields:
o Hidden fields HTML forms mein use hote hain jisme session ID ko store kiya
jata hai. Jab form submit hota hai, to session ID bhi submit hoti hai.
o Example: Hidden fields mein session ID pass karke form submission ke through
session ko track kiya jata hai.
4. Session Object (in Servlet/JSP):
o Server-side pe session ko manage karne ke liye Session Object use hota hai,
jisme user ke data ko store kiya jata hai.
o Example: Servlet mein session object create kiya jata hai:
o HttpSession session = request.getSession();
o session.setAttribute("username", "JohnDoe");
1. User Authentication: Jab user login karta hai, to session management uska login
state track karta hai. Isse user ko har page par dobara login nahi karna padta.
2. Maintaining User Data: Session tracking se user ke preferences ya choices ko web
application mein maintain kiya ja sakta hai. Jaise shopping cart mein items add karna
ya user settings ko store karna.
3. Personalized Experience: Session management se user ko personalized experience
milta hai, jisme application user ki activity aur preferences ko track karke customized
content display karta hai.
4. Security: Session management se user ke sensitive data ko secure rakha jata hai. Jab
user logout karta hai, to session automatically expire hota hai, jo security ko improve
karta hai.
1. User Data Management: Session ke through user ka data efficiently manage kiya ja
sakta hai, jaise cart items ya user preferences.
2. Reduced Server Load: Session tracking techniques se server par data ko continuously
store nahi karna padta, jo server load ko reduce karta hai.
3. Easy Authentication: Login aur authentication process ko easy banata hai, jisme user
ko baar-baar login nahi karna padta.
Session Management के िक
ु साि (Disadvantages):
1. Session Expiry: Agar session expire ho jata hai, to user ko phir se login karna padta
hai.
2. Security Risk: Agar session hijacking ho jaye (user ki session ID chura li jaye), to
sensitive information compromise ho sakti hai.
3. Client-Side Dependency: Kuch techniques (jaise cookies) client-side pe depend karti
hain, jise users disable kar sakte hain.
Conclusion:
Session aur session management web applications mein user interaction ko track karne aur
data ko maintain karne ke liye important techniques hain. Inhe implement karne se user ka
experience better hota hai, security improve hoti hai, aur server ka load bhi kam hota hai.
Question – 13 Http Servlet और Generic Servlet में क्या अंतर है? या फफर
Request Dispatcher के उपयोग बताइए?
Answer - Http Servlet और Generic Servlet में क्या अंतर है?
1. Http Servlet:
• Definition: HttpServlet ek subclass hota hai GenericServlet ka aur specifically HTTP
requests ko handle karta hai. Jab ek servlet HTTP request ko process karta hai, tab
HTTP methods (GET, POST, PUT, DELETE) use kiye jate hain.
• Methods:
o doGet(): GET request ko handle karta hai.
o doPost(): POST request ko handle karta hai.
o doPut(), doDelete() bhi HTTP methods ke liye available hote hain.
• Usage: HttpServlet tab use kiya jata hai jab aapko web-based applications banaani
ho, jahan HTTP protocol ka use hota ho.
• Example:
• @WebServlet("/myServlet")
• public class MyHttpServlet extends HttpServlet {
• protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
• response.getWriter().println("This is a GET request");
• }
• }
2. Generic Servlet:
• Definition: GenericServlet ek abstract class hota hai jo Servlet interface ko implement
karta hai. Yeh class protocol-independent hota hai, matlab HTTP ya kisi aur protocol
ke liye use kiya ja sakta hai.
• Methods:
o service(): Yeh method request aur response ko handle karta hai, aur aapko
doGet(), doPost() ya similar methods ko override karna padta hai.
• Usage: Jab aapko kisi specific protocol ke liye servlet develop karni ho (jaise FTP ya
SMTP), tab GenericServlet ka use hota hai.
• Example:
• public class MyGenericServlet extends GenericServlet {
• public void service(ServletRequest request, ServletResponse response) throws
ServletException, IOException {
• response.getWriter().println("This is a Generic Servlet");
• }
• }
Request Dispatcher ka use ek servlet ke request ko dusre servlet, JSP page, or HTML page
ko forward karne ke liye kiya jata hai. Yeh RequestDispatcher interface ke through hota hai,
aur yeh request aur response object ko transfer karta hai.
Methods:
• forward(Request, Response): Yeh method current request aur response ko kisi aur
resource (jaise servlet, JSP, etc.) ke pass forward karta hai.
• include(Request, Response): Yeh method ek resource ka content include karta hai
aur current response mein add kar deta hai.
Usage Example:
1. Forwarding Request:
2. RequestDispatcher dispatcher = request.getRequestDispatcher("nextPage.jsp");
3. dispatcher.forward(request, response);
Ismein current servlet ya JSP page se request ko forward kiya jata hai "nextPage.jsp" ke
paas.
4. Including Content:
5. RequestDispatcher dispatcher = request.getRequestDispatcher("header.jsp");
6. dispatcher.include(request, response);
Yeh method current page ke content mein "header.jsp" ka content include kar deta hai.
1. Code Reusability: Aap ek page ya servlet ka content dusre page mein reuse kar sakte
hain.
2. Separation of Concerns: View aur business logic ko separate karne mein madad karta
hai. (JSP aur servlet ka use alag-alag functionalities ke liye)
3. Redirection: RequestDispatcher se forward karne ka process server ke side pe hota
hai, isliye URL change nahi hota.
Conclusion:
• HttpServlet aur GenericServlet mein major difference protocol support mein hota
hai. HttpServlet HTTP requests ko handle karta hai jabki GenericServlet kisi bhi
protocol ko handle kar sakta hai.
• RequestDispatcher ka use ek servlet ke request ko kisi dusre resource (JSP, servlet)
tak forward karne ya include karne ke liye hota hai. Yeh web applications mein
request management ko manage karne mein madad karta hai.
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h2>GET Request Aayi</h2>");
out.println("<p>Name: " + name + "</p>");
out.println("<p>Age: " + age + "</p>");
}
}
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Data Location URL ke saath jata hai Body ke andar chhupa hota hai
Data Length Chhota data (2048 char tak) Large data bhej sakte ho
Kab use karein Jab data sirf lena ho Jab form ya secret data bhejna ho
Form ke Example:
अब, 2-Tier, 3-Tier, और N-Tier आकिणटे क्चर, प्रत्येि में लसस्टम िो विलभन्न िेयरों में बााँटने िा
तरीिा है।
1. 2-Tier Architecture:
• Server Layer (Data Layer): यह िह हहस्सा होता है जहां िेटाबेस और िेटा प्रोसेलसंग होती है।
• क्िाइंट द्िारा भेजे गए अनरु ोध िो सीधे सिणर द्िारा संसाधधत किया जाता है ।
उदाहरण:
लाभ:
हानियाँ:
• जैसे-जैसे यूजसण िी संख्या बढ़ती है , लसस्टम धीमा हो सिता है ।
2. 3-Tier Architecture:
3-Tier Architecture में , लसस्टम िो तीि लेयरों में बााँटा जाता है:
• Logic Layer (Application Layer): यह िेयर बबजनेस िॉजजि और िेटा प्रोसेलसंग िरती है ।
• Data Layer (Database Layer): यह िेयर िेटाबेस से संबधं धत िायण िरती है , जैसे िेटा
संग्रहर् और पुनप्राणजप्त।
• क्िाइंट (Presentation Layer) सिणर से िेटा प्राप्त िरने िे लिए एजप्ििेशन िॉजजि
(Application Layer) िो िॉि िरता है ।
• एजप्ििेशन िॉजजि िेटा िो िेटाबेस से प्राप्त िरता है और कफर उसे क्िाइंट िो भेजता है ।
उदाहरण:
लाभ:
• अिग-अिग िेयर िे िारर्, प्रत्येि िेयर िो स्ितंत्र रूप से अपिेट किया जा सिता है ।
हानियाँ:
N-Tier Architecture में , लसस्टम िो N लेयरों (जहां N 3 से अधधि हो सिता है ) में बााँटा जाता है ।
इसमें हर िेयर िा विलशष्ट िायण होता है और सभी िेयर एि दस
ू रे से स्ितंत्र होती हैं।
• Additional Layers (Optional): जैसे Caching Layer, Security Layer, Web Service Layer,
आहद।
• प्रत्येि िेयर विलशष्ट िायण िरती है और किसी अन्य िेयर पर ननभणर नहीं होती। उदाहरर् िे
लिए, एि सुरक्षा िेयर हो सिता है जो िेटाबेस या एजप्ििेशन िेयर से पहिे आता है ।
उदाहरण:
• एि एंटरप्राइज-िेिि िेब एजप्ििेशन, जैसे एि बैंकिंग लसस्टम, जहां प्रत्येि िायण िो अिग-
अिग िेयर में विभाजजत किया जाता है।
लाभ:
हानियाँ:
Summary of Differences:
Feature 2-Tier Architecture 3-Tier Architecture N-Tier Architecture
Conclusion:
• 2-Tier Architecture is suitable for simpler applications with lower traffic.
• 3-Tier Architecture is more robust and suited for applications requiring more
scalability and data processing.
• N-Tier Architecture is designed for very large applications that require extensive
layers for various concerns (security, caching, etc.), and it’s ideal for enterprise-level
systems.
Yeh tha PageContext ka explanation! Agar aur kuch padhna hai toh batao!
Answer - Arre yaar, ab tumhara question hai na JSP mein Request aur Response ke Implicit
Objects ke baare mein, toh chalo thoda simple style mein samjhata hoon.
Summary:
• Request object tumhe client se aayi hui request ki details provide karta hai, jaise form
data, query parameters, etc.
• Response object tumhe server ki taraf se client ko bheje jaane wale response ko
manage karta hai, jisme content type, redirection, output stream, etc. shamil hain.
1. Standard Tags:
Standard tags woh tags hote hain jo JSP mein built-in hote hain aur tumhe manually koi
extra configuration nahi karni padti. Yeh tags server-side processing ko manage karte hain
aur tumhare web page ko dynamic banate hain.
Types of Standard Tags:
1. Action Tags: Yeh tags tumhe JSP ke request, response, session, etc., ko handle karne
mein madad karte hain.
o jsp:include: Iska use kisi dusre resource ko include karne ke liye hota hai.
o jsp:forward: Iska use request ko forward karne ke liye hota hai ek dusre page
par.
o jsp:param: Yeh use hota hai parameters ko pass karne ke liye.
o jsp:useBean: Iska use Java bean ko create ya access karne ke liye hota hai.
2. Scripting Tags: Yeh tags allow karte hain Java code ko directly JSP page mein likhne ke
liye.
o <%! Declaration %>: Yeh tag variable ya method ko declare karne ke liye use
hota hai.
o <%= Expression %>: Iska use Java expression ko output ke roop mein display
karne ke liye hota hai.
o <% Scriptlet %>: Yeh tag tumhe Java code ko directly HTML ke beech mein
likhne ka option deta hai.
3. Directive Tags: Yeh tags page ka behavior define karte hain.
o <%@ page %>: Yeh tag page-specific settings ko define karta hai (jaise content
type, session management, etc.)
o <%@ include %>: Yeh tag ek file ko page ke ander include karne ke liye use
hota hai.
2. Custom Tags:
Custom tags tum khud define kar sakte ho agar tumhe kuch special functionality chahiye ho
jo JSP ke standard tags mein nahi mil rahi.
• Custom Tags ka Creation: Tum custom tags ko tag libraries ke through create karte
ho. Yeh tags Java classes ke form mein likhe jaate hain aur unhe JSP mein use kiya
jaata hai.
• JSP Tag Library (JSTL): JSTL ek collection hai predefined tags ka jo common tasks jaise
loop, conditional statements, etc. ko simplify karta hai.
Example of Custom Tag:
public class MyTagHandler extends TagSupport {
public int doStartTag() throws JspException {
// Custom code to be executed when the tag is encountered
return SKIP_BODY;
}
}
Yeh custom tag tumhare JSP page mein special functionality implement karta hai.
3. Directive Tags:
Directive tags tumhare JSP page ko define karte hain aur unka configuration set karte hain.
Yeh page level par applicable hote hain.
Types of Directive Tags:
1. <%@ page %>: Yeh directive tag page ke configuration ko define karta hai, jaise ki
page ka content type, error handling, buffer size, etc.
o Example:
o <%@ page language="java" contentType="text/html; charset=ISO-8859-1" %>
2. <%@ include %>: Yeh tag ek external file ko include karta hai tumhare page mein. Yeh
file include karne se pehle compile hoti hai.
o Example:
o <%@ include file="header.jsp" %>
3. <%@ taglib %>: Yeh tag libraries ko include karta hai. Tum agar custom tags use kar
rahe ho, toh tumhe yeh directive specify karna padta hai.
o Example:
o <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
Summary of JSP Tags Classification:
• Standard Tags: Predefined tags jo JSP mein already available hote hain (Action,
Scripting, Directive tags).
• Custom Tags: Custom tags jo tum khud define karte ho Java classes ke through.
• Directive Tags: Yeh page ke configuration ko set karte hain (page, include, taglib).
Agar tumhe yeh samajh mein aaya ho toh, next question bata dena!
2. JSP-Directive:
JSP directives tumhare JSP page ka configuration set karte hain. Yeh page level par apply
hote hain, aur tumhare page ke behavior ko control karte hain.
JSP Directives ke Types:
1. <%@ page %>: Iska use page ke configuration ko define karne ke liye hota hai, jaise ki
content type, language, error handling, etc.
2. <%@ include %>: Iska use ek external file ko include karne ke liye hota hai. Yeh file
compile hone se pehle tumhare page mein include hoti hai.
3. <%@ taglib %>: Yeh tag tumhe custom tag libraries ko page mein include karne ke
liye use hota hai.
1. <%@ page %> Directive:
Yeh directive page ke general settings ko configure karta hai. Jaise content type, buffer size,
etc.
Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" %>
• language: Java programming language use ho rahi hai.
• contentType: Page ka content type set kiya gaya hai.
2. <%@ include %> Directive:
Iska use ek file ko dynamically include karne ke liye hota hai, jisme HTML ya JSP code hota
hai.
Example:
<%@ include file="header.jsp" %>
Yeh file tumhare page ke start mein header.jsp ko include kar dega.
3. <%@ taglib %> Directive:
Yeh directive custom tag libraries ko page mein include karta hai.
Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
• uri: Yeh URI define karta hai jahan se tag library ko include kiya gaya hai.
• prefix: Yeh prefix define karta hai jo tum custom tags mein use karoge.
Summary of JSP Directives:
• <%@ page %>: Page configuration set karta hai (language, content type, etc.)
• <%@ include %>: External files ko include karta hai.
• <%@ taglib %>: Tag libraries ko include karta hai.
Conclusion:
• JDBC API tumhe Java application se database ke saath interact karne ka tareeka
provide karta hai.
• JSP-Directive tumhare JSP page ke configuration ko set karte hain aur tumhe page ko
control karne mein madad karte hain.
Agar yeh samajh mein aa gaya ho toh, next question bata dena!