Friday, January 29, 2016

JSP

JSP


What are implicit objects in JSP?
Implicit objects in JSP are the Java objects that the JSP Container makes available to developers in each page. These objects need not be declared or instantiated by the JSP author. They are automatically instantiated by the container and are accessed using standard variables; hence, they are called implicit objects.The implicit objects available in JSP are as follows:
  • request
  • response
  • pageContext
  • session
  • application
  • out
  • config
  • page
  • exception
The implicit objects are parsed by the container and inserted into the generated servlet code. They are available only within the jspService method and not in any declaration.
What are scripting elements?
JSP scripting elements let you insert Java code into the servlet that will be generated from the current JSP page. There are three forms:
1. Expressions of the form that are evaluated and inserted into the output,
2. Scriptlets of the form that are inserted into the servlet’s service method,
3. Declarations of the form that are inserted into the body of the servlet class, outside of any existing methods.
What is a scriptlet?
A scriptlet contains Java code that is executed every time a JSP is invoked. When a JSP is translated to a servlet, the scriptlet code goes into the service() method. Hence, methods and variables written in scriptlets are local to the service() method. A scriptlet is written between the tags and is executed by the container at request processing time.
Is JSP technology extensible?
Yes. JSP technology is extensible through the development of custom actions, or tags, which are encapsulated in tag libraries.
How do I prevent the output of my JSP or Servlet pages from being cached by the browser?
You will need to set the appropriate HTTP header attributes to prevent the dynamic content output by the JSP page from being cached by the browser. Just execute the following scriptlet at the beginning of your JSP pages to prevent them from being cached at the browser. You need both the statements to take care of some of the older browser versions.
<%
response.setHeader(“Cache-Control”,”no-store”); //HTTP 1.1
response.setHeader(“Pragma”,”no-cache”); //HTTP 1.0
response.setDateHeader (“Expires”, 0); //prevents caching at the proxy server
%>
How can I set a cookie and delete a cookie from within a JSP page?
A cookie, mycookie, can be deleted using the following scriptlet:
  1.  <%
     //creating a cookie
     Cookie mycookie = new Cookie("aName","aValue");
     response.addCookie(mycookie);
     //delete a cookie
     Cookie killMyCookie = new Cookie("mycookie", null);
     killMyCookie.setMaxAge(0);
     killMyCookie.setPath("/");
     response.addCookie(killMyCookie);  %>
      1. How can I prevent the word “null” from appearing in my HTML input text fields when I populate them with a resultset that has null values?– You could make a simple wrapper function, like
         <%!
         String blanknull(String s) {
         return (s == null) ? "" : s;
         }
         %>
         then use it inside your JSP form, like
         <input type="text" name="shoesize" value="<%=blanknull(shoesize)% >" >
    What is JSP Include Directive?
    <%@ include file="relativeURL" %>
    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.
JSP life cycle?
JSP engine does the following 7 phases.
• Page translation: page is parsed, and a java file which is a servlet is created.
• Page compilation: page is compiled into a class file
• Page loading : This class file is loaded.
• Create an instance : Instance of servlet is created
• jspInit() method is called
• jspService is called to handle service calls
• jspDestroy is called to destroy it when the servlet is not required.

2 comments:

  1. The main motive of the Big data app development is to spread the knowledge so that they can give more big data engineers to the world.

    ReplyDelete
  2. Nice Blog, When I was read this blog, I learnt new things & it’s truly have well stuff related to developing technology, Thank you for sharing this blog. If Someone wants to know about Top Big Data Companies this is the Right place for you!

    ReplyDelete