Tag Archives: কোড

ServletUtilities.java Utility class that simplifies the output of the DOCTYPE and HEAD in servlets, among other things. Used by most remaining servlets in the chapter.

Huge Sell on Popular Electronics

ServletUtilities.java  Utility class that simplifies the output of the DOCTYPE and HEAD  in servlets, among other things. Used by most remaining servlets in the chapter. 

package cwp;

import javax.servlet.*;
import javax.servlet.http.*;

/** Some simple time savers. Note that most are static methods.
 *  


 *  Taken from Core Web Programming Java 2 Edition
 *  from Prentice Hall and Sun Microsystems Press,
 *  .
 *  May be freely used or adapted.
 */

public class ServletUtilities {
  public static final String DOCTYPE =
    "";

  public static String headWithTitle(String title) {
    return(DOCTYPE + "\n" +
           "\n" +
           "\n");
  }

  /** Read a parameter with the specified name, convert it
   *  to an int, and return it. Return the designated default
   *  value if the parameter doesn't exist or if it is an
   *  illegal integer format.
  */

  public static int getIntParameter(HttpServletRequest request,
                                    String paramName,
                                    int defaultValue) {
    String paramString = request.getParameter(paramName);
    int paramValue;
    try {
      paramValue = Integer.parseInt(paramString);
    } catch(NumberFormatException nfe) { // null or bad format
      paramValue = defaultValue;
    }
    return(paramValue);
  }

  /** Given an array of Cookies, a name, and a default value,
   *  this method tries to find the value of the cookie with
   *  the given name. If there is no cookie matching the name
   *  in the array, then the default value is returned instead.
   */

