Pdf jsp interview questions answers




















To handle exceptions thrown on the jsp page, you just need to set the error page. Then we will access the implicit exception objects in the JSP and will be able to pass our own error messages to the client usually more informative. How does error handling occur with jstl? The c: catch tag catches an exception and wraps it in a variable of exception, which we can handle in the c: if tag.

The jsp-config element is used to configure various parameters of jsp pages. There are two ways to ignore the execution of an expression language on a page:. If there are several 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.

For example, if we use one JSP page in web. Yes it is possible. Despite the fact that JSP is a server technology, it still creates an HTML page, to which you can also add javascript code and css styles. By default, the jsp page creates a session, but sometimes it is not needed.

We can use page directive with the session attribute to specify not to create a new session by default. To disable session creation, use the code shown below. The PrintWriter is the response object for recording the content in the response.

JspWriter uses the PrintWriter object imperceptibly to the user and provides buffering support. When the buffer is full or empty, JspWriter uses the PrintWriter object to write the content content to the response. The following tips are good practices for working with JSP technology:. Share on Facebook. Object References in Java. ESB Interview Questions. Please enter your comment! Please enter your name here. You have entered an incorrect email address! Popular Categories. What is Machine Learning?

The code is added to the body of the servlet class outside the service method. Directives for the servlet engine with basic settings information. A file on the local system that is included when translating a JSP to a servlet. Comment; ignored when translating a JSP page to a servlet. If you want the file to be connected during the page translation process, use the page directive along with the include attribute.

Attention: some servers require that the included files be in HTML or JSP format, depending on the server settings usually this restriction is based on specifying file extensions.

Sets the bean properties, either explicitly, or by pointing to the corresponding parameter value passed in at the request. Used to get the value of the request parameter, returns one value. Convenient for query parameters that contain multiple values. Used to get context init parameters. We cannot use them for servlet initialization parameters servlet init params.

For example: Another Example: file1. Answer : Yes. Preemptive termination of request processing on an error condition is a good way to maximize the throughput of a high-volume JSP engine. The trick assuming Java is your scripting language is to use the return statement when you want to terminate further processing. You can obtain the data for the FORM input elements via the request implicit object within a scriptlet or expression as.

Answer : Yes, there is. Under JSP 1. Answer : Typically, a default inactivity lease period for all sessions is set within your JSPengine admin screen or associated properties file. However, if your JSP engine supports the Servlet 2. This is done by invoking the HttpSession. Answer : 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.

The JSP page Bean1. Answer : No. You are supposed to make use of only a JSPWriter object given to you in the form of the implicit object out for replying to clients. A JSPWriter can be viewed as a buffered version of the stream object returned by response. Answer : No problem! The use Bean action specifies the beanName attribute, which can be used for indicating a serialized bean. A couple of important points to note.

Although you would have to name your serialized file "filename. Answer : Let's consider the answer to that from two different perspectives: that of an HTML designer and that of a Java programmer.

This is not the case. A JSP page is translated into a Java servlet and executed on the server. The resulting servlet is executed on the server. It is never visible to the user agent.

If you are a Java programmer, you can look at JSP technology as a new, higher-level means to writing servlets. The JSP environment takes your page and dynamically compiles it. Whenever a user agent requests that page from the Web server, the servlet that was generated from your JSP code is executed, and the results are returned to the user.

But it's a little trickier if you want the server to evaluate any JSP code that's inside the included file. If your data. This directive simply causes the contents of the specified file to be pasted into the JSP page at the place where the page contains this directive.

Irrespective of whether the included resource is a static resource or a JSP page - only the contents of the file are pasted and not the processed results. This directive is normally used for including static resources only - like, banner, header, footer, etc. Obviously if the specified page is a static resource then the contents of it are included as there won't be an executed result in that case. This action allows additional parameters to be passed via child element of this include action element.

Answer : By default, a JSP page will automatically create a session for the request if one does not exist. However, sessions consume resources and if it is not necessary to maintain a session, one should not be created.

For example, a marketing campaign may suggest the reader visit a web page for more information. If it is anticipated that a lot of traffic will hit that page, you may want to optimize the load on the machine by not creating useless sessions. Answer : A cookie, mycookie, can be deleted using the following scriptlet:. Answer : A Connection to a database can be established from a jsp page by writing the code to establish a connection using a jsp scriptlets.

