Friday, June 8, 2012

ජාවා අභියෝගය: ගැටළුවලට විසඳුම් මෙන්න...!!!

 කළින් post එකේ මම ඉදිරිපත් කළා ජාවා ගැටළු කීපයක්.


බොහෝ දෙනෙක් විසින් ලිපිය කියවා තිබුණත් ගැටළුවලට පිළිතුරු සැපයීමට ඉදිරිපත් වී තිබුනේ නැහැ.

නමුත් confuzed programmer විසින් නිවැරදි පිළිතුරු ඉදිරිපත් කර තිබුනා.

ඉහත ලිපියේ පොරොන්දු වූ පරිදි නිවැරදි පිළිතුරු වඩා විස්තරාත්මකව පහත දක්වා තිබෙනවා.



1) for loop  එක i=0  සිට i=4 දක්වා පස් වතාවක් execute වෙයි.
switch එකේදී අදාළ i අගයට අනුව case statement එක execute වෙයි.
break statement ඇත්නම් flow එක නැවතී,switch case එකෙන් එළියට යයි.එනම් ඊළඟ i අගය execute වීම ඇරඹෙයි.
එක සහ තුන cases වලදී break statement එක නොමැත. එමනිසා ඊළඟ case එකද execute වන අතර   x සහ z දෙවරක් print වෙයි.

ඒ අනුව පිළිතුර: v w x x y z z

2) valueOf method එකෙන් කරන්නේ input parameter එකේ String representation එක ලබාදීමයි.Input parameter එක object එකක් හෝ char array එකක් හෝ ඕනෑම primitive type එකක් විය හැක. එමනිසා එයින් 12  print වෙයි.

toString method එකෙන්  කරන්නේ String object එකට reference එකක් return කිරීමයි.එය අළුත් String instance එකක් නිර්මාණය නොකරයි.

ඒ අනුව පිළිතුර: 12, true

3) Parameter යක් නැති m1() method එකේදී A.m1() යන්න B.m1() මඟින් override වෙයි.Overriding නීති අනුව print වන්නේ B.m1() යන්නය.එක් parameter යක් අවශ්‍ය m1() method එකේදී m1 method එක overload වී ඇත. එමනිසා overloading rules සැළකිය යුතුය.

Overloaded methods දෙකෙන් එකක් instance method එකක් වන අතර අනික static method එකක් වෙයි.මෙහිදී call වෙන්නේ static method එකයි.

ඒ අනුව පිළිතුර: hi, B.m1

4) synchronized යනු method modifier එකක් මිස class modifier එකක් නොවේ.එමනිසා class එකක් synchronized මඟින් declare කිරීමට යාමේදී compile error ලැබේ.
synchronized මඟින් අදහස් කරන්නේ implementation detail එකක් නිසා, implementation එකක් නැති abstract method එකකට එය යෙදීමේ අර්ථයක් නැත.

ඒ අනුව පිළිතුර: 2,3,4 lines

5) සෑම array type එකක්ම Serializable interface එක implement කරයි. එමනිසා Serializable type එකේ reference එකකට array එකක් assign කිරීමේ ගැටළුවක් නැත.

ඒ අනුව පිළිතුර: ගැටළුවකින් තොරව compile වීම සහ run වීම සිදුවෙයි.





Monday, June 4, 2012

ඔබේ ජාවා දැනුම කොහොමද? හැකිනම් විසඳන්න, ජාවා වලින් පොඩි ගැටළු කීපයක්..

මේවා ඇත්තටම ඉතාම සරල මට්ටමේ ජාවා ගැටළු කීපයක්.
ජාවා පිළිබඳ මූලික මට්ටමේ සිද්ධාන්ත පිළිබඳ ගැටළු..
නමුත් ආරම්භය සඳහා සරල ගැටළු කීපයක් ඉදිරිපත් කළත් ඉදිරියට තරමක් අමාරු ගැටළු ඉදිරිපත් කරන්න බලාපොරොත්තු වෙනවා.

පහත ගැටළුවලට ඔබේ පිළිතුර සටහන් කරන්න comment තීරය භාවිතා කරන්න.
කෙසේ වෙතත් නිවැරදි පිළිතුරු ඉක්මනින් පළ කිරීමට බලාපොරොත්තු වෙනවා.

වැඩසටහන් compile කර run කර බලා පිළිතුරු ලියා පලක් නැත.:-) .
Program එක කියවා බලා ප්‍රතිඵලය කීමට උත්සහ කරන්න. ප්‍රතිඵලයට හේතුව ද දක්වන්න.

1) පහත වැඩසටහන compile කර run කිරීමට උත්සහ කළහොත් ලැබෙන ප්‍රතිඵලය කුමක්ද?