  public static String getCookieValue(Cookie[] cookies,
                                      String cookieName,
                                      String defaultValue) {
    if (cookies != null) {
      for(int i=0; i' with
   *  '>', and (to handle cases that occur inside attribute
   *  values), all occurrences of double quotes with
   *  '"' and all occurrences of '&' with '&'.
   *  Without such filtering, an arbitrary string
   *  could not safely be inserted in a Web page.
   */

  public static String filter(String input) {
    StringBuffer filtered = new StringBuffer(input.length());
    char c;
    for(int i=0; i') {
        filtered.append(">");
      } else if (c == '"') {
        filtered.append(""");
      } else if (c == '&') {
        filtered.append("&");
      } else {
        filtered.append(c);
      }
    }
    return(filtered.toString());
  }
}


ContactSection.jsp A snippet of a JSP page. It defines a field (accessCount), so pages that include it and want to directly utilize that field must use the include directive, not jsp:include.

Huge Sell on Popular Electronics

ContactSection.jsp হচ্ছে JSP পৃষ্ঠার একটি অংশ।এটি একটি ফিল্ডকে সঙ্গায়িত করে (accessCount), সুতরাং যে পৃষ্ঠায় এটি অন্তর্ভুক্ত থাকে এবং সরাসরি উক্ত ফিল্ড ব্যবহার করাতে চায় তাকে আবশ্যই include directive ব্যবহার করতে হবে, jsp:include নয়।.

<%@ page import="java.util.Date" %>

<%-- The following become fields in each servlet that
     results from a JSP page that includes this file. --%>
<%! 
private int accessCount = 0;
private Date accessDate = new Date();
private String accessHost = "<I>No previous access</I>";
%>

<P>
<HR>
This page © 2000 
<A HREF="http//www.my-company.com/">my-company.com</A>.
This page has been accessed <%= ++accessCount %>
times since server reboot. It was last accessed from 
<%= accessHost %> at <%= accessDate %>.

<% accessHost = request.getRemoteHost(); %>
<% accessDate = new Date(); %>

J2SE : LinkedList and Iterators in Java

Huge Sell on Popular Electronics

/*
 * LinkedList.java
 *
 * Created on January 10, 2008, 8:51 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package linkedlist;

import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Collections;
import java.util.Random;

/**
 *
 * @author Sayed
 */
public class LinkedListTest {
    
    /** Creates a new instance of LinkedList */
    public LinkedListTest() {
    }
    
    /**
     *Example operations using linked lists
     */
    
    public void linkedListOperation(){
        
        final int MAX = 10;
        int counter = 0;

        //create two linked lists
        List listA = new LinkedList();
        List listB = new LinkedList();

        //store data in the linked list A
        for (int i = 0; i < MAX; i++) {
            System.out.println("  - Storing Integer(" + i + ")");
            listA.add(new Integer(i));
        }
       
        //print data from the linked list using iterator 
        Iterator it = listA.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }

        //print data from the linked list using listIterator. 
        counter = 0;
        ListIterator liIt = listA.listIterator();
        while (liIt.hasNext()) {
            System.out.println("Element [" + counter + "] = " + liIt.next());
            System.out.println("  - hasPrevious    = " + liIt.hasPrevious());
            System.out.println("  - hasNext        = " + liIt.hasNext());
            System.out.println("  - previousIndex  = " + liIt.previousIndex());
            System.out.println("  - nextIndex      = " + liIt.nextIndex());
            System.out.println();
            counter++;
        }


        //retrieve data from the linked list using index
        for (int j=0; j < listA.size(); j++) {
            System.out.println("[" + j + "] - " + listA.get(j));
        }

        //find the location of an element
        int locationIndex = listA.indexOf("5");
        System.out.println("Index location of the String \"5\" is: " + locationIndex);  

        //find the first and the last location of an element
        System.out.println("First occurance search for String \"5\".  Index =  " + 
        listA.indexOf("5"));
        System.out.println("Last Index search for String \"5\".       Index =  " + 
        listA.lastIndexOf("5"));

        //create a sublist from the list 
        List listSub = listA.subList(10, listA.size());
        System.out.println("New Sub-List from index 10 to " + listA.size() + ": " + 
        listSub);

        //sort the sub-list
        System.out.println("Original List   : " + listSub);
        Collections.sort(listSub);
        System.out.println("New Sorted List : " + listSub);
        System.out.println();

        //reverse the new sub-list
        System.out.println("Original List     : " + listSub);
        Collections.reverse(listSub);
        System.out.println("New Reversed List : " + listSub);
        System.out.println();

        //check to see if the lists are empty
        System.out.println("Is List A empty?   " + listA.isEmpty());
        System.out.println("Is List B empty?   " + listB.isEmpty());
        System.out.println("Is Sub-List empty? " + listSub.isEmpty());
        
        //compare two lists
        System.out.println("A=B? " + listA.equals(listB));        
        System.out.println();

        //Shuffle the elements around in some Random order for List A
        Collections.shuffle(listA, new Random());

        //convert a list into an array
        Object[] objArray = listA.toArray();
        for (int j=0; j < objArray.length; j++) {
            System.out.println("Array Element [" + j + "] = " + objArray[j]);
        }

        //clear listA
        System.out.println("List A   (before) : " + listA);        
        System.out.println();
        listA.clear();
        System.out.println("List A   (after)  : " + listA);        
        System.out.println();
        
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        LinkedListTest listExample = new LinkedListTest();
        listExample.linkedListOperation();
    }
    
}

C++ code : Read file content

Huge Sell on Popular Electronics

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
  ifstream in("test", ios::in | ios::binary);

  if(!in) {
    cout << "Cannot open input file.\n";
    return 1;
  }

  double num;
  char str[80];

  in.read((char *) &num, sizeof(double));
  in.read(str, 14);
  str[14] = '\0'; // null terminate str

  cout << num << ' ' << str;

  in.close();

  return 0;
}

A simple C++ Program code

Huge Sell on Popular Electronics

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
  ifstream in("test", ios::in | ios::binary);

  if(!in) {
    cout << "Cannot open input file.\n";
    return 1;
  }

  double num;
  char str[80];

  in.read((char *) &num, sizeof(double));
  in.read(str, 14);
  str[14] = '\0'; // null terminate str

  cout << num << ' ' << str;

  in.close();

  return 0;
}

ASP.NET টিউটোরিয়াল :[পর্বঃ ৬]:: ASP.NET Web Forms দিয়ে একসাথে ফর্মের অনেক কোডের নিয়ন্ত্রণ . ASP.NET Web Forms – Maintaining the ViewState

Huge Sell on Popular Electronics

ASP.NET টিউটোরিয়াল :[পর্বঃ ৬]:: ASP.NET Web Forms দিয়ে একসাথে ফর্মের অনেক কোডের নিয়ন্ত্রণ
লেখকঃ মোস্তাফিজুর ফিরোজ ।

গত পর্বে আমরা শিখেছিলাম ASP.NET Web Forms দিয়ে এইচটিএমএল ফর্ম ট্যাগের ব্যবহার । আজ আমরা শিখবো একসাথে ফর্মের অনেক কোডের নিয়ন্ত্রণ । এজন্য আগে আপনাকে ViewState এর নিয়ন্ত্রণ সম্পর্কে জানতে হবে ।

ViewState এর নিয়ন্ত্রণ
যখন একটি ফর্ম classic ASP আকারে নিবেদন করা হয় তখন ফর্মের সকল মান চলে যায় । ধরুন আপনি একটি ফর্মের সাথে অনেক তথ্য যোগ করে পাঠালেন আর এরর রিপোর্ট আসলো । তাহলে বুঝতেই পারছেন আপনার কেমন মেজাজ গরম হবে । আপনার মেজাজ গরম না হলেও আমার কিন্তু খুব মেজাজ গরম হবে । তাই আবার ব্যাক করে এসে দেখলেন আবার সব ফর্ম পূরণ করা লাগছে । তাহলে বুঝতে হবে এই সাইট আপনার ViewState পুরাপুরি নিয়ন্ত্রণ করতে পারেনি ।

আবার অনেক সাইটে দেখবেন এমন এরর রিপোর্ট দেখালেও ব্যাক করলে ফর্মে আপনার পূরণ করা সকল তথ্য দেখতে পারবেন । এটা কে করলো? তাইতো । খুব খুশি তাই নাহ? এটা ঐ পেজের ASP .NET ধারণ করে রেখেছে । তার মানে ঐ পেজের ViewState পুরাপুরি নিয়ন্ত্রণ করতে সক্ষম হয়েছে । ঐ পেজে একটা <form runat="server"> ট্যাগ হিডেন করা আছে যা আপনার সকল তথ্য সংরক্ষণ করে রেখেছে । তাহলে আসুন দেখি ঐ পেজের সোর্স কোড কেমন হয়ঃ

<form name="_ctl0" method="post" action="page.aspx" id="_ctl0">
<input type="hidden" name="__VIEWSTATE"
value="dDwtNTI0ODU5MDE1Ozs+ZBCF2ryjMpeVgUrY2eTj79HNl4Q=" />

.....some code

</form>

ViewState এর নিয়ন্ত্রণ হলো ASP.NET Web Forms এর ডিফল্ট সেটিংস । আপনি যদি এটাকে নিয়ন্ত্রণ না করতে চান তাহলে .aspx পেজের উপরে একটি <%@ Page EnableViewState="false" %> অথবা EnableViewState="false" এই ট্যাগ যোগ করতে পারেন ।

তাহলে দেখেন পুরাতন পদ্ধতিতে কোডগুলো কেমন দেখায়ঃ

<html>
<body>

<form action="demo_classicasp.aspx" method="post">
Your name: <input type="text" name="fname" size="20">
<input type="submit" value="Submit">
</form>
<%
dim fname
fname=Request.Form("fname")
If fname<>"" Then
Response.Write("Hello " & fname & "!")
End If
%>

</body>
</html>

এটা হল পুরাতন পদ্ধতি যাতে আপনি যখন সাবমিট করবেন তখন এর ভিতরকার কোডগুলো অন্তর্নিহিত থাকবে ।

তাহলে এইবার নতুন পদ্ধতিটা দেখে নেই যে কেমন হবে কোডটাঃ

<script runat="server">
Sub submit(sender As Object, e As EventArgs)
lbl1.Text="Hello " & txt1.Text & "!"
End Sub
</script>

<html>
<body>

<form runat="server">
Your name: <asp:TextBox id="txt1" runat="server" />
<asp:Button OnClick="submit" Text="Submit" runat="server" />
<p><asp:Label id="lbl1" runat="server" /></p>
</form>

</body>
</html>

আপনি এই নতুন পুরাতন দুই পদ্ধতিতেই একটি ফর্মের সকল কোড নিয়ন্ত্রণ করতে পারবেন । যেটা আপনার সুবিধা হবে সেটাই প্র্যাকটিস করবেন । তাহলে আজ এটাই শিখতে থাকুন ভালো করে ।

জাভাস্ক্রিপ্ট সুইচ বিবৃতি (JavaScript Switch Statement in Bangla)

Huge Sell on Popular Electronics

শরিফুল ইসলাম
Job category-Php Coder

ভিন্ন ভিন্ন শর্তে ভিন্ন ভিন্ন কাজ পারফর্ম করার জন্য এই switch statement ব্যবহার করা হয়।

জাভাস্ক্রিপ্ট সুইচ বিবৃতি

Switch statement এর মাধ্যমে অনেকগুলো ব্লক কোড থেকে শর্ত অনুযায়ী একটি কোড পছন্দ করবে এবং সে অনুযায়ী কাজ করবে

Syntax


switch(expression) {
    case n:
        code block
        break;
    case n:
        code block
        break;
    default:
        default code block
}

  • এই expression একবার মূল্যায়ন করা হয়
  • এই expression যতগুলো কেস আছে তাদের মধ্যে তুলনা করে
  • যদি কোনটা মিলে যায় তবে তবে সেই কোড গণনা করে।

 

উদাহরণ

সপ্তাহের দিনের সংখ্যা দিয়ে আমরা সপ্তাহের নাম বের করব Sunday=0, Monday=1, Tuesday=2 ...


switch (new Date().getDay()) {
    case 0:
        day = "Sunday";
        break;
    case 1:
        day = "Monday";
        break;
    case 2:
        day = "Tuesday";
        break;
    case 3:
        day = "Wednesday";
        break;
    case 4:
        day = "Thursday";
        break;
    case 5:
        day = "Friday";
        break;
    case 6:
        day = "Saturday";
        break;
}

 

ফলাফল আসবে


Sunday


 

Break কীওয়ার্ড

Javascript কোড যখন break কি-ওয়ার্ড এ পৌঁছে তখন সে switch ব্লক কে ভেঙে দেয়। তখন অতিরিক্ত কোড গণনা করা ছেড়ে দেয়, বা ব্লকের ভিতর কোড টেস্ট করা থামিয়ে দেয়। যখন কোন statement মিলে যায় , তখন কাজ সম্পূর্ণ হয়, সে আর তখন অতিরিক্ত কোন টেস্টিং করে না।

 

ডিফল্ট কি-ওয়ার্ড

যদি কোন কেস না মিলে তাহলে ডিফল্ট ভাবে একটা কি ওয়ার্ড ডিসপ্লে করে যদি সেটি আমরা উল্লেখ করে দেই

উদাহরণ

যদি আজকে শনিবার বা রবিবার না হয় তাহলে ডিফল্ট মেসেজ ডিসপ্লে করবে


switch (new Date().getDay()) {
    case 6:
        text = "Today is Saturday";
        break;
    case 0:
        text = "Today is Sunday";
        break;
    default:
        text = "Looking forward to the Weekend";
}

 

ফলাফল


Looking forward to the Weekend


 

প্রচলিত কোড এবং fall through

অনেক সময় আপনি সুইচ ব্লক এর মধ্যে ভিন্ন ভিন্ন কেসে একই চদ এবাবহার করতে চান। আমাদের পরবর্তী উদাহরণ এ ঠিক এই ধরনের একটি একই কোড ব্লক দেখানো হয়েছে এবং সুইচ ব্লক এর মধ্যে ডিফল্ট কেস টি শেষ কেস হবে না।

উদাহরণ


switch (new Date().getDay()) {
    case 1:
    case 2:
    case 3:
    default:
        text = "Looking forward to the Weekend";
        break;
    case 4:
    case 5:
       text = "Soon it is Weekend";
        break;
    case 0:
    case 6:
       text = "It is Weekend";
}

যদি সুইচ ব্লকে ডিফল্ট ভাবে শেষ কেস না হয় , তাহলে মনে রাখতে হবে ব্রেক দিয়ে এর কাজ শেষ করতে হবে।