Top 50 JSP Interview Questions and Answers [Updated] Free Download
What is JSP?
JSP stands for JavaServer Pages. JSP is Java host-side technology to create dynamic web pages. JSP is an extension of Servlet technology to help developers create dynamic pages with HTML-like syntax.
The user creates drug user views in servlets as well, but the code becomes ugly and error-unerect. What is more, as most of the elements on a web foliate are electrostatic, therefore JSP page is suitable for web pages. Job logical system in JSP pages should beryllium avoided and must be used for view purposes only.
JSP Question Questions and Answers
Allow U.S.A now see touristed JSP Consultation Questions and Answers.
Question: What are the benefits of JSP?
Reply: Victimisation JSP scripting elements for writing Java codification in JSP pages should be avoided, and JSTL tags, JSP action elements, operating room custom tags should be used instead to achieve the same functionalities.
Other benefit of JSP is that most of the containers financial support the hot deployment of JSP pages. Shuffling the changes in the JSP page and replace the old page with the updated JSP foliate in the deployment directory, and the container loads the new JSP Page. We are non required to compile our project cipher or restart the server, unequal when making changes in servlet cipher, where we need to build the complete project again and deploy. Although most of the containers provide lucky deployment musical accompaniment for applications soundless, it's more work than JSP pages.
Dubiousness: Explain the lifecycle phases of JSP
Answer:
A typical JSP page code looks alike HTML and doesn't calculate anything same java classes. The JSP container takes care of translating the JSP pages and create the servlet class that is used in vane applications. Following are the phases of a JSP lifecycle:
- Translation: JSP codification is checked and parsed to father the servlet source code. For example, in Tom, made servlet class files are found at directory named TOMCAT/work/Catalina/localhost/WEBAPP/org/Apache/JSP. If home.jsp is the JSP page name, then ordinarily the generated servlet class describ is home_jsp, and the home_jsp.java is the file name.
- Compilation: In that phase angle, the JSP container compiles the JSP class source code, and a separate charge is produced.
- ClassLoading: In this phase, the container loads the class into memory.
- Instantiation: The no-args constructor of the generated class is invoked by the container to load it into memory and instantiate IT.
- Initialization: The init method acting of the JSP class object is invoked by the container, and the servlet config is initialized with init params organized in the deployment descriptor. JSP is ready to handle client requests after this form. Usually, translation to the low-level formatting of JSP happens when the first request for JSP comes. Still, we put up configure it to be loaded and initialized at the time of deployment, suchlike servlets using a payload-on-startup element.
- Call for Processing: This is the most extended lifecycle of the JSP page when the client requests are prepared. The processing is multi-threaded, similar to servlets, and a inexperienced thread is spawned for every application. Servlet requests and ServletResponse objects are created, and the JSP service method is invoked.
- Destroy: It is the last phase of the JSP lifecycle, where the JSP class is unloaded from memory. Usually, IT happens when the application is undeployed, or the server is shut down.
Query: Discuss JSP lifecycle
Resolution: JSP lifecycle consists of the next methods:
- jspInit(): It is a method that is declared in JspPage, and IT's implemented past JSP container implementations. The method acting is called once in the JSP lifecycle to initialize it with config params configured in the deployment signifier. We can override this method acting using the JSP resolution scripting chemical element to initialize any resources that we need to use in the JSP page.
- _jspService(): It is the JSP method acting that gets invoked by JSP container for each node request bypassing request and response object. Notice that the method name starts with an underscore to distinguish it from other lifecycle methods as this method cannot be overridden. All the JSP codification goes inside the method and is replaced by default. We should not examine to supercede it exploitation the JSP contract scripting component. The method acting is defined in the HttpJspPage interface.
- jspDestroy(): This method is called by the container when JSP is dud from memory, such as shutting down applications or box. The method is called only once in the JSP lifecycle, and this method acting is overridden to release resources created in JSP init method.
Question: Is it feasible to override the JSP lifecycle methods? If yes, which of the methods can be overridden?
Response: Yes, we rear override JSP lifecycle methods.Methods such as jspInit() and jspDestroy() can be overridden by using JSP declaration scripting element. We override jspInit() methods to create common resources that we would like to use in the JSP service method acting and override the jspDestroy() method to release the common resources.
Question: Mention the literals exploited in JSP
Answer: JSP uses the following literals;
- Null
- Boolean
- String
- Integer
- Float
Question: Inclination terzetto important tags used in the evolution of JSP Bean
Respond: The JSP Bean developing uses the following three tags as follows:
- jsp:useBean
- jsp:setProperty
- jsp:getProperty
Answer: There are two types of comments provided by JSP pages that we can use of goods and services:
- HTML Comments: We can use HTML comments like <!-- HTML Comment --> since JSP pages are like HTML. These comments are transmitted to the client as well and can live seen in the HTML reservoir. So we must invalidate debugging comments or code level comments using Hypertext mark-up language comments.
- JSP Comments: Scriptlets like <%-- JSP Comment --%> are used for writing JSP comments. These comments are not sent to the customer an are present in the generated servlet source code. We should use JSP comments for any cypher storey operating theater debugging info comments.
Question: Distinguish the scripting elements in JSP
Reply:
Expression, Scriptlets, and Declaration are scripting elements in the JSP varlet victimization which add coffee code in the JSP pages.
Any code written inside the scriptlet tags goes into the _jspService() method acting. A scriptlet tag begins with <% and ends with %>.For example;
<%
Date dt = new Date( );
System.out.println("Current Engagement=" +dt);
%>
There is a shortcut to printing process self-propelled data in the JSP page using the out.photographic print( ) method acting through JSP Expressions. JSP Expression begins with <%= and ends with %>.
<% out.impress("Saint John"); %> can live graphical using JSP Formulation as <%= "St. John the Apostle" %> Make a note that anything within <%= %> is sent every bit a parameter to the proscribed.print() method acting. Also, note that scriptlets fanny contain multiple java statements and always ends with a semicolon (;), but the expression doesn't end with a semicolon.
Declaration of appendage variables and methods of the servlet form are made victimization JSP Declarations. JSP Declarations begins and ends with " <%! " , " %>" severally.
For example, we make an int adaptable in JSP at the class degree as <%! public static int calculate=0; %>.
Question: Differentiate 'tween hiding point out and output comment.
Answer: The JSP comment is called shroud comment, whereas the HTML comment is known as the output comment. The JSP scuttlebutt bequeath non be shown in the case if a substance abuser views the source of the page, whereas HTML comments will personify displayed.
Doubtfulness: Why the use of scripting elements in JSP discouraged?
Answer: JSP pages are exploited for view purposes, and all the business logic should be in the servlet or model classes. We should pass parameters to the JSP page through attributes and then enjoyment them to make the HTML answer in the JSP page.
To the highest degree of the JSP Page contains Hypertext mark-up language code and to help web designers to understand JSP Sri Frederick Handley Page and develop them quickly, JSP technology provides sue elements, JSP Common Tag Library, JSP EL, and custom tags that we use rather than scripting elements to bridge deck the gap between JSP HTML and JSP Java part.
Question: What are the cinque types of JSTL tags?
Answer: They are categorized into five types supported the JSTL functions:
- Core Tags: Provides support for iteration, contrary to fact logic, catch the exclusion, URL, forward or airt reception, and more.
- Formatting and Localization Tags: Provided for the formatting of Dates, Numbers, and i18n support through with resource bundles and locales.
- SQL Tags: Provide support for interaction with relational databases such as Oracle, MySql, and more.
- XML Tags: Used to influence with XML documents such as parsing XML, transforming XML data and XPath manifestation evaluation.
- JSTL Functions Tags: JSTL tags provide single functions that we give the axe usage to perform the standard operation; nearly of them are for String manipulation so much as String Concatenation, Split String, and more.
Query: What is JSP Custom Tag, and what are the components of a JSP custom tag?
Answer: Action Tags, JSP EL, and JSTL tags are not plenty sometimes, and IT might tempt us to write encode in Java to perform some trading operations on the JSP page. Luckily, JSP is extendable, and we can create custom tags to deliver reliable services.
We force out make over JSP Custom Tags with the following components:
- JSP Custom Tag Handler
- Creating Tag Library Descriptor (TLD) File
- Deployment Descriptor Configuration for TLD
We can supply a customs tag library in the JSP page using a taglib directive and then use it.
Question: What are JSP implicit objects?
Answer: Objects created past the web container spell translating the JSP page to Servlet source to help developers are JSP implicit objects. We use these objects immediately in scriptlets that go in the religious service method. However, they can't be used in JSP Declaration because that code will go at the class even out.
We have nine inexplicit objects that we can straightaway use on the JSP page. 7 of them are declared A a local multivariate at the pop out of the _jspService() method, whereas two of them are take off of the _jspService() method argument that we can utilisation.
- out
- petition
- response
- config
- lotion
- session
- pageContext
- page
- exception
Question: We nates use JSP inexplicit objects in a method formed in the JSP Declaration? True or False? Excuse
Serve: False! Because these objects are local to service method, as advisable these JSP implicit in Objects are added by JSP Container. Also it is translating JSP page to servlet source code. JSP Declarations code went outside the service method acting, and It is used to create class level variables and methods. Thence can't expend JSP implicit objects.
Call into question: How to include static files in a JSP?
Answer: Static pages can embody included in a JSP victimization the include directive. The inclusion is performed once this way of life in the rendering phase. A relation URL must be supplied for the single file attribute. Though static resources English hawthorn Be included, it is not preferred as each request requires comprehension.
Doubtfulness: Explain Client-Side and Server-Root Proof.
Answer: The validation takes put together on the client-side inside the browser using JavaScript and HTML. The primary advantage of client-English validation is that information technology does not deman a network trip as it takes place on the client's machine, and thus near-instantaneous feedback English hawthorn be provided. On that point are extraordinary client-side constraints need to be applied to the input that cannot be handled past HTML. To validate a form using JavaScript. We add an event listener to the form so that we backside execute roughly JavaScript earlier form submission. Credit entry card information is a exemplary example of JavaScript client-side validation.
Server-side validation is trusted to execute without fail on totally input. The customer is supposed to wait for a response from the host before displaying any achiever or wrongdoing message to the user since the waiter can't execute its validation until it's received an HTTP postulation. In the case of validation loser, an duplicate web trip is needed to resend the form to fill out the form with the correct information by the client.
Question: Explain JSP declaration
Answer: Declaration fields and methods are done using the JSP resolve tag. The code is wrapped inside <%!%> tag and is placed outside of the Service( ) method acting of the car-generated servlet. Thence, it does not develop memory at for each one asking.
Phrase structure:
<%! field or method %>
Example
<hypertext mark-up language>
<body>
<%! int val=50; %>
<%= "Time value of the variable is:"+val %>
</body>
</html>
Question: Severalize between JSP Scriptlet and JSP Declaration.
Answer:
| JSP Scriptlet | JSP Declaration |
| Information technology can only if announce variables and non methods. | It can declare both methods and variables. |
| It is placed wrong _jspService( ) method acting. | It is placed outside _jspService( ) method acting. |
Interrogative sentence: What are the advantages of JSP all over ASP?
Answer:
- It is sinewy and easier to use as the dynamic disunite of the cipher is written in Java, not Visual Basic or the Microsoft-specific language.
- It is portable to Not-Microsoft Web servers and other operating systems.
Question: Define the JSP custom tag and its components.
Answer: Custom tags are user-formed tags; we can delimit our tags with our functionality and use those tags interior the JSP page.
It offers the following advantages:
- Information technology eliminates the need for the scriptlet give chase.
- Separation of business logic from the JSP page
- Reusability of the JSP page.
It consists of the tailing components:
- Custom tag coach class.
- Create a TLD (Tag Library Descriptor) file.
- Deployment Descriptor Configuration for TLD
We can add a custom tag library in the JSP page victimisation a taglib directive and past use it.
Dubiousness: Can JavaScript be put-upon with JSP pages?
Answer: Yes, we prat, even though JSP is a server-side technology, it's utilised to generate a client-broadside response, and we prat add javascript Beaver State CSS encipher like any other HTML page.
Doubt: How to configure init params for JSP?
Answer: We can configure init params for JSP similar to the servlet in web.xml register; Also it is necessary to configure JSP init params with servlet and servlet-mapping element. The only matter that differs from the servlet is a jsp-file element, where we need to bring home the bacon the JSP paginate positioning.
Question: Signalize between including Directional and include Carry out in JSP
Answer:
| Include Directive | Include Action |
| It is processed at translation time. | Information technology is processed at run sentence. |
| IT uses a relative or unconditional way of life | It uses a congeneric itinerary. |
| No other parameters can be passed. | Another parameters can be passed. |
| It includes the contents of resources only and will not process the dynamic resource. | It processes the dynamic resource, and the solution will be added to calling JSP |
| Passing some request or response object to calling jsp to included file or JSP or vice versa is not allowed. | Passing any request or response targe to calling jsp to included file or JSP or vice versa is allowed. |
Doubt: What are JSP Action Elements OR Action Tags? Explain
Serve: Tags provide useful functionalities like including resources, functional with Java Bean, furtherance the request, and to generate high-octane XML elements. Action elements start with jsp: and can be old in JSP pages directly without the need to import any tag libraries or any other constellation changes. The JSP tags are:
| JSP Tags | Description |
| jsp:forward | forrad the response and asking to another resource. |
| jsp:include | includes some other resource |
| Jsp: use of bean | Locates or creates bean objects |
| jsp:setProperty | value of the property is set in the bean physical object |
| jsp:getProperty | prints the value of the property of the noggin |
| jsp:plugin | embeds another component like an applet |
| jsp:param | sets the parameter value. It is used in forrard and includes largely. |
| jsp:pullout | can be secondhand to mark the message if the plugin is working. Information technology is used in jsp: plugin. |
Oppugn: What is JSP Aspect Language(EL)?
Resolve: JSP EL simplifies the accessibility of data stored in Java bean components and past objects like request, application, and session.
Syntax:
$
Question: How to utilization JSP EL to get the HTTP method name?
Suffice: pageContext JSP Elevation absolute object is used to get the request physical object acknowledgment, and the dot operator is used to get the HTTP method refer in the JSP foliate.
Below is the code snippet for the same:
$
Question: Can a JSP extend another java class? If yes, how?
Answer: Yes, JSP can extend another JSP victimization this
<%@ let in page extends="classname" %>
it's a correct because when JSP is regenerate to servlet its implements javax.servlet.jsp.HttpJspPage interface, so jsp Thomas Nelson Page extends another java class.
Question: How does JSP communicate with Java files?
Answer: The import chase after
<%@ page import="market.hackneyed.*" %>
like this, we meaning all the java files to jsp and habituate them as a regular grade. The past way is servlet can send the instance of the java class to jsp, and that physical object can be retrieved from the postulation object and wont IT happening the page.
Question: Which directive is used in the JSP custom tag along?
Answer: JSP taglib directive.
Doubtfulness: Name the three tags used in JSP development.
Answer:
- jsp:useBean
- jsp:setProperty
- jsp:getProperty
Answer:
<%-- Comment --%>
Question: Is JSP technology extensible? How?
Respond: Yes, JSP technology can atomic number 4 extensible done the growth of tags surgery custom actions that are encapsulated in tag libraries.
Question: How can buoy a wander-safe JSP page make up implemented?
Answer: By having JSPs implement the SingleThreadModel interface, we can make them thread-invulnerable. This john be finished by adding the directive <%@ page isThreadSafe="fake" %> inside the JSP Thomas Nelson Page.
Oppugn: Define JSP expressions
Answer: Scripting language face contained inside a JSP expression ingredient is premiere evaluated, so born-again to a String, and then inserted where the formulation appears in the JSP lodge.
The expression element may arrest whatsoever reflexion that is valid according to the Java Language Specification, but semicolon cannot constitute used to end an expression.
Syntax:
<% formulation %>
Question: Delimit the JSP directive?
Answer: The whole structure of the servlet class is affected by a JSP directive. IT has the following form:
<%@ guiding attribute = "value" %>
Question: State the types of directive tags?
Answer:
| Directive Tag | Description |
| <% @page … %> | Defines page conditional attributes. |
| <% @include … %> | File is enclosed during the translation phase. |
| <% @taglib ... %> | Declares a tag library, containing custom actions, utilized in the page. |
Question: Define JSP literals
Respond: Literals are circumscribed as the values, so much as a text string or a number, that are written as part of the write in code.
JSP literals are:
| JSP Literals | Description |
| Boolean | True or unharmonious |
| String | with single and two-bagger quotes; " is escaped as \," ' is loose as \,' and \ is loose as \\. |
| Whole number | As in Java |
| Floating Sharpen | As in Java |
| Null | null |
Question: How are JSP runtime exceptions handled?
Answer: Exception is defined As an abnormality thrown at runtime, and the swear out handling the runtime errors is called elision handling.
In JSP the exceptions are handled in two ways:
- Aside errorPage and isErrorPage attributes of page directive
- By <error-page> element in WWW.xml single file
Question: What is the page directive?
Answer: The page directive provides instructions to the container that pertain to the current JSP page. Page directives posterior be coded anywhere on the JSP page.
Interrogative sentence: Wherefore does the _jspService() method starts with an '_' while other life cycle methods do not?
Solution: jspInit() jspDestroy() and _jspService() are main JSP living bike method, by default whatever content is shorthand in JSP page goes within the _jspService() method by the container if again tries to nullification this method JSP compiler leave give an error but we can override other two life bike method atomic number 3 implementing this two in JSP so making this departure container utilize _ in jspService() method and shows that the method cannot be overridden.
Question: How is JSP used in the MVC model?
Answer: JSP is secondhand for display in the Model View Controller pattern (MVC ); it way it plays the role of the view. The controller calls the model and the business classes, which in turn amaze the information, and this data is then presented to the JSP for interpreting on to the client.
Question: Can the jspInit(), _jspService() and jspDestroy() methods make up overridden?
Answer: We cannot overrule _jspService() method but jspinit() and jspDestroy() methods can be overriden.
Question: When to use application scope?
Answer: It is put-upon when we want to make data available to the entire application.
Doubtfulness: Differentiate 'tween JavaBeans and taglib directives
Solvent: JavaBeans and taglib were introduced for reusability. The following are the major differences between them −
- JavaBeans are good for storing information and state, whereas Taglibs are for generating presentation elements.
- Use custom tags to implement actions and JavaBeans to show information.
Question: How is information is passed from JSP to included JSP?
Answer: Using
<%jsp:param>
Question: Define the auto-refresh feature
Answer: Imagine a webpage displaying endure gimpy score or timeworn market status Beaver State up-to-dateness exchange ratio. The webpage inevitably to be refreshed regularly using the freshen up or recharge button with your web browser for all such types of pages.
JSP makes this job easy by providing a mechanism where a webpage lavatory be made in so much a way that it would freshen up mechanically after a given interval.
Question: How is the auto-refresh implemented in JSP?
Answer: The simplest way of refreshing a Web Page is using the method acting setIntHeader() of the response object. Following is the signature of this method −
public void setIntHeader(Drawing string head, int header esteem)
This method sends back header "Refresh" to the browser along with an integer economic value, which indicates the time interval in seconds.
Question: Define JSTL SQL tags?
Answer: The JSTL SQL tag library provides tags for interacting with relative databases (RDBMSs) such as Oracle, MySQL, or Microsoft SQL Waiter.
Pursuing is the syntax to include JSTL SQL subroutine library in your JSP −
<%@ taglib prefix = "sql" uri = "http://java.sun.com/jsp/jstl/sql" %>
Question: What typecast of errors give the axe a JSP write in code encounter?
Reply:
- Chequered exceptions: An exception that is a user error or a problem that cannot be foretold by the programmer. For example, if a file is to glucinium opened, but the file cannot be found, an exception occurs. These exceptions are difficult to ignore.
- Runtime exceptions: An exception that occurs could probably have been avoided by the coder. Runtime exceptions are ignored at the time of compiling.
- Errors: These are not considered as exceptions, only the problems that arise beyond the control of the user or the programmer. Errors are ignored in your encode because you can seldom do anything almost an error. E.g., if a heap up overflow occurs, an wrongdoing bequeath come up. They are also neglected at the time of compilation.
Question: How to disable scripting?
Answer: We can disable scripting aside setting the scripting-invalid element of the deployment descriptor to actual. It is a subelement of the jsp-property-aggroup. Its valid values are true and false. The syntax for disabling scripting is:
<jsp-property-group>
<url-shape>*.jsp</url-practice>
<scripting-disable>honest</scripting-invalid>
</jsp-property-group>
Conclusion
It can be disagreeable to ready for an interview and so here we present you with the most popular JSP Interview questions for you to crack your JSP interview. Do you have whatsoever more questions to share? Or experience any tips to crack this interview?
JSP, Servlets, and JDBC for Beginners: Body-build a Database App is about of the popular Udemy courses when preparing for a JSP interview.
Seeking a JSP question questions book? Refer to this to sharpen your JSP Skills: Top 1000 Java Interview Questions: Includes Natural spring, Hibernate, Microservices, GIT, Maven, JSP, AWS, Cloud Computing: (in vogue 2018 edition).
Let USA acknowledge in the comments below!!
People are also reading:
- PHP Interview Questions
- Python Question Questions
- Technical Consultation Questions &A; Answers
- C Consultation Questions & Answers
- Java Interview Questions
- AWS Interview Questions
- Difference between coding vs programming
- Which Programming Languages is best for getting Jobs?
- How to become a Backend Developer?
DOWNLOAD HERE
Top 50 JSP Interview Questions and Answers [Updated] Free Download
Posted by: jenkinsthelarm56.blogspot.com
Post a Comment for "Top 50 JSP Interview Questions and Answers [Updated] Free Download"