class ABC {
public static void main ( String args [] ) {
for ( int i=0 ; i < 5 ; i++ ) {
switch (i) {
case 0 : System.out.print ("v" ) ; break ;
case 1 : System.out.print ("w" ) ; 
case 2 : System.out.print ("x" ) ; break ;
case 3 : System.out.print ("y" ) ; 
case 4 : System.out.print ("z" ) ; break ;
default : System.out.print ("d" ) ;
   }
  }
 }
}

2)  පහත වැඩසටහන compile කර run කිරීමට උත්සහ කළහොත් ලැබෙන ප්‍රතිඵලය කුමක්ද?

 class ABC {
 public static void main ( String args [] ) {
 System.out.print (String.valueOf (1) + String.valueOf (2) ) ;
String s1 = "S1" ;
String s2 = s1.toString ();
System.out.print ( " , " + ( s1==s2) ) ;
 }
}


3)   පහත වැඩසටහන compile කර run කිරීමට උත්සහ කළහොත් ලැබෙන ප්‍රතිඵලය කුමක්ද?

class A {
void m1 () {
System.out.print("A.m1" ) ; }
 }
class B extends A {
   void m1() {
   System.out.print("B.m1" ) ; }
   static void m1( String s ) {
   System.out.print(s + "," ) ; }
  }

class C {
public static void main ( String args [] ) {
B.m1("hi") ;
new B().m1() ;
 }
}

4) Compile errors ලබාදෙන්නේ පහත කුමන lines වලින්ද?

abstract class ABC {                                         // LINE 1
abstract  synchronized void m1 () ;                    // LINE2
abstract synchronized class B {}                       // LINE3
synchronized class C extends B { }                   // LINE4
}

5) පහත වැඩසටහන compile කර run කිරීමට උත්සහ කළහොත් ලැබෙන ප්‍රතිඵලය කුමක්ද?

import java.io.Serializable ;
class ABC {
public static void main ( String args [] ) {
int [] i = { 1, 2 , 3 } ;
Serializable  s = i ;
i = ( int [] ) s ;
 }
}

ගැටළුවලට නිවැරදි පිළිතුරු විස්තරාත්මකව මෙතනින්.





















Friday, June 1, 2012

පළමු Struts2 Web application එක ලියමු - User ට Hello කියමු.

Struts2 Framework එක භාවිතයෙන් සරල application එකක් ලියන හැටි කියන්නයි මේ ලිපිය ලියන්නේ.

 Web application එකක් පිළිබඳව මූලික දැනීමක් ඇතැයි අපේක්ෂා කරනවා. මන්ද හැමදෙයම මුලසිට සරලව විස්තර කිරීම අපහසු නිසා. මේ ලිපියේ කරුණු ගැන ගැටළුවක් ඇත්නම් විමසන්න comment තීරුව භාවිතා කරන්න. එලෙසම යම් වරදක් ඇතොත් පෙන්වා දෙන්න අමතක කරන්නත් එපා.

මේ සඳහා ලිවිය යුතු files මේවාය.
 1) Logon.jsp
 2) Struts.xml
 3)  LoginAction.java
 4)  ApplicationResource.properties
 5) Welcome.jsp
 6)  Web.xml
 

එක් එක් file වල කාර්යභාරය කෙටියෙන්...

 1) Logon.jsp
    -User ගෙන් අවශ්‍ය input ලබාගැනීම සඳහා
  
 2) Struts.xml
    -  Struts හි Configurations  සඳහා..
        request එකට අදාළ action class එක සහ method එක සොයාගැනීම
        response එකට අනුව display කළයුතු page එක සොයාගැනීම

 3)  LoginAction.java
    -Business logic එක ඇත්තේ මෙහිය.
     ලබාගත් input, variable වලට assign වන්නේ මෙහිදීය. 

 4)  ApplicationResource.properties
     වෙනම properties file එකක් යොදා ගැනීමෙන් labels සහ error messages ලබාදීම පහසු කරගත හැක.

 5) Welcome.jsp
     - Final Output එක display කරන page එක


 6)  Web.xml
    - Web application එකේ configurations  ලබාදීම සඳහා
     

ඊළඟට code සහිතව සහ වඩා විස්තරාත්මකව සළකා බලමු.



 1) Logon.jsp

මෙය Login JSP Page එකයි.

01. <%@ page contentType="text/html; charset=UTF-8"%>
02. <%@ taglib prefix="s" uri="/struts-tags"%>
03. <html>
04. <head>
05. <title>Struts 2 - Login Application | PMTDB </title>
06. </head>
07.
08. <body>
09. <h2>Struts 2 - Login Application</h2>
10. <s:actionerror />
11. <s:form action="login.action" method="post">
12.    <s:textfield name="username" key="label.username" size="20" />
13.    <s:password name="password" key="label.password" size="20" />
14.    <s:submit method="execute" key="label.login" align="center" />
15. </s:form>
16. </body>
17. </html>



