JavaNinja's Profile
Ninja
100000380
Points

Questions
185

Answers
186

  • Ninja Asked on 18th September 2018 in Jsp.

    Caching in JSP pages can be prevented by setting the appropriate HTTP header attributes.

    The following scriptlet defined at the beginning of JSP pages can prevent them from being cached at the browser.

    <%
     response.setHeader("Cache-Control","no-cache"); //HTTP 1.1
     response.setHeader("Pragma","no-cache"); //HTTP 1.0</p>
     
     //prevents caching at the proxy server
     response.setDateHeader("Last-Modified", (new Date()).getTime() ); 
    %>
    

    This answer accepted by JavaNinja. on 18th September 2018 Earned 15 points.

    • 1325 views
    • 1 answers
    • 0 votes
  • Ninja Asked on 18th September 2018 in Jsp.

    <%@ include file=”filename” %> is the JSP include directive. The include directive, includes the content of the specified file during the translation phase–when the page is converted to a servlet. At JSP page translation time, the content of the file given in the include directive is ‘pasted’ as it is, in the place where the JSP include directive is used.

    The include directive is used to statically insert the contents of a resource into the current JSP. Use the include directive if the file changes rarely. Generally JSP include directive is used to include header banners and footers.

    <jsp:include page=”relativeURL” /> is the JSP standard include action element. The include action, includes the response generated by executing the specified page (a JSP page or a servlet) during the request processing phase–when the page is requested by a user. At runtime, the included file will be ‘executed’ and the result content will be included with the source JSP page. The include standard action enables the current JSP page to include a static or a dynamic resource at runtime.

    Use the include action only for content that changes often, and if which page to include cannot be decided until the main page is requested.

    This answer accepted by JavaNinja. on 18th September 2018 Earned 15 points.

    • 1687 views
    • 1 answers
    • 0 votes
  • Ninja Asked on 18th September 2018 in Jsp.

    There are two ways for browser redirection in JSP:

    1. Using response.sendRedirect()
    2. Using <jsp:forward /> element

    A redirect is a totally different from a forward. The response.sendRedirect() creates a new request object which doesn’t carry any of old requests. The request handling JSP page tells the browser to make a new request to the target servlet or JSP page. The URL shown in the browser therefore changes to the URL of the new page when you redirect.

    The <jsp:forward /> standard action is actually a call to HttpServletRequest.forward(String url) which passes the request object within the server to either a servlet or a JSP page. The new servlet or JSP page continues to process the same request and the browser is not aware of the fact that more than one servlet or page is involved. In other words, the client is not aware that the request is being forwarded anywhere. The URL shown in the browser stays unchanged when you do forward.

    This answer accepted by JavaNinja. on 18th September 2018 Earned 15 points.

    • 1290 views
    • 1 answers
    • 0 votes
  • Ninja Asked on 18th September 2018 in Jsp.

    Objects in a JSP, whether explicit or implicit, are accessible within a particular scope. In the case of explicit objects, such as a JavaBean instance created in a <jsp:useBean> action statement, you can explicitly set the scope using the attribute scope=”scopevalue”.

    There are four scopes available in JSP:

    1. page” scope – The object is accessible only from within the JSP page where it was created.
    2. request” scope  – The object is accessible from any JSP page servicing the same HTTP request that is serviced by the JSP page that created the object.
    3. session” scope – The object is accessible from any JSP page sharing the same HTTP session as the JSP page that created the object.
    4. application” scope – The object is accessible from any JSP page used in the same Web application (within any single Java virtual machine) as the JSP page that created the object.

    This answer accepted by JavaNinja. on 18th September 2018 Earned 15 points.

    • 1405 views
    • 1 answers
    • 0 votes
  • Ninja Asked on 18th September 2018 in Jsp.

    JSP provides a bunch of standard action tags that can be used for specific tasks such as working with Java bean objects, including other resource, forwarding the request to other resource etc. JSP Standard action tags are represented using XML elements with a prefix of jsp.

    The list of standard JSP action elements in JSP are:

    1. <jsp:useBean /> – To get an instance of a Java object defined within a given scope and a given id.
    2. <jsp:setProperty /> -Sets the values of properties in a bean.
    3. <jsp:getProperty /> – Used to place the value of a bean instance property converted to a String into the implicit object out from which the value can be displayed as output.
    4. <jsp:include /> – Provides for the inclusion of static and dynamic resources in the same context as the current page.
    5. <jsp:forward /> – Allows the runtime dispatch of the current request to another resource in the same context. A <jsp:forward> tag effectively terminates the execution of the current page.
    6. <jsp:param /> – Used to provide key/value information.  This element is used in the jsp:include, jsp:forward, and jsp:params elements.
    7. <jsp: plugin /> – The plugin action enables to generate HTML that contains the appropriate client browser dependent constructs like OBJECT or EMBED for the Java plugins.
    8. <jsp:attribute />– Defines the value of an action attribute in the body of an XML element instead of in the value of an XML attribute.
    9. <jsp:body /> – Defines the body of a standard or custom action tag.
    10. <jsp:element /> –  Used to dynamically define the value of the tag of an XML element.
    11. <jsp:text /> – Used to enclose template data in a JSP page.

    This answer accepted by JavaNinja. on 18th September 2018 Earned 15 points.

    • 1229 views
    • 1 answers
    • 0 votes
  • Ninja Asked on 18th September 2018 in Design Pattern.

    Proxy design pattern allows you to create a wrapper class over real object. Wrapper class which is proxy, controls access to real object so in turn you can add extra functionalities to real object without changing real object’s code. Some of the situations when the Proxy pattern is used are:

    1. Remote Proxy: Represents an object locally which belongs to a different address space. A stub in RMI is an example of proxy implementation in Java. The stub provides a local representation of the object which is present in the different address location. Web services providing interfaces accessing a remote implementation is also an example for Proxy pattern.
    1. Virtual Proxy: Used in place of a complex or heavy object. When an underlying object is huge in size, just represent it using a virtual proxy object and on demand load the real object. This provides lazy initialization to create expensive objects on demand.
    1. Protection Proxy: Controls access to the original object. Protection proxies are useful when objects should have different access rights. An example for this is the Proxy server which provides restrictive access to internet inside work place.
    1. Smart Reference: Provides additional actions when the proxy object is accessed. For example smart reference helps to keep track (count) of the number of references that are held for a particular object.

    This answer accepted by JavaNinja. on 18th September 2018 Earned 15 points.

    • 1348 views
    • 1 answers
    • 0 votes
  • Ninja Asked on 18th September 2018 in Design Pattern.

    There is a built-in support for proxy pattern from JDK 1.3 onwards. The classes java.lang.reflect.Proxy and java.lang.reflect.InvocationHandler introduced in JDK 1.3 directly supports proxy pattern.

    Proxy provides static methods for creating dynamic proxy classes and instances. InvocationHandler is an interface that has a single invoke(Object proxy, Method method, Object[] args) method. This method is implemented by the invocation handler of a proxy instance. Each proxy instance has an associated invocation handler. When a method is invoked on a proxy instance, the method invocation is encoded and dispatched to the invoke method of its invocation handler.

    This answer accepted by JavaNinja. on 18th September 2018 Earned 15 points.

    • 1322 views
    • 1 answers
    • 0 votes
  • Ninja Asked on 18th September 2018 in Design Pattern.

    Proxy pattern is a structural pattern where a class represents the functionality of another class. A proxy, in its most general form, is a class functioning as an interface to something else. Proxy pattern intent according to GoF is “Provide a surrogate or placeholder for another object to control access to it”.

    The following example demonstrates “Virtual Proxy” pattern. In the example, actual class is RealAudio and ProxyAudio is used as a proxy for RealAudio. Using the proxy pattern, the ProxyAudio avoids multiple loading of the sound from the disk  in a memory-saving manner.

    public interface Audio {

    public void playAudio();

    }

     

    public class RealAudio implements Audio{

    public RealAudio() {

    System.out.println(“Playing real audio…..”);

    }

    @Override

    public void playAudio() {

    System.out.println(“Playing audio….”);

    }

    }

     

    public class ProxyAudio implements Audio{

    RealAudio real;

    @Override

    public void playAudio() {

    if(real == null){

    real = new RealAudio();

    }

    real.playAudio();

    }

    }

     

    public class TestProxy {

    public static void main(String[] args) {

    Audio audio = new ProxyAudio();

    audio.playAudio();

    audio.playAudio();

    }

    }

    Output:

    Playing real audio…..

    Playing audio….

    Playing audio….

    Proxy pattern can be used when one of the following is required in the application:

    1. The object being represented is external to the system.
    2. Objects need to be created on demand.
    3. Access control for the original object is required.
    4. Added functionality is required when an object is accessed.

    This answer accepted by JavaNinja. on 18th September 2018 Earned 15 points.

    • 1444 views
    • 1 answers
    • 0 votes
  • Ninja Asked on 18th September 2018 in Design Pattern.

    Flyweight Pattern Example:
    java.lang.Integer.valueOf(int)

    Integer class has a valueOf(int) method that returns a cached instance of Integer representing the specified int value without creating new object every time. This method is likely to yield significantly better space and time performance by caching frequently requested values. Similarly the valueOf(type) method in the classes Boolean, Byte, Character, Short, Float, Double and Long uses Flyweight pattern.

    Template Method Pattern Example:
    java.io.InputStream.read(byte b[], int off, int len)

    The read(byte[] b, int off, int len) method for class InputStream simply calls the method read() repeatedly. The read() method is defined as abstract method in this class and subclasses of InputStream (like FileInputStream, BufferedInputStream….) must provide an implementation for reading a byte of data for the appropriate stream in this method.

    Similarly the write(byte[], int, int) method in java.io.OutputStream uses Template method pattern.

    This answer accepted by JavaNinja. on 18th September 2018 Earned 15 points.

    • 1247 views
    • 1 answers
    • 0 votes
  • Ninja Asked on 18th September 2018 in Design Pattern.

    The Template Method pattern is used when

    1. You want to avoid code duplication by reusing an algorithm
    2. The behavior of an algorithm can vary, you let subclasses implement the behavior through overriding

    This answer accepted by JavaNinja. on 18th September 2018 Earned 15 points.

    • 3085 views
    • 1 answers
    • 0 votes