{"id":3498,"date":"2020-06-02T20:01:52","date_gmt":"2020-06-02T14:31:52","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=3498"},"modified":"2020-06-02T20:01:55","modified_gmt":"2020-06-02T14:31:55","slug":"jsp-implicit-objects","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/jsp-implicit-objects\/","title":{"rendered":"JSP Implicit Objects"},"content":{"rendered":"\n<p>JSP implicit objects are created by JSP Engine during translation JSP to Servlet(translation phase). We can use them directly in JSP pages without initializing or declaring them because they are predefined.\u00a0 There are nine JSP implicit objects: page, out, request, response, session, application, config, pageContext and Exception. Let&#8217;s look closer at them.<\/p>\n\n\n\n<p>Object <strong>out<\/strong> is an instance of class javax.servlet.jsp.JspWriter. It is used for the output content for client response. It contains methods for writing the value which has been passed them, such as:&nbsp;<\/p>\n\n\n<p>[box type=&#8221;shadow&#8221; align=&#8221;&#8221; class=&#8221;&#8221; width=&#8221;&#8221;]void print();<br \/>void println();<br \/>void newLine();[\/box]<\/p>\n\n\n<p><strong><em>Here is the example of use:<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;%@ page language=<em>\"java\"<\/em> contentType=<em>\"text\/html; charset=ISO-8859-1\"<\/em>\n\u00a0\u00a0\u00a0\u00a0pageEncoding=<em>\"ISO-8859-1\"<\/em>%>\n&lt;!DOCTYPE html>\n&lt;html>\n&lt;head>\n&lt;meta charset=<em>\"ISO-8859-1\"<\/em>>\n&lt;title>Test out implicit object&lt;\/title>\n&lt;\/head>\n&lt;body>\n&lt;%\nout.println(\"line 1\");\nout.newLine();\nout.print(\"line 2\");\n%>\n&lt;\/body>\n&lt;\/html><\/pre>\n\n\n\n<p><strong><em>The output will be:<\/em><\/strong><\/p>\n\n\n\n<p><em>line 1<\/em><br><em>line 2<\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The out implicit object has more methods:<\/h3>\n\n\n\n<p><em>void clear()<\/em> &#8211; clears the output buffer without giving possibility to write the content to the client. It is called as: <em>out.clear()<\/em>;<\/p>\n\n\n\n<p><em>void clearBuffer()<\/em>&#8211; method is similar to the previous clear() method. The difference is that in the case of invoking out.clear() we are already flushed buffer and it throws an exception. In case of out.clearBuffer() it doesn\u2019t.<\/p>\n\n\n\n<p><em>void flush() <\/em>&#8211; method clears the buffer just like clear() method, but content is forcing to write to the output before flushing it.<\/p>\n\n\n\n<p><em>boolean isAutoFlush() <\/em>:&nbsp; can return true or false boolean value. It returns true if the buffer is automatically flushed.<\/p>\n\n\n\n<p><em>int getBufferSize()<\/em> &#8211;\u00a0 the output of this methods is the size of <a href=\"https:\/\/www.computerhope.com\/jargon\/o\/outputbu.htm\" rel=\"nofollow noopener\" target=\"_blank\">output buffer<\/a> in bytes.<em>int getRemaining() <\/em>&#8211; the method output is the number of bytes remaining before hitting the buffer overflow condition.<\/p>\n\n\n\n<p>The <strong>request<\/strong> implicit object (javax.servlet.http.HttpServletRequest) contains the data that has been sent to the JSP page by the client. A user is prompted to log in and signup forms in JSP,\u00a0 to fill in some details. All this data is sending to JSP as a request object.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The request Implicit Object contains methods:<\/h3>\n\n\n\n<p><em>getParameter(String name) <\/em>&#8211; method to get the value of a particular request parameter. For example, a user enters the username and password at the login page. After verification, the user is redirected to another page, then using a method request.getParameter we can get the value of username and password:<\/p>\n\n\n<p>[box type=&#8221;shadow&#8221; align=&#8221;&#8221; class=&#8221;&#8221; width=&#8221;&#8221;]String Uid= request.getParameter(&#8220;username&#8221;);<br \/>String Pass= request.getParameter(&#8220;password&#8221;);[\/box]<\/p>\n\n\n<p><em>getParameterNames()<\/em> \u2013 method returns an enumeration of all the parameter names from the request. The example of use:<\/p>\n\n\n\n<p>Enumeration e= request.getParameterNames();<\/p>\n\n\n\n<p><em>getParameterValues(String name)<\/em> \u2013 method returns the all values of a specific parameter. The example of use:<\/p>\n\n\n\n<p>String[] allpasswords = request.getParameterValues(&#8220;password&#8221;);<\/p>\n\n\n\n<p><em>getAttribute(String name) <\/em>\u2013 method is used to get the attribute value.&nbsp; request.getAttribute(\u201cname\u201d)&nbsp;<\/p>\n\n\n\n<p><em>getAttributeNames() <\/em>\u2013 method gets the attribute names from the current session. It returns the enumeration.<\/p>\n\n\n\n<p><em>setAttribute(String,Object)<\/em> \u2013 method to assign an object value to a specific attribute.<\/p>\n\n\n\n<p>removeAttribute(String) \u2013 method is using to remove an attribute.&nbsp;<\/p>\n\n\n\n<p><em>getCookies() <\/em>\u2013 method returns an array of cookie objects received from the client.<\/p>\n\n\n\n<p><em>getHeader(String name) <\/em>\u2013 method is used to get specific header information from the request.<\/p>\n\n\n\n<p><em>getHeaderNames()<\/em> \u2013 method returns enumerator of all header names.&nbsp;<\/p>\n\n\n\n<p><em>getRequestURI()<\/em> \u2013&nbsp; method returns the URL of current JSP page.<\/p>\n\n\n\n<p><em>getMethod()<\/em> \u2013 method returns <a href=\"https:\/\/www.h2kinfosys.com\/blog\/servlet-request-and-response-objects\/\">HTTP request method<\/a>, such as GET for a Get request or POST for a Post Request.<em>getQueryString() <\/em>\u2013 method is used to get the query string associated with the JSP page URL.<\/p>\n\n\n\n<p>The <strong>response<\/strong> implicit object(javax.servlet.http.HttpServletResponse) is basically used to build, modify, and dealing with the response that will be sent to the client(browser) after processing the request.<\/p>\n\n\n\n<p><strong><em>It contains such methods:<\/em><\/strong><\/p>\n\n\n\n<p><em>void setContentType(String type) <\/em>\u2013 method tells client the type of response data by setting up the MIME type and character encoding. This information helps browser to interpret the response. For example:<\/p>\n\n\n\n<p>response.setContentType(&#8220;text\/html&#8221;);<\/p>\n\n\n\n<p>response.setContentType(&#8220;image\/gif&#8221;);<\/p>\n\n\n\n<p><em>void sendRedirect(String address)<\/em> \u2013 method redirects the control to the specified page. For example:&nbsp;<\/p>\n\n\n\n<p>response.sendRedirect(&#8220;http:\/\/google.com&#8221;);<\/p>\n\n\n\n<p><em>void addHeader(String name, String value)<\/em> \u2013 method adds a specified header name and it\u2019s value to the response. For example:<\/p>\n\n\n\n<p>response.addHeader(&#8220;myheader&#8221;, &#8220;hello&#8221;);<\/p>\n\n\n\n<p><em>void setHeader(String name, String value) <\/em>\u2013&nbsp; method overrides the current value of header with the new value. Example:<\/p>\n\n\n\n<p>response.setHeader(&#8220;myheader&#8221;, &#8220;new_hello&#8221;);<\/p>\n\n\n\n<p><em>boolean containsHeader(String name)<\/em> \u2013 method returns a boolean value true if the header is present in the response, otherwise &#8211; false. For example&nbsp;<\/p>\n\n\n\n<p>response.containsHeader(&#8220;myheader&#8221;);<\/p>\n\n\n\n<p><em>void addCookie(Cookie cookie) <\/em>\u2013 method adds a specified cookie to the response.&nbsp;<\/p>\n\n\n\n<p><em>void sendError(int status_code, String message<\/em>) \u2013 method is used to send an error code and an error message in the response. Here is the example of use:<\/p>\n\n\n\n<p>response.sendError(404, &#8220;Page not found&#8221;);<em>boolean isCommitted()<\/em> -method returns true if the Http Response has been sent to the client, else it gives false.<\/p>\n\n\n\n<p><strong><em>Exampe of use:<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;% if(response.isCommited())\n{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;%--do something --%>\n}else {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;%--do something else --%>\n} %>\n<\/pre>\n\n\n\n<p><em>void setStatus(int statuscode) <\/em>\u2013 method is used to set the HTTP status to a given response.For example:<\/p>\n\n\n\n<p>response.setStatus(200);<\/p>\n\n\n\n<p><strong><em>Here is an example of use response implicit object, that performs the login:<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;%@ page language=<em>\"java\"<\/em> contentType=<em>\"text\/html; charset=ISO-8859-1\"<\/em>\n\u00a0\u00a0\u00a0\u00a0pageEncoding=<em>\"ISO-8859-1\"<\/em>%>\n&lt;!DOCTYPE html>\n&lt;html>\u00a0\n&lt;head>&lt;title>Check Credentials&lt;\/title>\n&lt;\/head>\n&lt;body>\u00a0\n&lt;%\u00a0\nString username=request.getParameter(\"user\");\u00a0\nString password=request.getParameter(\"password\");\u00a0\n<strong>if<\/strong>(username.equals(\"user\") &amp;&amp; password.equals(\"qwerty\"))\n{\n\u00a0response.sendRedirect(\"success.jsp\");\n}\n<strong>else<\/strong>\n{\n\u00a0response.sendRedirect(\"failed.jsp\");\n}\n%>\u00a0\n&lt;\/body>\u00a0\n&lt;\/html><\/pre>\n\n\n\n<p>The <strong>session<\/strong> implicit object is used for storing data for an individual user to make it available on other JSP pages until the user session is active.<\/p>\n\n\n\n<p><strong><em>It has methods to work with attributes:<\/em><\/strong><\/p>\n\n\n\n<p><em>setAttribute(String, object)<\/em> &#8211; method is used to save an object in a session.<\/p>\n\n\n\n<p><em>getAttribute(String name)<\/em> \u2013 method fetches from a session an attribute that was set by the method setAttribute.<\/p>\n\n\n\n<p><em>removeAttribute(String name) <\/em>\u2013 method removes the attribute from the session.<\/p>\n\n\n\n<p><em>getAttributeNames<\/em> \u2013 method returns an enumeration of all the objects stored in a session.&nbsp;<\/p>\n\n\n\n<p>Also, the session object contains more methods for different purposes:<\/p>\n\n\n\n<p><em>getCreationTime()<\/em> \u2013 method returns the time of session activation.<\/p>\n\n\n\n<p><em>getId()<\/em> \u2013 returns a unique string identifier of a session assigned by the Servlet container during the creation.<\/p>\n\n\n\n<p><em>isNew() <\/em>\u2013 method returns true if cookies are disabled on the client-side.<\/p>\n\n\n\n<p>invalidate() \u2013 method kills a session.<\/p>\n\n\n\n<p><em>getMaxInactiveInterval() <\/em>\u2013\u00a0 method returns session maximum inactivate time interval in seconds.<em>getLastAccessedTime() <\/em>\u2013 method is used to know the last accessed time to a session.<\/p>\n\n\n\n<p>The <strong>application<\/strong> implicit object is used for maintaining useful data across the whole JSP application.<\/p>\n\n\n\n<p><strong><em>It contains methods to work with attributes:<\/em><\/strong><\/p>\n\n\n\n<p><em>Object getAttribute(String attributeName);<\/em><\/p>\n\n\n\n<p><em>void setAttribute(String attributeName, Object object);<\/em><\/p>\n\n\n\n<p><em>void removeAttribute(String objectName);<\/em><\/p>\n\n\n\n<p><em>Enumeration getAttributeNames();&nbsp;<\/em><\/p>\n\n\n\n<p><strong><em>The example of using:<\/em><\/strong><\/p>\n\n\n\n<p>application.setAttribute(\u201cmyAttribute\u201d, \u201csome_value\u201d);<\/p>\n\n\n\n<p>String string = (String)application.getAttribute(&#8220;myAttribute&#8221;);<\/p>\n\n\n\n<p>application.removeAttribute(\u201cmyAttribute\u201d);<\/p>\n\n\n\n<p>Some more methods:<em>String getInitParameter(String name)<\/em> &#8211; returns the value of the initialization parameter for a given parameter name. You can get the context parameters from web.xml:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u00a0\u00a0&lt;web-app>\n\u00a0\u00a0\u00a0\u00a0\u2026\n\u00a0\u00a0\u00a0\u00a0&lt;context-param>\n\u00a0\u00a0\u00a0\u00a0&lt;param-name>param&lt;\/param-name>\n\u00a0\u00a0\u00a0\u00a0&lt;param-value>hello&lt;\/param-value>\n\u00a0\u00a0\u00a0\u00a0&lt;\/context-param>\n\u00a0\u00a0\u00a0\u00a0&lt;\/web-app><\/pre>\n\n\n\n<p>This line of code will return String &#8220;hello&#8221;:<\/p>\n\n\n\n<p>String s=application.getInitParameter(\u201cparam\u201d);<\/p>\n\n\n\n<p><em>Enumeration getInitParameterNames()<\/em> &#8211; method returns all the initialization parameters.<\/p>\n\n\n\n<p>Enumeration e= application.getinitParameterNames();<\/p>\n\n\n\n<p><em>String getRealPath(String value)<\/em>&#8211; method converts a path to file system absolute path.<\/p>\n\n\n\n<p>String abspath = application.getRealPath(\u201c\/index.jsp\u201d);<\/p>\n\n\n\n<p><em>void log(String message)<\/em>&#8211; method writes the given message to the container default log file.<\/p>\n\n\n\n<p>application.log(\u201cHere is our message\u201d);<\/p>\n\n\n\n<p><em>String getServerInfo()<\/em> &#8211; method returns the name and version of web container.<\/p>\n\n\n\n<p>The <strong>exception<\/strong> implicit object (java.lang.Throwable) is used to displaying the error messages. This object is not Null just to the JSP pages with isErrorPage set to true. Here is an example of use:<\/p>\n\n\n\n<p>&lt;%@ page isErrorPage=&#8221;true&#8221; %&gt;&nbsp;<\/p>\n\n\n\n<p>An application got this Exception: &lt;%= exception %><\/p>\n\n\n\n<p>Please correct the issue in your code.<\/p>\n\n\n\n<p>The <strong>page<\/strong> implicit object (javax.servlet.jsp.PageContext) is a reference to the current Servlet instance, generated during the translation phase from a JSP page. We would not dip into detail as it is rarely used. It is not a useful object for building an application on JSP technology.<\/p>\n\n\n\n<p>The pageContext implicit object is using to access page, request, application, and session attributes. You can use this object to find attribute, get attribute, set attribute, and remove attribute at any of levels, such as JSP Page, HTTP Request, HTTP Session, Application Level. Here are the methods:<\/p>\n\n\n\n<p>Object findAttribute (String name);<\/p>\n\n\n\n<p>void removeAttribute(String name, int scope);<\/p>\n\n\n\n<p>void setAttribute(String name, Object value, int scope);<\/p>\n\n\n\n<p>Object getAttribute (String name, int scope).<\/p>\n\n\n\n<p><strong><em>Example of usage:<\/em><\/strong><\/p>\n\n\n\n<p>Object obj = pageContext.getAttribute(&#8220;test&#8221;, PageContext. PAGE_CONTEXT);<\/p>\n\n\n\n<p>Object obj = pageContext.getAttribute(&#8220;test&#8221;, PageContext. APPLICATION_CONTEXT);<\/p>\n\n\n\n<p>The <strong>config<\/strong> implicit object (javax.servlet.ServletConfig) holds the configuration of JSP, such as a servlet context, a servlet name, configuration parameters, etc. It has such methods:<\/p>\n\n\n\n<p><em>String getInitParameter(String param)<\/em> \u2013 method to get initialization parameters.<\/p>\n\n\n\n<p><em>Enumeration getInitParameterNames()<\/em> \u2013 method returns enumeration of initialization parameters.<\/p>\n\n\n\n<p><em>ServletContext getServletContext() <\/em>\u2013 method returns Servlet context reference.<\/p>\n\n\n\n<p>ServletContext contains methods helpful for communication between servlet and its servlet container: to get the MIME type of a file, dispatch requests, to write to a log file, etc.<\/p>\n\n\n\n<p><em>String getServletName() <\/em>\u2013 method returns the name of the servlet. We usually define the servlet name in the web.xml file inside tag &lt;servlet-name&gt;.<\/p>\n\n\n\n<p><strong><em>Let&#8217;s look at the example of using the config implicit object. Here is web.xml:<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;web-app>\n&lt;servlet>\u00a0\n&lt;servlet-name>TestServlet&lt;\/servlet-name>\u00a0\n&lt;jsp-file>\/test.jsp&lt;\/jsp-file>\u00a0\n&lt;\/servlet>\u00a0\n\n&lt;servlet-mapping>\u00a0\n&lt;servlet-name>TestServlet&lt;\/servlet-name>\u00a0\n&lt;url-pattern>\/test&lt;\/url-pattern>\u00a0\n&lt;\/servlet-mapping>\u00a0\n&lt;\/web-app><\/pre>\n\n\n\n<p><strong><em>Here is our test.jsp:<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;%@ page language=<em>\"java\"<\/em> contentType=<em>\"text\/html; charset=ISO-8859-1\"<\/em>\n\u00a0\u00a0\u00a0\u00a0pageEncoding=<em>\"ISO-8859-1\"<\/em>%>\n&lt;!DOCTYPE html>\n&lt;html>\n&lt;head> &lt;title>Testing the config Implicit Object&lt;\/title>\n&lt;\/head>\n&lt;body>\n&lt;%\u00a0\nString name = config.getServletName();\u00a0\nout.print(\"Servlet Name is: \"+ name);\u00a0\n%>\n&lt;\/body>\n&lt;\/html><\/pre>\n\n\n\n<p><strong><em>The output will be:\u00a0<\/em><\/strong><\/p>\n\n\n\n<p>TestServlet<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JSP implicit objects are created by JSP Engine during translation JSP to Servlet(translation phase). We can use them directly in JSP pages without initializing or declaring them because they are predefined.\u00a0 There are nine JSP implicit objects: page, out, request, response, session, application, config, pageContext and Exception. Let&#8217;s look closer at them. Object out is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3505,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[883,882,884,885],"class_list":["post-3498","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorials","tag-java-server-pages","tag-jsp-implicit-objects","tag-out-implicit-object","tag-request-implicit-object"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3498","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=3498"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3498\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/3505"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=3498"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=3498"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=3498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}