• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Servlet FAQ

January 3, 2011 By j2eereference 3 Comments

What is Servlet ?

Servlets are server side programs , they are request and response oriented program. Suppose you enter something in the form and submit it, Then who is processing your request ? Someone has to pick up the details you have entered in the form.

Servlet is the server side program which handles all the request and redirect to the result page.

What is the difference between getSession(true) and getSession(false) ?

When you say getSession(true), this method will check whether already a session is existing for the user. If a session is existing, it will return that session object, otherwise will create a session object explicitly and return to the cilent.

When you say getSession(false), this method will check whether a session is existing. If yes, then it returns the reference of that session object, otherwise it will return null.

What is load on startup element in web.xml file ?

We can specify the order in which we want to initialize various Servlets.Like first initialize Servlet1 then Servlet2 and so on.This is accomplished by specifying a numeric value for the <load-on-startup> tag. <load-on-startup> tag specifies that the servlet should be loaded automatically when the web application is started.

The value is a single positive integer, which specifies the loading order. Servlets with lower values are loaded before servlets with higher values.

What is Session Tracking?

Session tracking is a mechanism that servlets use to maintain state about a series of requests from the same user. period of time

Difference between ServletContext and ServletConfig

ServletContext is for the whole web-app but the ServletConfig is for one particular Servlet.

The ServletConfig parameters are specified for a particular servlet and are unknown to other servlets.

The ServletContext parameters are specified for an entire application and are available to all the servlets within that application.

Example :

ServletConfig Parameters

<servlet>

<servlet-name>FirstServlet</servlet-name>

<servlet-class>FirstServlet</servlet-class>

<init-param>

<param-name>MaxLength</param-name>

<param-value>25</param-value>

</init-param>

</servlet>

ServletContext Parameters

<context-param>

<param-name>AdminEmail</param-name>

<param-value>admin@209.97.166.197</param-value>

</context-param>

Difference between RequestDispatcher’s forward method and HttpServletResponse’s sendRedirect method

The forward method of RequestDispatcher will forward the ServletRequest and ServletResponse that it is passed to the path that was specified in getRequestDispatcher(String path). This method is useful for communicating between server resources. Because the request and response are forwarded to another resource all request parameters are maintained and available for use.

Example using forward:

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

RequestDispatcher rd = request.getRequestDispatcher(“pathToResource”);

rd.forward(request, response);

}

In case of  sendRedirect(String path) method the client will build a new request and submit it to the server. All previous parameters stored in the request will be unavailable. The client’s history will be updated so the forward and back buttons will work.

Example using sendRedirect:

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.sendRedirect(“pathToResource”);

}

Filed Under: Servlets

Reader Interactions

Comments

  1. venugopal.reddy1108 says

    October 18, 2011 at 1:16 pm

    in the 5th question u’ve given heading “servlet config parameters” for d Servlet context parameters example..

    Reply
    • admin says

      October 18, 2011 at 1:31 pm

      Thanks a lot for notifying the mistake.

      Reply
  2. venugopal.reddy1108 says

    October 18, 2011 at 1:18 pm

    this material very useful for me.
    i’m preparing advanced java concepts from this website only.

    Thanq..

    Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.