This blog is intended to provide frequently asked interview questions and answers.
Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts
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 want50First 50 prime numbers are :-2357111317192329313741434753596167717379838997101103107109113127131137139149151157163167173179181191193197199211223227229
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); } } }
Subscribe to:
Posts (Atom)
What is Normalization?
Database normalization is a data design and organization process applied to data structures based on rules that help building relational dat...