Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Difference between ServletConfig and ServletContext

ServletConfig
·         ServletConfig available in javax.servlet.*; package
·         ServletConfig object is one per servlet class
·         Object of ServletConfig will be created during initialization process of the servlet
·         This Config object is public to a particular servlet only
·         Scope: As long as a servlet is executing, ServletConfig object will be available, it will be destroyed once the servlet execution is completed.
·         We should give request explicitly, in order to create ServletConfig object for the first time
·         In web.xml – <init-param> tag will be appear under <servlet-class> tag
ServletContext
·         ServletContext available in javax.servlet.*; package
·         ServletContext object is global to entire web application
·         Object of ServletContext will be created at the time of web application deployment
·         Scope: As long as web application is executing, ServletContext object will be available, and it will be destroyed once the application is removed from the server.
·         ServletContext object will be available even before giving the first request
·         In web.xml – <context-param> tag will be appear under <web-app> tag

Difference between requestdispatcher and sendRedirect

The RequestDispatcher interface allows you to do a server side forward/include whereas sendRedirect() does a client side redirect. 

SendRedirect() will search the content between the servers. it is slow because it has to intimate the browser by sending the URL of the content. then browser will create a new request for the content within the same server or in another one. 

RquestDispatcher is for searching the content within the server i think. its the server side process and it is faster compare to the SendRedirect() method. but the thing is that it will not intimate the browser in which server it is searching the required date or content, neither it will not ask the browser to change the URL in URL tab. so it causes little inconvenience to the user.


Difference Between doGet() and doPost() Methods

Difference between doGet and doPost mehtods

DoGet

1)In doGet Method the parameters are appended to the URL and sent along with header information
2)Maximum size of data that can be sent using doget is 240 bytes
3)Parameters are not encrypted
4)DoGet is faster if we set the response content length since the same connection is used. 
5)Thus increasing the performance
6)DoGet should be idempotent. i.e. doget should be able to be repeated safely many times
7)DoGet should be safe without any side effects for which user is held responsible

DoPost

1)In doPost, parame
ters are sent in separate line in the body
2)There is no maximum size for data
3)Parameters are encrypted 
4)Dopost is generally used to update or post some information to the server
5)DoPost is slower compared to doGet since doPost does not write the content length
6)This method does not need to be idempotent. Operations requested through POST can have side effects for which the user can be held accountable, for example, updating stored data or buying items online
7)This method does not need to be either safe 


Difference between System.out.println() and PrintWriter ?

Both are used to display the output. But system.out.println is used in java and out.prinln (PrintWriter) is used in scripting languages 

One thing we have to notice is, we will create the object for the class PrintWriter as "out".  out.println() is used in JSP. If we create object for that PrintWriter class as "hello", we have to print line as hello.println(). 

With System.out.println() we can print to Console. If you want to be very precise, then you have to say that System.out.println() writes to the default output stream. This is usually shown at the console, but can equally well go into a file (if you've redirected it) or nowhere (when running without a console). But System.out internally creates PrintWriter object. Not exactly. It refers to a PrintStream object. So why cant we use PrintWriter.println() directly to write to console. Because if you create a PrintWriter it is just another PrintWriter that writes to whatever you specified at the constructor. It is not the same thing as the one PrintStream that is used in System.out.

So finally there are 2 ways to write to console
1)Using System.Out.println() where System.Out is an Instance of PrintStream and by default points to Console
2)By writing in constructor of PrintWriter as mentioned above.

Disadvantage:
PrintWriter.println() is not a static method so you can just invoke the method. System.out is a PrintStream that "points to" the standard output stream and is ready for writting. To use PrintStream.println() to write to the console you would need to create a PrintStream instance that streams to standard out.


Java program print prime numbers

import java.util.*;
class PrimeNumbers
{
   public static void main(String args[])
   {
      int n, status = 1, num = 3;

      Scanner in = new Scanner(System.in);
      System.out.println("Enter the number of prime numbers you want");
      n = in.nextInt();

      if (n >= 1)
      {
         System.out.println("First "+n+" prime numbers are :-");
         System.out.println(2);
      }

      for ( int count = 2 ; count <=n ;  )
      {
         for ( int j = 2 ; j <= Math.sqrt(num) ; j++ )
         {
            if ( num%j == 0 )
            {
               status = 0;
               break;
            }
         }
         if ( status != 0 )
         {
            System.out.println(num);
            count++;
         }
         status = 1;
         num++;
      }        
   }
}



Output:


Enter the number of prime numbers you want
50
First 50 prime numbers are :-
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
101
103
107
109
113
127
131
137
139
149
151
157
163
167
173
179
181
191
193
197
199
211
223
227
229

Java program to check palindrome

import java.util.*;
 
class Palindrome
{
   public static void main(String args[])
   {
      String original, reverse = "";
      Scanner in = new Scanner(System.in);
 
      System.out.println("Enter a string to check if it is a palindrome");
      original = in.nextLine();
 
      int length = original.length();
 
      for ( int i = length - 1; i >= 0; i-- )
         reverse = reverse + original.charAt(i);
 
      if (original.equals(reverse))
         System.out.println("Entered string is a palindrome.");
      else
         System.out.println("Entered string is not a palindrome.");
 
   }
}

Output:

Enter a string to check if it is a palindrome
this is my blog
Entered string is not a palindrome. 
 

Java program to find factorial

import java.util.Scanner;
 
class Factorial
{
   public static void main(String args[])
   {
      int n, c, fact = 1;
 
      System.out.println("Enter an integer to calculate it's factorial");
      Scanner in = new Scanner(System.in);
 
      n = in.nextInt();
 
      if ( n < 0 )
         System.out.println("Number should be non-negative.");
      else
      {
         for ( c = 1 ; c <= n ; c++ )
            fact = fact*c;
 
         System.out.println("Factorial of "+n+" is = "+fact);
      }
   }
}

Output:

Enter an integer to calculate it's factorial
5
Factorial of 5 is = 120

Java programming to get ip address

import java.net.InetAddress;
 
class IPAddress
{
   public static void main(String args[]) throws Exception
   {
      System.out.println(InetAddress.getLocalHost());
   }
}

Java program to open Notepad

import java.util.*;
import java.io.*;
 
class Notepad {
  public static void main(String[] args) {
    Runtime rs = Runtime.getRuntime();
 
    try {
      rs.exec("notepad");
    }
    catch (IOException e) {
      System.out.println(e);
    }   
  }
}

What is Normalization?

Database normalization is a data design and organization process applied to data structures based on rules that help building relational dat...