Web Technology-Unit-3-Question Bank
Web Technology-Unit-3-Question Bank
Ans: JSP is an abbreviation for Java Servlet Page. It is a Server-Side Programming Language
used to create dynamic web-pages in the form of HTML. The JSP page is implicitly converted
into a servlet and it enables some additional features such as Expression Language, Custom
Tags, and many more.
Ans: The results of another page can be included by using the following methods:
Include Directive
Include Action
Ans: The most frequently used JSP Action Tags are as follows:
Ans: JSP is used in MVC in the presentation tasks. It is used as a view. The controller calls the
model and the business classes that get the data. This data is rendered to the JSP for rendering it
to the Client.
Ans: Context Initialization Parameters are the Initializing Parameters for the whole
application. They are not specific to any Servlet or a JSP. Context Initialization Parameters are
specified using the following syntax in a web.xml file.
1 <context-param>
2 <param-name>parametername</param-name>
3 <param-value>parametervalue</param-value>
</context-param>
4
Ans: There are mainly four scope values available for <jsp.useBean> tag.
page
request
application
session
Null
Boolean
String
Integer
Float
Ans: The major difference between ServletContect and PageContext is, the ServletContext is
designed to provide information about the Container and on the other hand, the PageContext is
designed to provide information about the Request.
Ans: The RequestDispatcher() and the context.getRequestDispatcher() are used for the
following purposes.
Q11. List down the major differences between the JSP Custom Tags and Java Beans.
Ans: The Major Differences between JSP Custom Tags and Java Beans are as follows:
Ans: Custom Tags in JSP are created using the following steps.
TLD stands for Tag Library Descriptor file. It contains the information related to the tag and
Tag Hander classes. It must be held inside the WEB-INF directory.
We will be specifying the path of the TLD file directly. It is recommended to use the URI name
instead of full a path of the TLD file. It uses taglib directive to use the tags defined in the TLD
file.
Q14. Mention the three important tags used in the development of JSP Bean.
Ans: The Three tags used in the JSP Bean development are as follows:
jsp:useBean
jsp:setProperty
jsp:getProperty
Q15. Can you disable the caching on the back button of a particular browser?
Ans: Yes, The Caching process can be disabled on the back button of the browser. To do so,
we can use the following code below.
Ans: The Web Container creates certain objects that include the information related to a
particular Request, Application or a Page. These Objects are called as Implicit Objects. The
Implicit Objects in the JSP are as follows:
Instructor-led Sessions
Real-life Case Studies
Assignments
Lifetime Access
Explore Curriculum
1. Request
2. Response
3. Application
4. Exception
5. Config
6. Page
7. Session
8. PageContext
9. Out
Q17. Can you stop Multiple Submits to a Web Page that are initiated by clicking to refresh
button?
Ans: Yes, This issue can be solved by using a Post/Redirect/Get or a PRG pattern.
A form filed by the user gets submitted to the server using POST/GET method.
The state in the database and business model are updated.
A redirect response is used to reply by the servlet for a view page.
A view is loaded by the browser using the GET command and no user data is sent.
This is safe from multiple submits as it is a separate JSP page.
Ans: Static pages can be included in a JSP using the include directive. This way the inclusion
is performed in the translation phase once. Note that a relative URL must be supplied for file
attribute. Although static resources may be included, it is not preferred as each request requires
inclusion.
Ans: We can stop errors in display in a JSP Page by setting up an “ErrorPage” attribute of the
PAGE directory to the name of the error page in the JSP page, and then in the error JSP page
set “isErrorpage=”TRUE”.
Ans: Yes, We can use a constructor in place of init() method. But it is not preferred because
init() was developed because earlier Java versions could not invoke constructors with arguments
dynamically. So they could not assign a servletConfig. However, servlet containers still call an
only no-arg constructor. So there is no access to servletContext or servletConfig.
jspInit()
_jspService()
jspDestroy
jspInit(): Container calls jspInit() method to initialize servlet instance. It is called once for the
servlet instance and preceded every other method.
_jspService(): Container calls _jspService() method for each request and passes it on to
the objects.
jspDestroy(): Container calls the jspDestroy() just before destruction of the instance.
Session: It is designed to show if any session data is available to the page or not.
Import: It is dedicated show packages that are imported.
isELIgnored: It shows whether EL expressions are ignored when JSP transforms into a
servlet.
contentType: It allows the user to specify the content-type of the page.
Ans: The Client-Side validation is done using JavaScript. The validation takes place within the
browser. Javascript is used to submit the data in the form when the validation
is successful. Validation errors do not require any extra network trip because the form cannot be
submitted if there are any errors.
Similar kind of data validation is carried out in the Server-Side after submission of the form. In
if the validation fails, then, the extra network trip is required to resend the form to the client to
refill the form with the correct data.
Ans: During the Translation Phase, the JSP engine translates and compiles a JSP file into
a servlet. This servlet moves to the execution phase where all the requests and responses are
handled. They are compiled for the first time. They are not accessed unless they are manually
compiled. The manual/explicit compilation is useful for long and convoluted programs.
1
2
3 class Student18 implements Cloneable{
int rollno;
4 String name;
5 Student18(int rollno,String name){
6 this.rollno=rollno;
7 this.name=name;
}
8 public Object clone()throws CloneNotSupportedException{
9 return super.clone();
10 }
11 public static void main(String args[]){
12 try{
Student18 s1=new Student18(101102,"Arjun");
13 Student18 s2=(Student18)s1.clone();
14 System.out.println(s1.rollno+" "+s1.name);
15 System.out.println(s2.rollno+" "+s2.name);
16 }
catch(CloneNotSupportedException c){
17
}
18 }
19 }
20
21
//Output:
101102 Arjun
101102 Arjun
Ans: The JSP declaration tag is used to declare fields and methods. The code written inside the
JSP declaration is enclosed in <%!%> tag. It is placed outside the service() method of the auto-
generated servlet.
Syntax:
1 <html>
2 <body>
3 <%! int data=50; %>
4 <%= "Value of the variable is:"+data %>
</body>
5
</html>
6
Q29. How are JSP(Java Server Pages) better than ASP(Active Server Pages)?
The dynamic part of the code is written in Java, not in Visual Basic or the Microsoft-
specific language. Hence, it is powerful and easier to use.
It is portable to other operating systems and Non-Microsoft Web servers.
Ans: Some of the Major Advantages of JSP over Pure Servlets are as discussed below:
It is more convenient to write and modify normal HTML than to have plenty
of println statements that generate the HTML.
Embedding of Java code in HTML pages.
Platform independence.
Creation of database-driven Web applications.
Server-side programming capabilities.
Advanced Level JSP Interview Questions
Ans: The autoFlush attribute is used to specify if a buffered output should be flushed
automatically when the buffer is filled, or whether an exception should be raised to indicate
buffer overflow. A value of true by default indicates automatic buffer flushing and a value
of false throws an exception.
Ans: isScriptingEnabled attribute determines if scripting elements are allowed for use or not.
The default value is true and it enables scriptlets, expressions, and declarations. If the attribute’s
value is set to false, a translation-time error will be raised if the JSP uses any
scriptlets, expressions/declarations.
Q33. What are the steps involved in reading data from a form using JSP?
Ans: The data parsing is a JSP is Automatic. It is done through the following steps depending
on the situation.
1. Creating a Cookie object: Cookie constructor is called with a cookie name and a
cookie value, both are strings.
2. Setting the maximum age: setMaxAge is used to specify the length of the cookie(in
seconds) should be valid.
3. Sending the cookie into the HTTP response headers: response.addCookie is used to add
cookies in the HTTP response header.
Ans: When a forward request is called, the request is sent to a different resource on the server,
without the client being informed that a different resource is going to process the request. This
process occurs completely with in the web container.
When a sendRedirtect method is invoked, it causes the web container to return to the browser
indicating that a new URL should be requested. Since the browser issues a completely new
request any objects that are stored as request attributes before the redirect occurs will be lost.
This extra round trip a redirect is slower than forward.
Q39. Why are JSP pages preferred for creating web-based client program?
Ans: JSP is preferred for creating web-based client program. Because
no plug-ins/security policy files are needed on the client systems whereas applet does. Also, JSP
pages enable cleaner and more module application design because they provide a way to separate
applications programming from web page design. This means personnel involved in web page
design do not need to understand Java programming language syntax to do their jobs.
Q40. How can you make the Finally Clause not to fail to execute?
Ans: It is possible to make the Finally Clause to not to fail by using System.exit(1); in the try
block.
Ans: SQLWarning objects are a subclass of SQLException that deal with database access
warnings. Warnings do not stop the execution of an application, as exceptions do; they simply
alert the user that something did not happen as planned. A warning can be reported on a
Connection object, a Statement object including PreparedStatement and CallableStatement
objects, or a ResultSet object. Each of these classes has a getWarnings method, which you must
invoke in order to see the first warning reported on the calling object.
1
2 SQLWarning warning = stmt.getWarnings();
if (warning != null){
3 while (warning != null){
4 System.out.println(\"Message: \" + warning.getMessage());
5 System.out.println(\"SQLState: \" + warning.getSQLState());
6 System.out.print(\"Vendor error code: \");
System.out.println(warning.getErrorCode());
7
warning = warning.getNextWarning();
8 }
9 }
10
Q42. Why Does Jcomponent have Add() And Remove() methods but the component does
not?
Ans: It is because, the JComponent is a subclass of Container, and it can contain other
components and JComponents. You can make your JSPs thread-safe by having them
implement the SingleThreadModel interface. This is done by adding the directive <%@ page
isThreadSafe=”false” % > within your JSP page
Q43. Explain some JSP Life-Cycle methods that can be overridden.
Ans: You can override the jspInit() and jspDestroy() methods within a JSP page. It is good
programming practice to free any allocated resources
within jspDestroy(). The jspInit() and jspDestroy() methods are each executed just once during
the lifecycle of a JSP page and are typically declared as JSP declarations:
Ans: Methods can be declared for use within a JSP page. The methods are invoked within any
other methods you declare, or within JSP scriptlets and expressions.
NOTE: Do note that you do not have direct access to any of the JSP implicit objects like
request, response, session and so forth from within JSP methods. However, you should be able to
pass any of the implicit JSP variables as parameters to the methods you declare.
Ans: The following code snippet shows how a servlet instantiates a bean and initializes it
with FORM data posted by a browser. The bean is then placed into the request, and the call is
then forwarded to the JSP page, Bean1.jsp, by means of a request dispatcher for downstream
processing.
1
2 public void doPost (HttpServletRequest request, HttpServletResponse response){
3 try {
4 govi.FormBean f = new govi.FormBean();
String id = request.getParameter("id");
5 f.setName(request.getParameter("name"));
6 f.setAddr(request.getParameter("addr"));
7 f.setAge(request.getParameter("age"));
8 f.setPersonalizationInfo(info);
9 request.setAttribute("fBean",f);
getServletConfig().getServletContext().getRequestDispatcher
10 ("/jsp/Bean1.jsp").forward(request, response);
11 }
12 catch (Exception ex) {
13 }
}
14
15
The JSP page Bean1.jsp can then process fBean, a fter first extracting it from the default request
scope via the useBean action.
1 jsp:useBean id="fBean" class="govi.FormBean" scope="request"/ jsp:getProperty n
2 property="name" / jsp:getProperty name="fBean"
3 property="addr" / jsp:getProperty name="fBean" property="age" / jsp:getProperty
property="personalizationInfo" /
4
Ans: A comment that documents the JSP page but is not sent to the client is known as a Hidden
comment. The JSP engine ignores a hidden comment and does not process any code within
hidden comment tags. A hidden comment is not sent to the client, either in the displayed JSP
page or the HTML page source. The hidden comment is useful when you want to hide or
“comment out” part of your JSP page.
You can use any characters in the body of the comment except the closing –%> combination. If
you need to use –%> in your comment, you can escape it by typing –%>.
JSP Syntax:
1 <jsp-property-group>
2 <url-pattern>*.jsp</url-pattern>
3 <scripting-invalid>true</scripting-invalid>
</jsp-property-group>
4
Ans: There are two ways to ignore the execution of an (EL) Expression Language on
a JSP page.
1 <jsp-config>
2 <jsp-property-group>
3 <url-pattern>*.jsp</url-pattern>
4 <el-ignored>true</el-ignored>
</jsp-property-group>
5
</jsp-config>
6
Ans: In the case, where there are multiple servlets and servlet-mapping elements in the
deployment descriptor for one servlet or JSP page, then the container initializes an object for
each element and each of these objects has its own ServletConfig object and initialization
parameters.
The following code snippet uses one JSP page in web.xml as shown below.
1 <servlet>
<servlet-name>Test</servlet-name>
2
<jsp-file>/WEB-INF/test.jsp</jsp-file>
3 <init-param>
4 <param-name>test</param-name>
5 <param-value>Test Value</param-value>
6 </init-param>
</servlet>
7
8 <servlet-mapping>
9 <servlet-name>Test</servlet-name>
10 <url-pattern>/Test.do</url-pattern>
11
12
</servlet-mapping>
13
14 <servlet>
15 <servlet-name>Test1</servlet-name>
16 <jsp-file>/WEB-INF/test.jsp</jsp-file>
17 </servlet>
18
19 <servlet-mapping>
<servlet-name>Test1</servlet-name>
20 <url-pattern>/Test1.do</url-pattern>
21 </servlet-mapping>
22
23
Ans: The JSP-config element is used to configure various parameters of JSP pages.
1 jsp-config>
2 <taglib>
3 <taglib-uri>https://www.edureka.co/jsp/tlds/mytags</taglib-uri>
4 <taglib-location>/WEB-INF/numberformatter.tld</taglib-location>
</taglib>
5
</jsp-config>
6