Showing posts with label JSP/SERVLET. Show all posts
Showing posts with label JSP/SERVLET. Show all posts

Friday 5 April 2013

When can we use forward and when can we use sendRedirect?



What is the difference between sendRedirect and forward in java?

forward

SendRedirect

Control can be transferred within the server

Control can be transferred to the other server also

The request and response objects are transferred with the forward

Request and response objects are not transferred with the sendRedirect

In case of forward URL is not refreshed

In case of sendRedirect the URL is refreshed

No new request is generated

New request is generated

Session is not lost in forward

Session is not lost in sendRedirect also



Where to use and where to use sendRedirect?
sendRedirect can be used in two scenarios
  1. You want to transfer control to resource on different server.
  2. Where you want to separate tasks as Do the PaymentProcess and then redirect to displayPaymentInfo. If the client refreshes the browser only the displayPaymentInfo will be done again and PyamenProcess will not be repeated..
In all other cases Forward wil serve the purpose.

Difference between forward and sendRedirect


S No.
Forward
sendRedirect
1
Control can be transferred resources available on the same server.
Control can be redirect to defferent server.
2
Request/Response objects  transferred with forward
New Request/Response objects are created.  
3
Session is not lost
Session is not lost.
4
This is done by the container
This is done by the browser
5
URL is not changed
New URL is generated
6
This  is faster than sendRedirect
This is slower than Faster

What happens if you call destroy() from init() in java servlet?



This is a very tricky question often asked in java interviews
The simple answer to this question is that don’t  get confused by the name of the method destroy()  because it never destroy the servlet. When this method is call, The content of this method gets executed and then the respective process continues.
Programmers are not supposed to call this method. It will be invoked by the container.

Thursday 4 April 2013

Difference between Statement and Prepeared Statement




Most relational databases handles a JDBC / SQL query in four steps:
  1. Parse the incoming SQL query
  2. Compile the SQL query
  3. Plan/optimize the data acquisition path 
  4. Execute the optimized query / acquire and return data

A Statement will always proceed through the four steps above for each SQL query sent to the database. A PreparedStatement pre-executes steps (1) - (3) in the execution process above. Thus, when creating a PreparedStatement some pre-optimization is performed immediately. The effect is to lessen the load on the database engine at execution time.