Skip to main content

JSP : Predifined variables | implicit objects

There are following implicit objects those become available to programmer to use in JSP page.

 

out

The out implicit object is an instance of a javax.servlet.jsp.JspWriter object and is used to send content in a response. The JspWriter object emulates some of the functionality found in the java.io.PrintWriter and java.io.BufferedWriter objects to provide a convenient method of writing text in a buffered fashion. The out implicit object can be configured on a per JSP basis by the page directive. Example :

<HTML>
<HEAD>
<TITLE>JSP Demo</TITLE>
</HEAD>
<BODY>
<% out.write("Server Date is: "); %>
<% out.write(new java.util.Date().toString()); %>
</BODY>
</HTML>

 

request

The request implicit object is an instance of a javax.servlet.http.HttpServletRequest object. The request implicit object represents a client's request and is a reference to the HttpServletRequest object passed into a HttpServlet's appropriate service method.

 <HTML>
<HEAD>
<TITLE>JSP Demo</TITLE>
</HEAD>
<BODY>
<%= request.getParameter("text")%>
</BODY>
</HTML>

Type the following url on address bar: http://localhost:8080/jsp1/requestobject.jsp?text=hello user Output is : hello user

 

response

The response implicit object is an instance of a javax.servlet.http.HttpServletRequest object. The response implicit object represents a response to a client's response and is a reference to the HttpServletResponse object passed into a HttpServlet's appropriate service method.

 <HTML>
<HEAD>
<TITLE>JSP Demo</TITLE>
</HEAD>
<BODY>
<%
response.setContentType("text/html");
java.io.PrintWriter pw=response.getWriter();
pw.println("Hello world");
%>
</BODY>
</HTML>

 

session

The session implicit object is an instance of a javax.servlet.http.HttpSession object. By default JSP creates a keep session context with all clients. The session implicit object is a convenience object for use in scripting elements and is the equivalent of calling the HttpServletRequest getSession() object.

<HTML>
<HEAD>
<TITLE>JSP Demo</TITLE>
</HEAD>
<BODY>
<%
int hits=0;
String h=""+session.getAttribute("hits");
if(h==null){
hits=0;
}else{
hits=Integer.parseInt(h);
}
hits++;
out.write("Total Hits : "+hits);
session.setAttribute("hits",""+hits);
%>
</BODY>
</HTML>

Output: Total Hits : 4

 

application

The application implicit object is an instance of a javax.servlet.ServletContext object. The application implicit object represents a Servlet's view of a Web Application and is equivalent to calling the ServletConfig getServletContext() method.

<HTML>
<HEAD>
<TITLE>JSP Demo</TITLE>
</HEAD>
<BODY>
<%
int hits=0;
String h=""+application.getAttribute("hits");
if(h==null||h.equals("null")){
hits=0;
}else{
hits=Integer.parseInt(h);
}
hits++;
out.write("Total Hits : "+hits);
application.setAttribute("hits",""+hits);
%>
</BODY>
</HTML>

Output: Total Hits : 4

 

config

The normal JSP deployment scheme automatically done by a container works, but nothing stops a JSP developer from declaring and mapping a JSP via the Web Application Deployment Descriptor, web.xml. A JSP can be manually deployed in the same fashion as a Servlet by creating a Servlet declaration in web.xml and replacing the servlet-class element with the jsp-page element. After being declared, the JSP can be mapped to a single or set of URLs same as a Servlet. Example to passing initial parameters to JSP page using config object.

In Web.xml 

  <web-app>
<servlet>
<servlet-name>configobject.jsp</servlet-name>
<jsp-file>/configobject.jsp</jsp-file>
<init-param>
<param-name>author</param-name>
<param-value>Hemraj</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>configobject.jsp</servlet-name>
<url-pattern>/configobject.jsp</url-pattern>
</servlet-mapping>
</web-app> 

 In configobject.jsp 

 <HTML>
<HEAD>
<TITLE>JSP Demo</TITLE>
</HEAD>
<BODY>
<h2>Author of Page:</h2>
<h3><%=""+config.getInitParameter("author")%></h3>
</BODY>
</HTML>

 

page

