Wednesday 23 March 2016

JSP REQUEST IMPLICIT OBJECT

The JSP request is an implicit object of type HttpServletRequest i.e. created for each jsp request by the web container. It can be used to get request information such as parameter, header information, remote address, server name, server port, content type, character encoding etc.
It can also be used to set, get and remove attributes from the jsp request scope.
Let's see the simple example of request implicit object where we are printing the name of the user with welcome message.

Example of JSP request implicit object

index.html


  1. <form action="welcome.jsp">  
  2. <input type="text" name="uname">  
  3. <input type="submit" value="go"><br/>  
  4. </form>  

welcome.jsp


  1. <%   
  2. String name=request.getParameter("uname");  
  3. out.print("welcome "+name);  
  4. %>  

Output

jsp request implicit object output 2 jsp request implicit object output 3

JSP response implicit object

In JSP, response is an implicit object of type HttpServletResponse. The instance of HttpServletResponse is created by the web container for each jsp request.
It can be used to add or manipulate response such as redirect response to another resource, send error etc.
Let's see the example of response implicit object where we are redirecting the response to the Google.

Example of response implicit object

index.html

  1. <form action="welcome.jsp">  
  2. <input type="text" name="uname">  
  3. <input type="submit" value="go"><br/>  
  4. </form>  
welcome.jsp

  1. <%   
  2. response.sendRedirect("http://www.google.com");  
  3. %>  

Output

jsp response implicit object output 1

JSP config implicit object

In JSP, config is an implicit object of type ServletConfig. This object can be used to get initialization parameter for a particular JSP page. The config object is created by the web container for each jsp page.
Generally, it is used to get initialization parameter from the web.xml file.

Example of config implicit object:

index.html

  1. <form action="welcome">  
  2. <input type="text" name="uname">  
  3. <input type="submit" value="go"><br/>  
  4. </form>  
web.xml file

  1. <web-app>  
  2.   
  3. <servlet>  
  4. <servlet-name>sonoojaiswal</servlet-name>  
  5. <jsp-file>/welcome.jsp</jsp-file>  
  6.   
  7. <init-param>  
  8. <param-name>dname</param-name>  
  9. <param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>  
  10. </init-param>  
  11.   
  12. </servlet>  
  13.   
  14. <servlet-mapping>  
  15. <servlet-name>sonoojaiswal</servlet-name>  
  16. <url-pattern>/welcome</url-pattern>  
  17. </servlet-mapping>  
  18.   
  19. </web-app>  
welcome.jsp

  1. <%   
  2. out.print("Welcome "+request.getParameter("uname"));  
  3.   
  4. String driver=config.getInitParameter("dname");  
  5. out.print("driver name is="+driver);  
  6. %>  

Output

jsp config implicit object output 1 jsp config implicit object output 2

JSP application implicit object

In JSP, application is an implicit object of type ServletContext.
The instance of ServletContext is created only once by the web container when application or project is deployed on the server.
This object can be used to get initialization parameter from configuaration file (web.xml). It can also be used to get, set or remove attribute from the application scope.
This initialization parameter can be used by all jsp pages.

Example of application implicit object:

index.html

  1. <form action="welcome">  
  2. <input type="text" name="uname">  
  3. <input type="submit" value="go"><br/>  
  4. </form>  
web.xml file

  1. <web-app>  
  2.   
  3. <servlet>  
  4. <servlet-name>sonoojaiswal</servlet-name>  
  5. <jsp-file>/welcome.jsp</jsp-file>  
  6. </servlet>  
  7.   
  8. <servlet-mapping>  
  9. <servlet-name>sonoojaiswal</servlet-name>  
  10. <url-pattern>/welcome</url-pattern>  
  11. </servlet-mapping>  
  12.   
  13. <context-param>  
  14. <param-name>dname</param-name>  
  15. <param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>  
  16. </context-param>  
  17.   
  18. </web-app>  
welcome.jsp

  1. <%   
  2.   
  3. out.print("Welcome "+request.getParameter("uname"));  
  4.   
  5. String driver=application.getInitParameter("dname");  
  6. out.print("driver name is="+driver);  
  7.   
  8. %>  

Output

jsp application implicit object output 1 jsp application implicit object output 2

No comments:

Post a Comment