Answer : request. Answer : When a JSP include directive is used, the included file's code is added into the added JSP page at page translation time, this happens before the JSP page is translated into a servlet.

While if any page is included using action tag, the page's output is returned back to the added page. This happens at runtime. This process of including a file is also called as static include. A Jsp Page, Include. What Happens When The Index. Answer : Compilation error, as two variables with same name can't be declared. This happens because, when a page is included statically, entire code of included page becomes part of the new page.

Hence compilation error. Can You Override Jspinit Method? If Yes, In Which Cases? Answer : yes, we can. Answer : include directive :. Answer : RequestDispatcher: server-side redirect with request and response objects. Create an instance of that class and put that instance in HttpSession.

What Is A Output Comment? Answer : A comment that is sent to the client in the viewable page source. You can see the comment by viewing the page source from your Web browser.

What Is A Hidden Comment? Answer : A comments that documents the JSP page but is not sent to the client. The JSP engine ignores a hidden comment, and does not process any code within hidden comment tags. The hidden comment is useful when you want to hide or "comment out" part of your JSP page. What Is A Expression? Answer : An expression tag contains a scripting language expression that is evaluated, converted to a String, and inserted where the expression appears in the JSP file.

Because the value of an expression is converted to a String, you can use an expression within text in a JSP file. What Is A Declaration? Answer : A declaration declares one or more variables or methods for use later in the JSP source file. A declaration must contain at least one complete declarative statement. You can declare any number of variables or methods within one declaration tag, as long as they are separated by semicolons. The declaration must be valid in the scripting language used in the JSP file.

What Is A Scriptlet? Answer : A scriptlet can contain any number of language statements, variable or method declarations, or expressions that are valid in the page scripting language. Within scriptlet tags, you can. Declare variables or methods to use later in the file see also Declaration. Write expressions valid in the page scripting language see also Expression.

Scriptlets are executed at request time, when the JSP engine processes the client request. If the scriptlet produces output, the output is stored in the out object, from which you can display it. What Are Implicit Objects? List Them? Answer : Certain objects that are available for the use in JSP documents without being declared first. These objects are parsed by the JSP engine and inserted into the generated servlet. Difference Between Forward And Sendredirect? Answer : When you invoke a forward request, the request is sent to another resource ohe server, without the client being informed that a different resource is going to process the request.

This process occurs completly 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. Because the browser issues a completly new request any object that are stored as request attributes before the redirect occurs will be lost.

This extra round trip a redirect is slower than forward. The JspPage interface declares only two mehtods - jspInit and jspDestroy that must be implemented by all JSP page regardless of the client-server protocol. The jspInit - The container calls the jspInit to initialize te servlet instance.

It is called before any other method, and is called only once for a servlet instance. The jspDestroy - The container calls this when it decides take the instance out of service. It is the last method called n the servlet instance. Singlethreadmodel Interface Or Synchronization? Answer : Although the SingleThreadModel technique is easy to use, and works well for low volume sites, it does not scale well. If you anticipate your users to increase in the future, you may be better off implementing explicit synchronization for your shared data.

The key however, is to effectively minimize the amount of code that is synchronzied so that you take maximum advantage of multithreading. The most serious issue however is when the number of concurrent requests exhaust the servlet instance pool. In that case, all the unserviced requests are queued until something becomes free - which results in poor performance.

Since the usage is non-deterministic, it may not help much even if you did add more memory and increased the size of the instance pool. Answer : JSP is used mainly for presentation only. Answer : Custom JSP tag is a tag you defined. You define how a tag, its attributes and its body are interpreted, and then group your tags into collections called tag libraries that can be used in any number of JSP files.

To use custom JSP tags, you need to define three separate components: the tag handler class that defines the tag's behavior ,the tag library descriptor file that maps the XML element names to the tag implementations and the JSP file that uses the tag library. JavaBeans are Java utility classes you defined. Beans have a standard format for Java classes.

You use tags. Custom tags and beans accomplish the same goals -- encapsulating complex behavior into simple and accessible forms. There are several differences. Custom tags can manipulate JSP content; beans cannot. Complex operations can be reduced to a significantly simpler form with custom tags than with beans.

