Tolal:67 Click:
1
2 3 4
JSP Interview Questions And Answers
Page 1
Ques: 1 what are cookies?
Ans:
A Cookie is a small piece of information or messages used to communicate between the web server and the web browser. Your browser stores each message in a small file, called cookie.txt. This information is exchanged between the browser and the server to remember some specific information.
How cookies Work : A command line in the HTML of a document tell the browser to set a cookie of a certain name or value. Here is an example of some script used to set a cookie. Set-Cookie: NAME=VALUE; expir es=DATE; path=PATH; domain=DOMAIN_NAME; secure Cookies are usually run from CGI scripts, but they can also be set or read by Javascript.
Examples of cookies :
* Cookies are used to track web site activity.
* Cookies are also used for online shopping.
* Servers also use cookies to provide personalized web pages.
Cookie Security : The easiest way to secure yourself against the supposed dangers of cookies is to get one of the latest browser versions and turn cookies off.
Ques: 2 What is JSP?
Ans:
JSP is a Java technology provides support to generate dynamic page using HTML, XML tag and scriptlets written in the Java programming language. JSP provides us a way to write Java code embedded into the HTML. JSP pages separate the page logic from its design and display.
JSP pages are compiled into servlets and may call JavaBeans components or Enterprise JavaBeans components to perform processing on the server.
JavaServer Pages has two subcategories :
* Tag Libraries
* JavaServer Pages Standard Tag Library(JSTL).
Ques: 3 What is lifecycle of JSP?
Ans:
Life cycle of JSP :
* JSP page translation,
* JSP page compilation,
* load class,
* create instance,
* call the jspInit() method,
* call the _jspService() method, and
* call the jspDestroy() method.
Ques: 4 What is the difference between JSP and Servlets ?
Ans:
JSP(Java Server Pages) and Servlets, both are server side executed program and used to generate dynamic web page. Servlets are pure java classes and compiled directly into the .class whereas JSP page is a combination of HTML and java code. First of all JSP is translated into java file and then compiled into servlet. Functionally both are same. Both use the Servlet API to communicate with the web server and the client. Both require a container on the server, such as Tomcat, which provides the environment and VM for the Java program. Servlets are best suited for request processing, handling the business logic while JSP is suitable for content generating (client view). It is easy to include HTML tag in JSP rather than servlet.
Ques: 5 What is difference between custom JSP tags and beans?
Ans:
JSP has its own tag library that you can use in any nuber of pages. You can also create your own tags (Cusustom tags), define attributes and body of the tags and then group your tags into collections called tag library.
To use custom JSP tags, you need to define three separate components:
* Tag handler class.
* Tag library descriptor file.
* JSP file that uses the tag library.
When the first two components are done, you can use the tag by using taglib directive.
JavaBeans are Java utility classes you defined. Beans have a standard format for Java classes. Custom tags and beans accomplish the same goals � encapsulating complex behavior into simple and accessible forms.
Difference between custom JSP tags and beans :
* Custom tags require more work to set up than do beans.
* Custom tags can manipulate JSP content, beans cannot.
* Custom tags usually define relatively self-contained behavior, whereas beans are often defined in one servlet and used in a different servlet or JSP page.
Ques: 6 What are the different ways for session tracking?
Ans:
The concept of session tracking allows you to maintain the relation between the two successive request from the same client (browser). The browser sends the request to the server and server process the browser request generate the response and send back response to the browser. The server is not at all bothered about who is asking for the pages. The server (because of the use of HTTP as the underlying protocol) has no idea that these 2 successive requests have come from the same user. There is no connection between 2 successive requests on the Internet.
There are three ways of tracking sessions :
* Using Cookies.
* Using URL Rewriting.
* Using Hidden Form Fields.
Ques: 7 What mechanisms are used by a Servlet Container to maintain session information?
Ans:
The mechanisms used by a Servlet Container to maintain session information :
a) Cookies
b) URL rewriting
c) Hidden form fields
d) SSL (using HTTPS protocol) Sessions
Ques: 8 Difference between GET and POST
Ans:
Difference between GET and POST :
* Client can send information to the server by two methods : GET and POST.
* The GET method appends parameters (name/value) pairs to the URL. The length of a URL is limited, there is a character restriction of 255 in the URL. so this method only works if there are only a few parameters.
* The main difference between GET and POST is, POST has a body. Unlike GET, parameters are passed in the body of the POST insteat of appending to the URL. We can send large number of data with the POST.
* "GET" is basically for just getting (retrieving) data whereas "POST" may involve anything, like storing or updating data, or ordering a product, or sending E-mail.
* The data send with the GET method is visible at the browser's address bar. Don't send important and sensitive data with GET method, use POST.
* Get is idempotent, means it has no side effects on re-requesting the same thing on the server.
Ques: 9 What is servlet mapping?
Ans:
Servlet mapping controls how you access a servlet. Servlet mapping specifies the web container of which java servlet should be invoked for a url given by client. For this purpose web container uses the Deployment Decriptor (web.xml) file. Servlets are registered and configured as a part of a Web Application. To register a servlet, you add several entries to the Web Application deployment descriptor.
Servlet Mapping :
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>myservlets.MappingExample</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>com/*</url-pattern>
</servlet-mapping>
A developer can map all the servelts inside its web application.
Ques: 10 What is servlet context ?
Ans:
Servlet context defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file. A servlet context object is one per web application per JVM and this object is shared by all the servlet. You can get and set the Servlet context object. You can use servelt context object in your program by calling directly getServletContext() or getServletConfig().getServletContext().
Ques: 11 How many JSP scripting elements are there and what are they?
Ans:
Types of Scripting Elements
� Expressions :
<%= expression %> :� Evaluated and inserted into the servlet�s output, i.e., results in something like out.print(expression)
� Scriptlets :
<% code %> :� Inserted verbatim into the servlet�s _jspService method (called by
service)
� Declarations :
<%! code %> :- Inserted verbatim into the body of the servlet class, outside of any
existing methods.
� Directive :
<%@ directive %> :- To use "import" statements in JSPs.
Ques: 12 How do I include static files within a JSP page?
Ans:
We can include static files within a JSP page using the include directive :
< % @ include file="top.html" % >
Ques: 13 How can I implement a thread-safe JSP page?
Ans:
To make your JSPs thread-safe, you hace to implement the SingleThreadModel interface.
This can be done by using page directive with isThreadSafe attribute by setting it to false:
<%@ page isThreadSafe="false" %>
Ques: 14 What is the difference in using request.getRequestDispatcher() and context.getRequestDispatcher()?
Ans:
The getRequestDispatcher() can take a relative path while getRequestDispatcher() take relative to the current context's root.
Example :
With ServletRequest-
request.getRequestDispatcher("./jsp/jsppage.jsp") :- evaluated relative to the path of the request
request.getRequestDispatcher("/jsp/jsppage.jsp") :- evaluated relative to the root are all valid.
With ServletContext only
context.getRequestDispatcher("/jsp/jsppage.jsp") is valid.
Ques: 15 What are context initialization parameters?
Ans:
Context initialization parameters are defined in the Deployment descriptor an accessible to all the servlets in the application. You can use context initialization parameters to specify the information that is dynamically change. You can also use these to specify the URL of the database.
<context-param>
<param-name>webmaster</param-name>
<param-value>jalees786@gmail.com</param-value> </context-param>
Ques: 16 What is a Expression?
Ans:
Expression in JSP is used to write the output on the stream. It has the following form :
<%= Java Expression %>
The Java expression is evaluated, converted to a string, and inserted in the page. This evaluation is performed at run-time and thus has full access to information about the request. For example, the following shows the date/time that the page was requested:
Current time is : <%= new java.util.Date() %>
Ques: 17 What is a Declaration?
Ans:
You must declare the variable or method before you use it in the JSP page. The Declaration is a JSP element use to declare the variable or methods that you can use in Java code later in the JSP page.
JSP Declaration :
<%! code %> :- Code is inserted in body of servlet class, outside of service method.
Example : The declaration must be valid in the scripting language used in the JSP file.
<%! int i = 0; %>
<%! int a, b, c; %>
Ques: 18 What is a Scriptlet?
Ans:
Scriptlet is a JSP element. It contains the java code enclosed within <% and %>. JSP Engine places these code in the _jspService() method.
Syntax of JSP Scriptles are:
<%
//java codes
%>
Example:
<HTML>
<BODY>
<%
String userName=null;
userName=request.getParameter("userName");
System.out.println( "Current Date is:" );
java.util.Date date = new java.util.Date();
%>
Hello <%= userName %> ! <br>
The time is now :<%= date %>
</BODY>
</HTML>
Ans:
The answer of jeesan is wrong.
System can not come in printing statement.
It must be out.println("message here");
Ques: 19 What are the implicit objects?
Ans:
Web container provides developer to access some objects instantiated by container and available to all the jsp pages. These objects are called implicit objects because they are automatically instantiated. Within each JSP application is the complete set of implicit objects.
There are nine implicit objects in JSP.
1. pageContext
2. session
3. request
4. response
5. exception
6. out
7. application
8. config
9. page
Ques: 20 What's the difference between forward and sendRedirect?
Ans:
When using request.forward(), request is forwarded to the another resource specified in the forward() parameter. Whereas response.sendRedirect() causes the web container to return to the browser indicating that a new URL should be requested. The browser issues a completely new request any object that are stored as request attributes before the redirect occurs will be lost.
Goto Page:
1
2 3 4
JSP Objective
JSP Objective Questions And Answers
JSP Interview Questions And Answers
JSP Interview Questions And Answers
R4R,JSP Objective, JSP Subjective, JSP Interview Questions And Answers,JSP,JSP Interview,JSP Questions ,JSP Answers