Web Application එක Run වෙනවිට මුලින්ම දර්ශනය වන්නේ මේ පිටුවයි.
මෙහිදී User ගෙන් username සහ password ලබාගැනෙයි. Login Button එක Click කළ විට Login request එකක් generate වෙයි. මේ request එක action class එකකට ගොස් අදාළ conditions පරීක්ෂා කළ යුතුය.

ඉතින් මේ request එක යා යුතු action class එක කුමක්දැයි සොයාගන්නේ කෙසේද? එය සොයාදෙන්නේ Struts.xml file එකයි.

2) Struts.xml


 01. <?xml version="1.0" encoding="UTF-8" ?>
02.   <!DOCTYPE struts PUBLIC
03.    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
04.   "http://struts.apache.org/dtds/struts-2.0.dtd">

05. <struts>
06.    <constant name="struts.enable.DynamicMethodInvocation"
07.        value="false" />
08.    <constant name="struts.devMode" value="false" />
09.    <constant name="struts.custom.i18n.resources"
10.        value="ApplicationResources" />

11.     <package name="default" extends="struts-default" namespace="/">
12.        <action name="login" method="execute"
13.            class="PMTDB.struts2.LoginAction">
14.            <result name="success">Welcome.jsp</result>
15.            <result name="error">Login.jsp</result>
16.        </action>
17.        </package>
18. </struts>






action name එක Login නම් යා යුතු Action class එක LoginAction බවද ක්‍රියාත්මක විය යුතු method එක    execute බවද ඉහත file එකේ සඳහන් වෙයි.

එපමණක් නොව method එක execute වීමේදී ලැබෙන result එක අනුව ඊළඟට යා යුතු pages සඳහන් වෙන්නේ ද මෙහිමය. result එක success නම් Welcome.jsp page එකට යා යුතු බවද result එක error නම් නැවත Login.jsp page එකට යා යුතු බවද ඉහත සඳහන් වී ඇත.

 3)  LoginAction.java

           package PMTDB.struts2;

                import com.opensymphony.xwork2.ActionSupport;

               public class LoginAction extends ActionSupport {
                       private String username;
                       private String password;

                  public String execute() {

                       if (this.username.equals("PQR")
                          && this.password.equals("PQR123")) {
                          return "success";
                     } else {
                         addActionError(getText("error.login"));
                       return "error";
                      }
                       }

                  public String getUsername() {
                    return username;
                   }

                   public void setUsername(String username) {
                  this.username = username;
                  }

                  public String getPassword() {
                  return password;
                    }

                 public void setPassword(String password) {
                 this.password = password;
                    }
                  }





Action class එකේදී username සහ password යන properties, declare කර ඇත. එලෙසම ඒවාට getters සහ setters ලියා ඇත.

 Action class එක ActionSupport නම් class එක inherit කර ඇත.

 public class LoginAction extends ActionSupport {

 execute method එක තුළ username සහ password, check කර ඇත. username යන්න PQR නම් සහ password යන්න PQR123 නම් success යන්නද නැත්නම් error යන්නද return කර ඇත.

 4)  ApplicationResource.properties

ApplicationResources වෙනම properties file එකක් යොදා ගැනීමෙන් labels සහ error messages ලබාදීම පහසු කරගත හැක.


label.username= Username
label.password= Password
label.login= Login
error.login= Invalid Username/Password. Please try again.



5) Welcome.jsp

Action class එකේදී success return වුනොත් පෙන්නුම් කරන්නේ පහත jsp එකයි.

01. <%@ page contentType="text/html; charset=UTF-8"%>
02. <%@ taglib prefix="s" uri="/struts-tags"%>
03. <html>
04. <head>
05. <title>Welcome</title>
06. </head>
07.
08. <body>
09.     <h2>Hello, <s:property value="username" />.......!</h2>
10. </body>
11. </html>



මෙහිදී කලින් ලබාගත් username එක යොදාගෙන user ට Hello කියා ඇත.

6)  Web.xml

පහත ලබාදී ඇත්තේ configurations සඳහා වන Web.xml එකයි.

01. <?xml version="1.0" encoding="UTF-8"?>
02. <web-app id="WebApp_9" version="2.4"
03.    xmlns="http://java.sun.com/xml/ns/j2ee"
04.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
05.    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
06.
07.    <display-name>Struts2 Application | PMTDB </display-name>
08.    <filter>
09.        <filter-name>struts2</filter-name>
10.        <filter-class>
11.            org.apache.struts2.dispatcher.FilterDispatcher
12.        </filter-class>
13.    </filter>
14.    <filter-mapping>
15.        <filter-name>struts2</filter-name>
16.        <url-pattern>/*</url-pattern>
17.    </filter-mapping>
18.    <welcome-file-list>
19.        <welcome-file>Login.jsp</welcome-file>
20.    </welcome-file-list>
21.
22. </web-app>