Servlets and JSP Questions and Answers
Posted On December 19, 2007 by Priyadarshan Roy filed under Java
1) Define a servlet
Servlet is a java class file executed in Servlet-compliant webserver like Tomcat and result sent to browser(normally). It is also possible to invoke the servlet from an applet or frame. In such cases, the result will be sent to the applet or frame respectively.
(tunnel method).The invoking device may be a wap-browser also.
2) Define JSP
JSP can be viewed as html with interposed java code either as scriptlet or as expression.
3) What is the the problem with plain JSP?
In plain JSP without using bean, the business logic is exposed to the web-server administrator. So, this is not desirable.
4) what is the problem with Servlet?
It is very clumsy to create a dynamic page using Servlets. We will need a lot of out.println(...) statements.
5) What is path? classpath?
path is the folder(s) where exe/dll files for compiling/running the programs are available.
classpath is the folder(s) where class files are available.
6) Merits & demerits of JSP vs. Servlet
Servlets are more powerful. They can be used for sending binary output also.(ex) dynamic bar & pie-charts. They hide the business logic from prying eyes.
It is possible to use declarative management of servlet behaviour by entries in web.xml
JSP are easy to work with. The server compiles the JSP file by itself. But, for hiding the logic, we should use JSP beans.
7) How many versions of Tomcat?
Tomcat3.2, Tomcat4, Tomcat5 (the latest is 5.5).
8) What is special about Tomcat?
Tomcat is the reference implementation of Servlet & JSP API by SUN in collaboration with Apache Software Foundation. It is open-source & free.
9) What path is required for tomcat5 and tomcat4?
Tomcat5 & Tomcat4 require path as d:\jdk1.4.2\bin or higher.
10) What classpath is required to compile servlet, using tomcat4?
d:\tomcat4\common\lib\servlet.jar
11) What is a bean?
Any java class is a java bean. It has attributes, setter/getter methods for the attributes, public no-args constructor and any general functionality.
12) What is the difference between function bean & value bean?
Function bean does not have any attributes. So no need for setter/getter methods or constructor even, because there is nothing to initialize.
Value bean is a java class without any functionality but only attributes , setter/getter methods and public no-args constructor.
13) What is the important point to be noted when we use package?
The package statement should be the first statement in the source file.
14) give code for echo servlet.
import javax.servlet.*;
import java.io.*;
public class echoservlet extends HttpServlet
{
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
//-----------------
String s = request.getParameter("text1");
out.println(s);
}
}
15) how will you compile and test echo servlet in tomcat4?
>set path=d:\winnt\System32;d:\jdk1.4.2\bin
>set classpath=d:\tomcat4\common\lib\servlet.jar
>javac echoservlet.java
>edit echoservlet.htm
// echoservlet.htm
<html>
<body>
<form method=post action="/servlet/echoservlet" >
<input type=text name="text1">
<input type=submit>
</form>
</body>
</html>
copy echoservlet.htm to d:\tomcat4\webapps\root
copy echoservlet.class to
d:\tomcat4\webapps\root\WEB-INF\classes
make entry in d:\tomcat4\webapps\root\WEB-INF\web.xml
<servlet>
<servlet-name>echoservlet</servlet-name>
<servlet-class>echoservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>echoservlet</servlet-name>
<url-pattern>/servlet/echoservlet</url-pattern>
</servlet-mapping>
16) what is the action field if no package is used for servlet?
"/servlet/demo"
17) what is a context?
The folder in the same level as 'root folder .
(ex) d:\tomcat4\webapps\ourcontext
18) How will you create and test a new context in tomcat4?
Create a folder tomcat\webapps\ourcontext
create WEB-INF under ourcontext
create classes and lib folders under WEB-INF
copy web.xml from root\WEB-INF into ourcontext\WEB-INF
19) Where is web.xml located?
d:\tomcat4\webapps\root\WEB-INF
20) what is JSTL?
JSTL stands for JSP standard tag library. These are tags approved by JCP. These tags deal with
a) core
b) xml
c) sql
d) functions
e) formatting
21) What jar files are essential for using JSTL?
jstl.jar & standard.jar
22) What is the typical servlet-configuaration if no package is used?
<servlet>
<servlet-name>echoservlet</servlet-name>
<sefvlet-class>echoservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>echoservlet</servlet-name>
<url-pattern>/servlet/echoservlet</url-pattern>
</servlet-mapping
23) ,, if we use a package?
<servlet>
<servlet-name>echoservlet</servlet-name>
<sefvlet-class>mypack.echoservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>echoservlet</servlet-name>
<url-pattern>/servlet/mypack.echoservlet</url-pattern>
</servlet-mapping>
24) What is the URL if new context 'ourcontext' is used?
http://localhost:8080/ourcontext
25) How will you develop a JSP using a bean?
>md work
>cd work
work>edit greeter.java
package mypack;
public class greeter
{
public String greetme(String a)
{
return "hello..."+a;
}
}
>javac greeter.java
copy greeter.class to tomcat\webapps\root\WEB-INF\classes\mypack
create greeterjsp.jsp
<html>
<body>
<jsp:useBean id=bean1 class="mypack.greeter" />
<%
String a = request.getParameter("text1");
String r = bean1.greetme(a);
out.println(r);
%>
</body>
</html>
=========================
create greeterjsp.htm
<html>
<body>
<form method=post action="greeterjsp.jsp"
<input type=text name='text1'>
<input type=submit>
</form>
</body>
</html>
=========================
copy greeterjsp.htm & greeterjsp.jsp to:
tomcat\webapps\root
===========================
invoke by:
http://localhost:8080/greeterjsp.htm'
===========================
26) What is the advantage of tags?
Development of webpages is made easier if we use tags. As these tags are just similar to html tags, even non-programmers can use them and develop functioning web pages easily. And tags are re-usable. (ie) we can use tags developed by third party companies.
27) differentiate between scriptlet and expression.
<%
String s = request.getParameter("text1");
out.println(s);
%>
The above code is scriptlet.
The following line is expression.
<%= request.getParameter("text1") %>
28) what is a custom tag?
Tags are of two types.
a) JSTL ( JSP STANDARD TAG LIBRARAY)
b) custom tags .
These tags are developed by companies and are distributed either free or at a cost. There are very powerful tags, which make programming difficult tasks very easy. Such tags are known as custom tags. Jakarta tag library is famous in this respect.
29) What is a servlet-filter?
Servlet filters are powerful tools that are available to web application developers using the Servlet specification. Filters are designed to be able to manipulate a request or response (or both) that is sent to a web application. Almost every single web application will seriously benefit from using Servlet filters to both cache and compress content.
1.Cache filter:
A caching filter optimizes the time it takes to send back a response from your web server.
2.compression filter:
Compression filter optimizes the size of the content that you send from your web server to a user via the Internet.
30) What is a servlet-listener?
Listeners is basically pre-defined interfaces that are available for developers in the application lifecycle to achieve some tasks especially when dealing with the ServletContext as well as HttpSession objects.The two most widely used Servlet Listeners are
1.ServletContext Listener
2.HttpSession Listener
1.ServletCotext Listener
ServletContext Listener will be executed once your web application is deployed in your application server (Tomcat or etc). If you have any requirements that need to be executed before the application is started,ServletContext is the best place for you. Servletontext
Listener also detects when your web application is removed.
2.HttpSession Listener
HttpSession Listener deals with the HttpSession object. HttpSession object are always used in every web application and are very useful in maintaining the data as it is available throughout the lifecycle of the web application until it is invalidated or the user closes the browser.
31) what is the classpath for compiling servlet using tomcat5?
d:\tomcat5\common\lib\servlet-api.jar
32) How will you compile a servlet which uses a bean?
Assuming that our folder is d:\demos
give classpath to d:\demos (ie) current folder.
33) ,, if we use a package?
assuming that the package name is 'ourpack',
d:\demos\ourpack is the working folder.
give classpath to d:\demos (ie) parent folder.
34) What is a cookie? Where is it stored?
Cookie is a name-value pair text stored in user's hard-disk.for session-tracking.
35) what is a session object?
It is an object in server's memory.It usually lasts for 20 minutes.
36) what is an application object?
It is an object in server's memory and lasts till the server is shutdown.
37) what are the various scopes?
page, request, session, application
38) what is url rewriting?
The data is appended to the URL and sent back to the user.
39) what is session-tracking?
http is a stateless protocol (ie) data submitted by the user is not retained anywhere in server-side unless we make suitable arrangemnt.
This is known as session-tracking. This is required in applications in which conversational state has to be mainitained. (ex) shopping cart.
40) How will you send data from servlet to jsp?
RequestDispatcher rd =
request.getRequestDispatcher("/demo.jsp");
rd.forward(request, response);
41) ,, from servlet to servlet?
RequestDispatcher rd =
request.getRequestDispatcher("/servlet/demo");
rd.forward(request, response);
42) ,, from jsp to servlet?
RequestDispatcher rd =
request.getRequestDispatcher("/servlet/demo");
rd.forward(request, response);
43) ,, from jsp to jsp ?
RequestDispatcher rd =
request.getRequestDispatcher("/demo.jsp");
rd.forward(request, response);
44) ,, from jsp to jsp without using RequestDispatcher?
<jsp:forward page=”demo.jsp” />
45) difference between forward and redirect?
Forward will take the values to the forwarded page.
Redirect will leave all data behind and go to redirected page.
46) demo for setProperty, getProperty
package mypack;
public class mybean
{
String name;
String place;
mybean()
{
name="";
place="";
}
public void setName(String a) { name=a;}
public void setPlace(String b) { name=b;}
public String getName() { return name; }
public String getPlace() { return place; }
}
----
<jsp:useBean id='bean1' class='mypack.mybean' />
<%
String a = request.getParameter("text1");
bean1.setName(a);
String r = bean1.getName();
out.println(r);
%>
----
47) demo for servlet,functionbean and jsp chain
String a = request.getParameter("text1");
helper helper1 = new helper();
String r = helper1.help(a);
request.setAttribute("result", r);
RequestDispatcher rd =
request.getRequestDispatcher("/demo.jsp");
rd.forward(request,response);
In the jsp side
----------------
<%
String r = (String) request.getAttribute("result");
out.println(r);
%>
48) ,, ,, ,, , valuebean and jsp chain.
String a = request.getParameter("text1");
helperbean helper1 = new helper();
String r = helper1.help(a);
resultbean tom = new resultbean();
tom.setValue(r);
request.setAttribute("result", r);
RequestDispatcher rd =
request.getRequestDispatcher("/demo.jsp");
rd.forward(request,response);
49) How to embed applet in jsp?
The <jsp:plugin> directive takes care of generating all the HTML code necessary to embed and activate a Java applet. Consider the following example:
<jsp:plugin type="applet" code="demo.class"
name="demo" height="100" width="100">
</jsp:plugin>
The applet class file is placed in the same folder as the jsp.
Servlet is a java class file executed in Servlet-compliant webserver like Tomcat and result sent to browser(normally). It is also possible to invoke the servlet from an applet or frame. In such cases, the result will be sent to the applet or frame respectively.
(tunnel method).The invoking device may be a wap-browser also.
2) Define JSP
JSP can be viewed as html with interposed java code either as scriptlet or as expression.
3) What is the the problem with plain JSP?
In plain JSP without using bean, the business logic is exposed to the web-server administrator. So, this is not desirable.
4) what is the problem with Servlet?
It is very clumsy to create a dynamic page using Servlets. We will need a lot of out.println(...) statements.
5) What is path? classpath?
path is the folder(s) where exe/dll files for compiling/running the programs are available.
classpath is the folder(s) where class files are available.
6) Merits & demerits of JSP vs. Servlet
Servlets are more powerful. They can be used for sending binary output also.(ex) dynamic bar & pie-charts. They hide the business logic from prying eyes.
It is possible to use declarative management of servlet behaviour by entries in web.xml
JSP are easy to work with. The server compiles the JSP file by itself. But, for hiding the logic, we should use JSP beans.
7) How many versions of Tomcat?
Tomcat3.2, Tomcat4, Tomcat5 (the latest is 5.5).
8) What is special about Tomcat?
Tomcat is the reference implementation of Servlet & JSP API by SUN in collaboration with Apache Software Foundation. It is open-source & free.
9) What path is required for tomcat5 and tomcat4?
Tomcat5 & Tomcat4 require path as d:\jdk1.4.2\bin or higher.
10) What classpath is required to compile servlet, using tomcat4?
d:\tomcat4\common\lib\servlet.jar
11) What is a bean?
Any java class is a java bean. It has attributes, setter/getter methods for the attributes, public no-args constructor and any general functionality.
12) What is the difference between function bean & value bean?
Function bean does not have any attributes. So no need for setter/getter methods or constructor even, because there is nothing to initialize.
Value bean is a java class without any functionality but only attributes , setter/getter methods and public no-args constructor.
13) What is the important point to be noted when we use package?
The package statement should be the first statement in the source file.
14) give code for echo servlet.
import javax.servlet.*;
import java.io.*;
public class echoservlet extends HttpServlet
{
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
//-----------------
String s = request.getParameter("text1");
out.println(s);
}
}
15) how will you compile and test echo servlet in tomcat4?
>set path=d:\winnt\System32;d:\jdk1.4.2\bin
>set classpath=d:\tomcat4\common\lib\servlet.jar
>javac echoservlet.java
>edit echoservlet.htm
// echoservlet.htm
<html>
<body>
<form method=post action="/servlet/echoservlet" >
<input type=text name="text1">
<input type=submit>
</form>
</body>
</html>
copy echoservlet.htm to d:\tomcat4\webapps\root
copy echoservlet.class to
d:\tomcat4\webapps\root\WEB-INF\classes
make entry in d:\tomcat4\webapps\root\WEB-INF\web.xml
<servlet>
<servlet-name>echoservlet</servlet-name>
<servlet-class>echoservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>echoservlet</servlet-name>
<url-pattern>/servlet/echoservlet</url-pattern>
</servlet-mapping>
16) what is the action field if no package is used for servlet?
"/servlet/demo"
17) what is a context?
The folder in the same level as 'root folder .
(ex) d:\tomcat4\webapps\ourcontext
18) How will you create and test a new context in tomcat4?
Create a folder tomcat\webapps\ourcontext
create WEB-INF under ourcontext
create classes and lib folders under WEB-INF
copy web.xml from root\WEB-INF into ourcontext\WEB-INF
19) Where is web.xml located?
d:\tomcat4\webapps\root\WEB-INF
20) what is JSTL?
JSTL stands for JSP standard tag library. These are tags approved by JCP. These tags deal with
a) core
b) xml
c) sql
d) functions
e) formatting
21) What jar files are essential for using JSTL?
jstl.jar & standard.jar
22) What is the typical servlet-configuaration if no package is used?
<servlet>
<servlet-name>echoservlet</servlet-name>
<sefvlet-class>echoservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>echoservlet</servlet-name>
<url-pattern>/servlet/echoservlet</url-pattern>
</servlet-mapping
23) ,, if we use a package?
<servlet>
<servlet-name>echoservlet</servlet-name>
<sefvlet-class>mypack.echoservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>echoservlet</servlet-name>
<url-pattern>/servlet/mypack.echoservlet</url-pattern>
</servlet-mapping>
24) What is the URL if new context 'ourcontext' is used?
http://localhost:8080/ourcontext
25) How will you develop a JSP using a bean?
>md work
>cd work
work>edit greeter.java
package mypack;
public class greeter
{
public String greetme(String a)
{
return "hello..."+a;
}
}
>javac greeter.java
copy greeter.class to tomcat\webapps\root\WEB-INF\classes\mypack
create greeterjsp.jsp
<html>
<body>
<jsp:useBean id=bean1 class="mypack.greeter" />
<%
String a = request.getParameter("text1");
String r = bean1.greetme(a);
out.println(r);
%>
</body>
</html>
=========================
create greeterjsp.htm
<html>
<body>
<form method=post action="greeterjsp.jsp"
<input type=text name='text1'>
<input type=submit>
</form>
</body>
</html>
=========================
copy greeterjsp.htm & greeterjsp.jsp to:
tomcat\webapps\root
===========================
invoke by:
http://localhost:8080/greeterjsp.htm'
===========================
26) What is the advantage of tags?
Development of webpages is made easier if we use tags. As these tags are just similar to html tags, even non-programmers can use them and develop functioning web pages easily. And tags are re-usable. (ie) we can use tags developed by third party companies.
27) differentiate between scriptlet and expression.
<%
String s = request.getParameter("text1");
out.println(s);
%>
The above code is scriptlet.
The following line is expression.
<%= request.getParameter("text1") %>
28) what is a custom tag?
Tags are of two types.
a) JSTL ( JSP STANDARD TAG LIBRARAY)
b) custom tags .
These tags are developed by companies and are distributed either free or at a cost. There are very powerful tags, which make programming difficult tasks very easy. Such tags are known as custom tags. Jakarta tag library is famous in this respect.
29) What is a servlet-filter?
Servlet filters are powerful tools that are available to web application developers using the Servlet specification. Filters are designed to be able to manipulate a request or response (or both) that is sent to a web application. Almost every single web application will seriously benefit from using Servlet filters to both cache and compress content.
1.Cache filter:
A caching filter optimizes the time it takes to send back a response from your web server.
2.compression filter:
Compression filter optimizes the size of the content that you send from your web server to a user via the Internet.
30) What is a servlet-listener?
Listeners is basically pre-defined interfaces that are available for developers in the application lifecycle to achieve some tasks especially when dealing with the ServletContext as well as HttpSession objects.The two most widely used Servlet Listeners are
1.ServletContext Listener
2.HttpSession Listener
1.ServletCotext Listener
ServletContext Listener will be executed once your web application is deployed in your application server (Tomcat or etc). If you have any requirements that need to be executed before the application is started,ServletContext is the best place for you. Servletontext
Listener also detects when your web application is removed.
2.HttpSession Listener
HttpSession Listener deals with the HttpSession object. HttpSession object are always used in every web application and are very useful in maintaining the data as it is available throughout the lifecycle of the web application until it is invalidated or the user closes the browser.
31) what is the classpath for compiling servlet using tomcat5?
d:\tomcat5\common\lib\servlet-api.jar
32) How will you compile a servlet which uses a bean?
Assuming that our folder is d:\demos
give classpath to d:\demos (ie) current folder.
33) ,, if we use a package?
assuming that the package name is 'ourpack',
d:\demos\ourpack is the working folder.
give classpath to d:\demos (ie) parent folder.
34) What is a cookie? Where is it stored?
Cookie is a name-value pair text stored in user's hard-disk.for session-tracking.
35) what is a session object?
It is an object in server's memory.It usually lasts for 20 minutes.
36) what is an application object?
It is an object in server's memory and lasts till the server is shutdown.
37) what are the various scopes?
page, request, session, application
38) what is url rewriting?
The data is appended to the URL and sent back to the user.
39) what is session-tracking?
http is a stateless protocol (ie) data submitted by the user is not retained anywhere in server-side unless we make suitable arrangemnt.
This is known as session-tracking. This is required in applications in which conversational state has to be mainitained. (ex) shopping cart.
40) How will you send data from servlet to jsp?
RequestDispatcher rd =
request.getRequestDispatcher("/demo.jsp");
rd.forward(request, response);
41) ,, from servlet to servlet?
RequestDispatcher rd =
request.getRequestDispatcher("/servlet/demo");
rd.forward(request, response);
42) ,, from jsp to servlet?
RequestDispatcher rd =
request.getRequestDispatcher("/servlet/demo");
rd.forward(request, response);
43) ,, from jsp to jsp ?
RequestDispatcher rd =
request.getRequestDispatcher("/demo.jsp");
rd.forward(request, response);
44) ,, from jsp to jsp without using RequestDispatcher?
<jsp:forward page=”demo.jsp” />
45) difference between forward and redirect?
Forward will take the values to the forwarded page.
Redirect will leave all data behind and go to redirected page.
46) demo for setProperty, getProperty
package mypack;
public class mybean
{
String name;
String place;
mybean()
{
name="";
place="";
}
public void setName(String a) { name=a;}
public void setPlace(String b) { name=b;}
public String getName() { return name; }
public String getPlace() { return place; }
}
----
<jsp:useBean id='bean1' class='mypack.mybean' />
<%
String a = request.getParameter("text1");
bean1.setName(a);
String r = bean1.getName();
out.println(r);
%>
----
47) demo for servlet,functionbean and jsp chain
String a = request.getParameter("text1");
helper helper1 = new helper();
String r = helper1.help(a);
request.setAttribute("result", r);
RequestDispatcher rd =
request.getRequestDispatcher("/demo.jsp");
rd.forward(request,response);
In the jsp side
----------------
<%
String r = (String) request.getAttribute("result");
out.println(r);
%>
48) ,, ,, ,, , valuebean and jsp chain.
String a = request.getParameter("text1");
helperbean helper1 = new helper();
String r = helper1.help(a);
resultbean tom = new resultbean();
tom.setValue(r);
request.setAttribute("result", r);
RequestDispatcher rd =
request.getRequestDispatcher("/demo.jsp");
rd.forward(request,response);
49) How to embed applet in jsp?
The <jsp:plugin> directive takes care of generating all the HTML code necessary to embed and activate a Java applet. Consider the following example:
<jsp:plugin type="applet" code="demo.class"
name="demo" height="100" width="100">
</jsp:plugin>
The applet class file is placed in the same folder as the jsp.