Custom tags require quite a bit more work to set up than do beans. 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. Custom tags are available only in JSP 1. Difference Between Get And Post? The data is submitted as part of URL. The data is not visible on the URL and it is more secure. Answer : The session is an object used by a servlet to track a user's interaction with a Web application across multiple HTTP requests.

The session is stored on the server. These are good JSP interview questions and answers for experienced programmers as well as beginners. Before you go through the answers, I would recommend you to read above tutorials and try to answer them yourself and test your understanding of JSP. JSP is java server side technology to create dynamic web pages. We can create user views in servlet also but the code will become very ugly and error-prone.

Also, most of the elements on a web page are static, so the JSP page is more suitable for web pages. We should avoid business logic in JSP pages and try to use it only for view purpose. Just make the required changes in the JSP page and replace the old page with the updated jsp page in the deployment directory and the container will load the new JSP page.

Actually JSP container takes care of translating the JSP pages and create the servlet class that is used in web application.

JSP lifecycle phases are:. We should override jspInit methods to create common resources that we would like to use in JSP service method and override jspDestroy method to release the common resources. But in this case, we will have to configure it in deployment descriptor just like Servlets.

The sample configuration is given below code snippet of web. For example;. Since most of the times we print dynamic data in JSP page using out. JSP Declarations are used to declare member methods and variables of servlet class. We have 9 implicit objects that we can directly use in JSP page. JSP pageContext implicit object is instance of javax. PageContext abstract class implementation. We can use pageContext to get and set attributes with different scopes and to forward request to other resources.

We can configure init params for JSP similar to servlet in web. The only thing differs from servlet is jsp-file element where we need to provide the JSP page location. JSP pages are mostly used for view purposes and all the business logic should be in the servlet or model classes. Below is the sample code for this:.

We can disable scripting elements in JSP pages through deployment descriptor configuration like below. JSP action elements or action tags are HTML like tags that provide useful functionalities such as working with Java Bean, including a resource, forwarding the request and to generate dynamic XML elements. JSP action elements always starts with jsp: and we can use them in JSP page directly without the need to import any tag libraries or any other configuration changes.

Some of the important action elements are jsp:useBean, jsp:getProperty, jsp:setProperty, jsp:include and jsp:forward. The difference between JSP include directive and include action is that in include directive the content to other resource is added to the generated servlet code at the time of translation whereas with include action it happens at runtime.

When the included resource is static such as header, footer, image files then we should use include directive for faster performance but if the included resource is dynamic and requires some parameters for processing then we should use include action tag. Most of the times we use JSP for view purposes and all the business logic is present in servlet code or model classes. We also use request params, headers, cookies and init params in JSP to create response views.

We can use scriptlets and JSP expressions to retrieve attributes and parameters in JSP with java code and use it for view purpose. JSP Expression Language provides many implicit objects that we can use to get attributes from different scopes and parameter values. Note that these are different from JSP implicit objects and contains only the attributes in given scope. Most of the times, you can find them in the example projects and you can use them. These jars are container specific, for example in Tomcat, we need to include jstl.

Fortunately JSP is extendable and we can create our own custom tags to perform certain operations. We can add custom tag library in JSP page using taglib directive and then use it. This can be very useful for user when the number is really long.

So we want some custom tags like below:. Based on the number and format passed, it should write the formatted number in JSP page, for the above example it should print , All we need to do it to include it in the JSP page using taglib directive.

To handle exceptions thrown by the JSP page, all we need is an error page and define the error page in JSP using page directive. To create a JSP error page, we need to set the page directive attribute isErrorPage value to true, then we can access exception implicit object in the JSP and use it to send a customized error message to the client.

Below code snippet provide sample usage. If we have multiple servlet and servlet-mapping elements in deployment descriptor for a single servlet or JSP page, then container will initialize an object for each of the element and all of these instances will have their own ServletConfig object and init params. Yes why not, I have seen some developers getting confused with this.

We can use JSP page directive session attribute to indicate compiler to not create session by default. To disable the session creation, we can use it like below. PrintWriter is the actual object responsible for writing the content in response.

JspWriter uses the PrintWriter object behind the scene and provide buffer support.



0コメント

  • 1000 / 1000