Java Servlets පටන්ගමු: වෙබ් ඇප් හැදෙන හැටි - SC Guide

ආයුබෝවන් යාළුවනේ! කොහොමද ඉතින්? අද අපි කතා කරන්න යන්නේ වෙබ් Application development වලදී ගොඩක්ම වැදගත් වෙන, ඒ වගේම අපිට Java එක්ක server-side development පටන් ගන්න අනිවාර්යයෙන්ම දැනගන්න ඕන දෙයක් ගැන – ඒ තමයි Java Servlets!
ගොඩක් වෙලාවට අලුතින් programming පටන් ගන්න අයට Frontend ගැන කිව්වම HTML, CSS, JavaScript හරි React, Angular වගේ දේවල් මතක් වුනත්, Backend එක ගැන කියද්දි පොඩි අවුලක් තියෙනවා නේද? Java එක්ක backend කරන්න ගියාම Spring Boot, Quarkus වගේ modern frameworks ගැන තමයි වැඩිය කතා වෙන්නේ. ඒත් මේ frameworks වලට පදනම දාපු core concepts තේරුම් ගන්න Servlets ගැන දැනගන්න එක හරිම වැදගත්.
හිතන්නකෝ ඔයා Facebook එකට යනවා කියලා. ඔයාගේ Profile එකට ගියාම ඔයාගේ Photos, Posts, Friends ඔක්කොම එතන තියෙනවා. මේ data ඔක්කොම එන්නේ Facebook එකේ servers වලින්. මේ server එකේ request process කරලා, data base එකෙන් data අරන්, ඒක ඔයාට පේන්න හදන එකේදී Servlet වගේ technologies තමයි භාවිතා වෙන්නේ.
අද අපි මේ article එකෙන් Servlets කියන්නේ මොකක්ද, ඒවා වැඩ කරන්නේ කොහොමද, ඒ වගේම අපිටම පොඩි Servlet එකක් කොහොමද හදන්නේ කියලා සරලව බලමු. එහෙනම් වැඩේට බහිමුද?
Servlet කියන්නේ මොකක්ද? (What is a Servlet?)
සරලව කිව්වොත් Servlet කියන්නේ Java programming language එකෙන් ලියපු class එකක්. මේකේ ප්රධානම වැඩේ තමයි HTTP requests process කරලා, HTTP responses ආපහු client (web browser) එකට යවන එක. හරියට වෙබ් server එකක ඉන්න "සේවකයෙක්" වගේ. Client එකකින් request එකක් ආවම, ඒ request එක අරන්, ඒකට අවශ්ය වැඩේ කරලා (උදා: database එකකින් data ගන්න එක, calculation එකක් කරන එක), ප්රතිඵලය ආපහු client ට දෙන එක තමයි Servlet එකක මූලිකම කාර්යය.
අපි සාමාන්යයෙන් වෙබ් Applications වලදී දකින්නේ Front-end (HTML, CSS, JavaScript) විතරයිනේ. ඒත් මේ Front-end එකට එහායින් server එක පැත්තේ (Back-end) වෙන්නේ මොනවද කියලා අපි ගොඩක් වෙලාවට දන්නේ නෑ. Backend එකේදී Web server එකක් (like Apache Tomcat, Jetty, WildFly) ඇතුලේ තමයි මේ Servlet Application deploy වෙන්නේ. මේ server එක තමයි එන requests අදාළ Servlets වලට යොමු කරන්නේ.
Servlet එකක් කියන්නේ server-side component එකක්. මේකේ ප්රධාන වාසියක් තමයි platform independent වීම. ඒ කියන්නේ ඔයා Windows වල Servlet එකක් develop කරලා, ඒක Linux server එකක deploy කරන්න පුළුවන්. ඒ වගේම Servlets වලට Java Enterprise Edition (JEE) වලට අයිති වෙන්නෙත් මේ නිසයි.
Servlet වැඩ කරන්නේ කොහොමද? (How does a Servlet work?)
Servlet එකක ජීවිත චක්රය (Lifecycle) අදියර තුනකට බෙදන්න පුළුවන්. මේක තේරුම් ගන්න එක හරිම වැදගත් ඔයාට Servlet එකක් හරියටම තේරුම් ගන්න.
- Initialization (init()): වෙබ් server එක Servlet එක load කරනකොට (ගොඩක් වෙලාවට මුල්ම request එක එනකොට) මේ
init()
method එක run වෙනවා. මේක run වෙන්නේ එකම එක පාරයි. මේකෙන් කරන්නේ Servlet එකට අවශ්ය ආරම්භක සැකසුම් (configurations) ටික කරගන්න එක. උදා: database connection pools හදාගන්න එක. - Service (service()): Client එකකින් request එකක් ආවම, මේ
service()
method එක run වෙනවා. හැම request එකකටම මේ method එක run වෙනවා. මේක තමයි request එක process කරන ප්රධානම තැන.service()
method එකHttpServletRequest
object එකක් සහHttpServletResponse
object එකක් parameter විදිහට ගන්නවා. මේHttpServletRequest
object එකෙන් client එක එවන data (parameters, headers, cookies) ඔක්කොම ගන්න පුළුවන්.HttpServletResponse
object එක පාවිච්චි කරලා client එකට response එක යවන්න පුළුවන් (HTML, JSON, XML, images). සාමාන්යයෙන් අපිdoGet()
,doPost()
,doPut()
,doDelete()
වගේ methods තමයි use කරන්නේ.service()
method එකේ ඇතුලේ තමයි මේ methods call වෙන්නේ HTTP method එක අනුව. - Destruction (destroy()): වෙබ් server එක shutdown කරනකොට හරි Servlet එක memory එකෙන් අයින් කරනකොට හරි මේ
destroy()
method එක run වෙනවා. මේකෙන් කරන්නේ Servlet එක පාවිච්චි කරපු resources (උදා: database connections) වහලා දාන එක. මේකත් run වෙන්නේ එකම එක පාරයි.
මේ Lifecycle එක තේරුම් ගන්න diagram එකක් අඳින්න පුළුවන් නම් තව හොඳට තේරෙයි. සරලව කිව්වොත්:
- Browser එකෙන් Request එකක් එවනවා.
- Web Server (Tomcat වගේ එකක්) Request එක අරන් Servlet එකට යවනවා.
- Servlet එක (
service()
method හරහා) Request එක process කරලා Response එකක් හදනවා. - Response එක ආපහු Browser එකට යනවා.
අපිම Servlet එකක් හදමු! (Let's build a Servlet!)
හරි, දැන් අපි Servlet එකක් ගැන basic theory එක දන්නවා. ඒ knowledge එක use කරලා අපිම පොඩි Servlet එකක් හදලා බලමු. මේක හරිම සිම්පල් "Hello World" Servlet එකක්.
මේකට අපිට අවශ්ය දේවල්:
- Java Development Kit (JDK): ඕනෑම Java project එකකට මේක අත්යවශ්යයි.
- IDE (Integrated Development Environment): IntelliJ IDEA, Eclipse, NetBeans වගේ එකක්. මම මේක IntelliJ IDEA වලින් කරනවා කියලා හිතලා තමයි කියන්නේ.
- Apache Tomcat: Web Server එකක්. අපේ Servlet Application එක deploy කරන්න මේක ඕන වෙනවා.
- Maven / Gradle: Project Dependency Management Tool එකක්.
Project Setup
මුලින්ම අපි Maven project එකක් හදාගමු. ඔයාගේ IDE එකේදී "New Project" ගිහින් Maven project එකක් හදන්න. Archetype එක විදිහට maven-archetype-webapp
තෝරගන්න. GroupId, ArtifactId එහෙම ඔයාට ඕන විදිහට දෙන්න. (උදා: GroupId: com.example
, ArtifactId: my-first-servlet
)
pom.xml
file එකේ අපිට javax.servlet-api
dependency එක add කරගන්න ඕන.
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version> <!-- හෝ ඔයාට ඕන version එකක් -->
<scope>provided</scope> <!-- Tomcat එකෙන් provide කරන නිසා -->
</dependency>
</dependencies>
scope
එක provided
විදිහට දාන්නේ, මේ dependency එක Tomcat වගේ server එකක් විසින් run time එකේදී provide කරන නිසා. ඒක අපේ .war
file එකට include කරන්න ඕන නෑ.
Servlet Class එක හදමු
දැන් src/main/java
folder එකේ (ඔයාගේ project structure එක අනුව) HelloServlet.java
කියලා class එකක් හදමු.
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/hello") // මේක තමයි අපේ Servlet එකට යන්න ඕන URL path එක
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html"); // Response එක HTML විදිහට යවන්න කියලා කියනවා
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Hello Servlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>ආයුබෝවන්, Servlet ලෝකයට!</h1>");
out.println("<p>මේක තමයි අපේ පළවෙනි Servlet එක!</p>");
out.println("</body>");
out.println("</html>");
}
}
මේ code එකේ @WebServlet("/hello")
කියන annotation එකෙන් කියන්නේ, /hello
කියන URL path එකට request එකක් ආවම මේ HelloServlet
class එක handle කරන්න ඕන කියලා. ඒ වගේම doGet()
method එක handle කරන්නේ HTTP GET requests විතරයි. Post requests handle කරන්න ඕන නම් doPost()
method එක override කරන්න පුළුවන්.
Deployment
දැන් ඔයාගේ IDE එකේ Run Configuration වලට ගිහින් "Tomcat Server" එකක් add කරලා, ඔයාගේ project එක build කරලා (.war
file එකක් හදලා) Tomcat එකට deploy කරන්න. (IntelliJ IDEA එකේදී Artifacts tab එකට ගිහින් .war
exploded option එක add කරලා Run Configuration එකේදී ඒක deploy කරන්න පුළුවන්).
Tomcat Server එක start කරලා, ඔයාගේ browser එකේ http://localhost:8080/your_project_context_root/hello
(your_project_context_root
කියන්නේ ඔයාගේ project එකේ ArtifactId එක වෙන්න පුළුවන්, නැත්නම් Tomcat deploy configuration එකේ දාන නම) ගහලා බලන්න. එතකොට ඔයාට "ආයුබෝවන්, Servlet ලෝකයට!" කියන message එක පෙනෙයි.
ඒක නියමයි නේද? මේක තමයි Servlet එකක් වැඩ කරන විදිහේ සරලම උදාහරණය.
Servlet වල වාසි සහ අපිට වැදගත් වෙන්නේ ඇයි? (Advantages of Servlets and why they are important for us?)
අපි දැන් Servlets ගැන ටිකක් ගැඹුරින් දැනගත්තා. හැබැයි මේවා පරණ තාක්ෂණයක් නේද, දැන් Spring Boot වගේ දේවල් තියෙද්දි මේවායින් වැඩක් තියෙනවද කියලා ඔයාට හිතෙන්න පුළුවන්. අනිවාර්යයෙන්ම Servlets දැනගන්න එක ඔයාගේ backend development දැනුමට ගොඩක් වටිනවා.
වාසි:
- Robustness & Scalability: Servlets ගොඩක් stable. ඒ වගේම request ගොඩක් handle කරන්න පුළුවන් විදිහට design කරලා තියෙන්නේ. ඒක නිසා විශාල enterprise applications වලටත් මේවා පාවිච්චි කරන්න පුළුවන්.
- Platform Independence: Java වගේම Servletsත් ඕනෑම operating system එකක, ඕනෑම server එකක run කරන්න පුළුවන්.
- Efficiency: Servlets run වෙන්නේ Java Virtual Machine (JVM) එක ඇතුලේ. ඒ නිසා Java ecosystem එකේ තියෙන performance optimizations ඔක්කොම Servlets වලටත් ලැබෙනවා.
- Integration with Java Ecosystem: Java libraries, frameworks, tools ඔක්කොම Servlets එක්ක පහසුවෙන් integrate කරන්න පුළුවන්.
- Foundation for Frameworks: අද තියෙන Spring MVC, Struts වගේ frameworks වලට පදනම දාලා තියෙන්නේ Servlets වලින්. ඒ නිසා Servlet concepts තේරුම් ගන්න එකෙන්, මේ frameworks වල ඇතුලත වැඩ කරන විදිහ තේරුම් ගන්න ඔයාට ලේසි වෙනවා.
අපිට වැදගත් වෙන්නේ ඇයි?
ශ්රී ලංකාවේ software industry එක දියුණු වෙමින් පවතිනවා. Bank applications, Government portals, E-commerce platforms වගේ ගොඩක් තැන්වල Java ecosystem එක භාවිතා වෙනවා. විශේෂයෙන්ම වසර ගණනාවක් තිස්සේ දියුණු කරපු large-scale systems බොහොමයක Servlets direct හෝ indirect විදිහට පාවිච්චි වෙලා තියෙනවා. මේ systems වල maintenance, enhancements කරන්න වගේම අලුතින් යම් feature එකක් develop කරන්න ගියාම Servlets ගැන දැනුම ගොඩක් වටිනවා.
අපේ රටේ ජාතික මට්ටමේ project වලදී වගේම private sector එකේ විශාල project වලදීත් Java backend එක බහුලව භාවිතා වෙනවා. Servlets වල fundamentals තේරුම් ගන්න එකෙන්, ඔයාට මේ වගේ project වලට දායක වෙන්න පහසු වෙනවා. ඒ වගේම interview වලදීත් මේ වගේ core concepts ගැන ප්රශ්න අහන්න පුළුවන්.
නිගමනය (Conclusion)
හරි යාළුවනේ, අද අපි Java Servlets ගැන සරලව වුනත් ගැඹුරින් ඉගෙන ගත්තා. Servlet එකක් කියන්නේ මොකක්ද, ඒකේ Lifecycle එක කොහොමද, ඒ වගේම සරල Servlet එකක් කොහොමද හදාගන්නේ කියලා අපි කතා කළා. මේ concepts ඔයාට Java backend development වලදී හරිම වැදගත් වේවි.
මතක තියාගන්න, coding කියන්නේ කරලාම ඉගෙන ගන්න ඕන දෙයක්. මේ Hello World example එක ඔයාගේ computer එකේම හදලා බලන්න. ඊට පස්සේ parameter pass කරන විදිහ, session management වගේ දේවල් ගැනත් හොයලා බලන්න. ඒකෙන් ඔයාගේ දැනුම තවත් දියුණු කරගන්න පුළුවන්.
මේ article එක ගැන ඔයාට මොකද හිතෙන්නේ? ඔයාට මොනවා හරි ප්රශ්න තියෙනවා නම්, පහලින් comment කරන්න. අපි ඊළඟ article එකෙන් තවත් මේ වගේම වැදගත් දෙයක් ගැන කතා කරමු. එහෙනම් හැමෝටම ජයවේවා!