The page implicit object represents the current class implementation of the page being evaluated. If the scripting language of the page is java, which by default it is, the page object is equivalent to the keyword of a Java class. This object is used to refer the instance of current JSP page created after translation in servlet. We can use it to differentiate local and instance variable as shown in following example.

<HTML>
<HEAD>
<TITLE>JSP Demo</TITLE>
</HEAD>
<BODY>
<%out.write(page.toString());%>
</BODY>
</HTML>

 

exception

The exception object is available only within error pages. It is a reference to the java.lang.Throwable object that caused the server to call the error page. You would use it just as you would use any other Exception or Error object in the catch block of a try-catch section of code. The exception object has page scope.
Example :

 <%@page contentType="text/html" pageEncoding="UTF-8"%> <html>
<head>
<title>JSP Page</title>
</head>
<body>
<h2>Error/Exception :</h2>
<%=exeception.getMessage()%>
</body>
</html>

 

pageContext

The pageContext implicit scripting variable is an instance of a javax.servlet.jsp.PageContext object. A PageContext object represents the context of a single JavaServer Page including all the other implicit objects, methods for forwarding to and including Web Application resources, and a scope for binding objects to the page. The PageContext object is not always helpful when used by itself because the other implicit objects are already available for use. A PageContext object is primarily used as a single object that can easily be passed to other objects such as custom actions. This is useful since the page context holds references to the other implicit objects.

 <HTML>
<HEAD>
<TITLE>JSP Demo</TITLE>
</HEAD>
<BODY>
<%
int hits=0;
String h=""+ pageContext.getAttribute("hits");
if(h==null||h.equals("null")){
hits=0;
}else{
hits=Integer.parseInt(h);
}
hits++;
out.write("Total Hits : "+hits);
pageContext.setAttribute("hits",""+hits);
%>
</BODY>
</HTML>

Output: Total Hits : 4

Comments

Popular posts from this blog

Using HyperSQL (HSQLDB)

HSQLDB is a portable RDBMS implemented in pure java. It can be embedded with your application as well as can be used separately. It is very a small database that supports almost all features of the standard database system. It comes with small jar file that can be found in lib folder. The HSQLDB jar package is located in the /lib directory of the ZIP package and contains several components and programs. Core components of jar file are : HyperSQL RDBMS Engine (HSQLDB), HyperSQL JDBC Driver, Database Manager, and Sql Tool. Installing and Using Download: download latest release of HyperSQL database from http://hsqldb.org website and extract it. You will see following contents. Here "bin" directory contains some batch files those can be used to run a swing based GUI tool. You can use runManagerSwing.bat to connect to database, but database must be on before running it. Directory lib contains File hsqldb.jar . It is the database to be used by you. Running database First

How to handle values from dynamically generated elements in web page using struts2

Some time you will see the form containing the button " Add More " . This facility is provided for the user to get the values for unknown number of repeating for some information. for example when you are asking to get the projects details from user, you need to put the option to add the more project for the user since you don't known how many projects user have. In the HTML form, you repeat the particular section to get the multiple values for those elements. In Html page , you can put the option to add new row of elements or text fields by writing the java script or using JQuery API. Now, the question is that how to capture the values of dynamically generated text fields on the server. Using the servlet programming you can get the values by using getParameters() method that resultants the array of the parameter having the same name. But this limit you to naming the text fields in the HTML form. To ally this approach, you have to take the same name for t

In Process Mode of HSQLDB in web application.

If you want to use the database into your web application, you can use the HSQLDB in In_Process mode. In this mode, you can embed the HSQLDB into your web application and it runs as a part of your web application programm in the same JVM. In this mode, the database does not open any port to connect to the application on the hosing machine and you don't need to configure anything to access it. Database is not expposed to other application and can not be accessed from any dabase tools like dbVisualizer etc. In this mode ,database will be unknown from any other person except you. But in the 1.8.0 version, you can use Server intance for external as well as in process access.  To close the databse, you can issue SHUTDOWN command as an SQL query.   In the in-process mode, database starts from JDBC with the associated databse file provided through  connection URL. for example   DriverManager.getConnection("jdbc:hsqldb:mydatabase","SA","");   Here myd