implicit objects in jsp

Definition of implicit objects in jsp –: These implicit objects are automatically created by the container during JSP page translation. implicit objects can be used directly on the JSP page. These are created by the container automatically so it can be accessed using objects.

Types of implicit object in jsp-We know that these are automatically created in jsp page. These are 9 types

  1. Request implicit object
  2. Response implicit object
  3. out implicit object
  4. session implicit object
  5. config implicit object
  6. application implicit object
  7. page implicit object
  8. exception implicit object
  9. page context implicit object

Request implicit object in jsp-:This is an instance of javax.servlet.http.HttpServletRequest object. The request object is created by the JSP Engine every time a request is made on a jsp page.request object provides methods and cookies to receive some headers information. There are also some methods to set, get and remove these attributes; request object.request this implicit object; Retrieves values ​​passed via HTTP request.For example, when some content by HTML Form; It is received by sending an HTTP request object to the server.

Example for Retrieve Header and its Information in JSP

GetHeaderNames () of request implicit object on example is used to return HTTP header information from this methods. This method returns the enumeration return, which holds the header information related to the HTTP request.Below are some headers and their information retrieved by request object.

 <%@ page import="java.util.*" %> 
<table border="1" >
<% Enumeration names = request.getHeaderNames();
   while(names.hasMoreElements()){
   String name = (String)names.nextElement();
  out.print("<tr><td>"+name+ "</td>" + "<td>"+request.getHeader(name) + "</td></tr>");
}
%>
</table> 

Output of this

Methods for request implicit Object

There are some methods are using in request implicit object. for requesting use the some parameters these are the

getParameter(string name)-:This is used to get the value of the requested parameter. The value of string type is passed to the method which is the name of the attribute in HTML Form. For example-

<%@page import="java.util.*" %>
 <%  String name=request.getParameter("myname");
  out.print("Name : " + name);
 %>

The getParameterNames ()-: This method returns all requested parameter names in enumeration for example

<form action="netnic.jsp">
   Enter name:<input type="text" name="myname/>
<input type="submit" value="submit"/>
</form>
   

For example of this

<%@page import="java.util.*" %>
 <%  Enumeration e=request.getParameterNames();
     while(e.hasMoreElements()){     
    Object obj=e.nextElement(); 
        String param=(String)obj; 
        String val=request.getParameter(param);
         out.println("Parameter Name : "+ param +" Parameter Value : "+ val + "");     } %>

getParameterValues ​​() -:This method returns an array of requested parameter values.

<input type="checkbox" name="course" value="HTML"/>HTML
 <input type="checkbox" name="course" value=" CSS"/>CSS
 <input type="checkbox" name="course" value=" javascript"/>javascript
 <input type="checkbox" name="course" value=" jQuwry"/>jQuery
 <input type="submit" value="submit"/> 
</form>
<%
if(request.getParameter("course") != null){
     String[] val = request.getParameterValues("course");
         for(int i = 0; i < val.length; i++){             out.print("Value "+ (i+1) + ": " + val[i] + "
"); 
         }
 }
 %>

setAttribute ()– This is used to set the attribute on request. The attribute name and its value are assigned to the attribute. For example

<%@page import="java.util.*" %>
 <%  
      request.setAttribute("attr","Welcome netnic jsp language");
 %>

GetAttribute ()– This is used to get the attribute set on the request. The name of the attribute set on this method is passed. If the given attribute name is false then ‘null’ returns.

 <%@page import="java.util.*" %>
 <%  request.setAttribute("attr","welcome to netnic jsp page");
 out.println(request.getAttribute("attr"));
 %>

GetAttributeNames ()-: This method returns the names of all the attributes requested in Enumeration.

<%@page import="java.util.*" %>
 <% request.setAttribute("attr1", "welcome netnic jsp");
 request.setAttribute("attr2", "hello jsp");
 Enumeration e=request.getAttributeNames();
     while(e.hasMoreElements()){  
       Object obj=e.nextElement();  
       String name=(String)obj;
         out.println("Attribute Name : " + name + ""); 
    }
 %>

RemoveAttribute ()– This method This is used to remove the set attribute with the given attribute name.

<%@page import="java.util.*" %> 
<% request.setAttribute("attr1", "welcome netnic");
 request.setAttribute("attr2", "welcome jsp");
 request.removeAttribute("attr2");
 Enumeration e=request.getAttributeNames();     
while(e.hasMoreElements()){         
Object obj=e.nextElement();         
String name=(String)obj;         
out.println("Attribute Name : " + name + ""); } 
%>

GetCookies() method-: This returns the cookie objects from the client to the array.

<%
  Cookie[] cookies = request.getCookies();
           if(cookies != null) { 
    for (int i = 0; i < cookies.length; i++){
        Cookie cookie = cookies[i];    
         out.print("Cookie Name : " + cookie.getName() + " ");
             out.print("Cookie Value: " + cookie.getValue() + "");     } }
  %>

GetHeader () -This method This returns the given request header in string.


getHeaderNames ()-: This method returns all header names in enumeration.

GetRequestURI()-: This method returns the URL of the current JSP Page.

GetMethod () – This method returns the request method. It returns GET for get request and POST for post request.

GetQueryString ()- This method returns the Query String from the JSP Page URL. The part of the URL after the question mark (?) In the URL is called QueryString.

GetServerPort ()-: This method request is received where its port number is returned.

GetProtocol ()-: This method returns the name and version of the protocol the request is using.

getRemoteAddr() – getProtocol () method returns the IP address of the requested client.

isSecure () -This method returns the requested boolean value whether it is secure or not. If the URL is https on the channel, it is not secure and it is not secure or if it is http, it is not secure.

Leave a Comment