Java @ Desk: JSP | Java at your desk

How to include a JSP inside another JSP

How to include a JSP inside another JSP

Generally, the two most common ways of including a JSP inside another JSP are:

1. Using the include directive (Static include)
<%@include file="sub.jsp"%>


2. Using jsp:include (Dynamic include)
<jsp:include page="sub.jsp"/>


It is a widely held misconception that both techniques produce the same results and can be used interchangeably. Even though both work well for static contents, for dynamic contents they are very different.
To understand the difference, let us first see how a JSP works.
All JSPs are translated and compiled to a servlet before they are used. Generally, this happens when the first request comes to the JSP (this can be changed to compile all JSPs at the start up). After this, for all subsequent requests to this JSP, this generated servlet does the functioning of the JSP.
Please note that in the subsequent text, for explanation; we have referred to two JSP files as examples – main.jsp (the main JSP file) and sub.jsp (the included JSP file).

Static Include
The <%@include file="" %> tag will inject the contents of the sub file (sub.jsp) into the JSP containing this tag (main.jsp), as if it were copied and pasted. During the JSP compilation of main.jsp, this file and its included sub.jsp are parsed as a single file, with the same context and constraints, and with the included sub.jsp contents copied and pasted in the place containing this tag. Thus, as you can see, the translated servlet will include all the sub.jsp contents. Once the translation and compilation is complete, the generated servlet will have the entire compiled code of both main.jsp and sub.jsp in it. Therefore, if we change sub.jsp post generating this servlet, the changes will not be reflected in main.jsp till its next compilation.

Dynamic Include
The tag adds the content from the value of the page attribute, to the current page at request time (i.e., every time you make a request to the main JSP file, the included JSP file is compiled). The dynamic include is particularly intended for the dynamic content coming from JSPs. In this case, sub.jsp is not compiled when main.jsp is compiled. When you look into the generated servlet, you can see the following code:

org.apache.jasper.runtime.JspRuntimeLibrary.include(request, response, "sub.jsp", out, false);


This include tag is a run-time instruction to the JSP engine – rather than copying the entire code into the current page, a method call is made to sub.jsp from main.jsp. Therefore, even if you change the sub.jsp after main.jsp is converted to a servlet, it will reflect the changes, as the sub.jsp is rendered every time a call to main.jsp is made (even without recompilation of the main.jsp). However, in this case, the pagecontext and other variables will be different for sub.jsp. If you want to pass some elements, you can use




Sample code illustrating the difference between static and dynamic include

Sub.jsp

<!DOCTYPE html>
SUB CONTENT
Argument from pageContext = <%= pageContext.getAttribute("PARAM") %>
Main.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<title>Main JSP</title>
</head>
<body>
<% pageContext.setAttribute("PARAM", "Argument");%>
 <P>MAIN CONTENT</P>
 <P>USING @ include</p>
 <p>
  <%@include file="sub.jsp"%>
 </P>

 <P>USING jsp:include</p>
 <p>
  <jsp:include page="sub.jsp" />
 </p>
</body>
</html>


Output
MAIN CONTENT
USING @ include
SUB CONTENT Argument from pageContext = Argument 
USING jsp:include 
SUB CONTENT Argument from pageContext = null  


In the above example, when we used the @include tag; we got the same value for pagecontext in sub.jsp as that in main.jsp. However, when we used jsp:include tag, we lost the content in the pagecontext variable. This happened because jsp:include translates the sub.jsp with a separate pagecontext during run time.




What to use when

Static include
In most of the cases, you can use the @include tag and it should work fine. It is the fastest mechanism. If your container doesn’t automatically detect changes in the included file, you can force the changes to take effect, by deleting the class file of the main page; so that the next time a request comes, the container will automatically translate both the main file and sub file.

Dynamic include
Use jsp:include tag in the following two cases:
i) If the contents of sub.jsp changes often,
ii) If the page where the sub.jsp needs to be included cannot be decided until the main page is requested.



These are some rare scenarios where you do not remove the main.jsp translated files and only include the sub.jsp to the container.

However, a real scenario where you will need to use the jsp:include tag is for the following error:
_jspService is exceeding the 65535 bytes limit: This is a common issue when dealing with very large JSPs. It happens because most of the JSP code goes into one single method - _jspService - in the translated and compiled servlet; and in java, the maximum size a method can have is 64kb. In such cases, we should invariably use the dynamic include, as it will only have a single line for the sub.jsp (which is a call to JspRuntimeLibrary.include), and the sub.jsp is resolved during run time. This is unlike the static include, which will have all the translated content of sub.jsp in main.jsp during compilation itself.

In addition to these two methods, we also have the following techniques to include one JSP inside another JSP. However, these are not very common.
1. Using the JSTL tag
Works almost like the jsp:include tag, but it’s more powerful and flexible. It can even import the url from outside the web container.
2. Preludes and codas - These can be applied only at the beginnings and ends of pages.
3. Create custom tags
4. Use some framework like "Tiles"

This post is written by Jerin Joseph. He is a freelance writer, loves to explore latest features in Java technology.

Ajax json jquery Post example

Ajax json jquery Post example

Ajax is used to update the parts of the web page with out reloading the entire page.

JSON is used to store and exchange the data between client and server.

Enums is used to store the data means a list of named constants.

In this example, Data will be loaded into drop down list based on the selected option(country) using Jquery and the above mentioned technologies.

Unlike JSP's , we can make use of HTML itself by using JSON concept for data exchange between client and server.

In this example, Dynamic data loading is shown by using drop down list, Code has flexibility to extend to other elements of a web page. Whenever Country option has selected OnChange event will be triggered and it in turn calls the specified function, In this example it was callRegCountry(), which has Ajax call (Asynchronous JavaScript and XML.) nothing but a replacement of Form submit in a better way.

Ajax and its terms:

type : Type of request eg: get, post
url : The URL to which the request has to sent,Typically Servlet or JSP.
data : An object or string that is sent to the server with the request.
Success : A callback function that is executed if the request succeeds.
Error : function called When request completes with an error

HTML Code:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>AJAX-JSON example</title>
<meta name="description"
 content="Dynamic Loading of data by Ajax via JSON and enums">
<meta name="author" content="sekhar">

<!-- jquery.js is loaded through Google CDN-->

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<!-- JSON2.js needed if IE versions less than IE8 -->

<script src="json2.js"></script>

<script>

// Will be triggered when country name was changed

function callRegCountry(){

 var countryName=$('#countryRows').val();//Will Get Selected Country

  $('#cityRows option:gt(0)').remove();//For removing old entries in city drop down.Since Ajax won't reload page.

 $.ajax({
  type : "POST",//request type
  url : "/AjaxJson/ControllerServlet",//Servlet name ..can be configured through web.xml
  data : {
   "flag":"retCities",//flag--to distinguish from multiple requests
        "country":countryName
  },

  dataType : "text",
  success : function(data) {
   try{
    var citiArray=JSON.parse(data);

    if(citiArray != null){

    for(var s=0;s<citiArray.length;s++){

    var serial=citiArray[s];
    //populating drop down list
       $("#cityRows").append($("<option></option>").val(serial.city).html(serial.city));
    }
    }
    }catch(err){
     alert(err);
    }

   },
   error : function(){
    alert("some error occured while fetching city details corresponding to county "+countryName);
   }
  
  });
}

</script>
</head>
<body>
 <div id="loader">
 <div id="Country_loader">
  <table>
   <tr>
    <td width="250px"><label>Choose the Country</label></td>
    <td><select id="countryRows" onchange="callRegCountry()">
     <option value="india">India</option>
     <option value="america">America</option>
           </select>
    </td>
   </tr>
  </table>
 </div>
 <div id="Citi_loader">
  <table>
   <tr>
    <td width="250px"><label>Corresponding Cities</label></td>
    <td><select id="cityRows">
    <option value="-1">-Choose-</option>
     </select></td>
   
   </tr>
  </table>
 </div>
 </div>
</body>
</html>


Ajax Request will call servlet post method, Flag was used to separate from multiple requests. Based on that it will call corresponding method in java.

Servlet Code



protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException {
 response.setContentType("text/html");
 String flag=request.getParameter("flag");
 BusinessHandler handler=new BusinessHandler();
 if(flag.equalsIgnoreCase("retCities")){
  try {
   handler.retCities(request,response);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}


In this code fragment,Used two enumerations to hold the data,instead of getting data from Database. Here based on selected country ,iterating the corresponding enumeration and creating JSON object and adding that to JSON array and sending the JSON array as a response.

Java Code:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

enum India{
 Mumbai,Hyderabad,Chennai,Delhi,Bangalore;
}
enum America{
 Mexico,NewYork,Toronto,Chicago;
}

public class BusinessHandler {
 //Based on the selected country in front end corresponding enumeration will be iterated.enumeration is nothing but a list of constants. 

public void retCities(HttpServletRequest request, HttpServletResponse response) throws Exception {
 String country=request.getParameter("country");//holds selected country
JSONArray citArray=new JSONArray();
if(country.equalsIgnoreCase("india")){
      for(India cnt:India.values()){//iterating India enum
   JSONObject citObject=new JSONObject();
   citObject.put("city", cnt);//populating JSONobject
    citArray.add(citObject);
  }
} else {
for(America cnt:America.values()){
JSONObject citObject=new JSONObject();
   citObject.put("city", cnt);
   citArray.add(citObject);
}
}
 response.getWriter().write(citArray.toString());
}
}


jar files used for smooth flow:

ezmorph.jar
json-lib-2.2.2-jdk15.jar
commons-beanutils-1.7.jar
commons-collections.jar
commons-lang.jar
commons-logging-1.1.1.jar

Server used:

Apache tomcat v7.0

JSP implicit object PageContext with example

JSP implicit object PageContext with example

In our last posts, we have learned
JSP implicit objects REQUEST with example,
JSP implicit objects OUT with example,
JSP implicit object RESPONSE with example,
JSP Implicit Object CONFIG with example ,
JSP Implicit Object APPLICATION with example ,
JSP Implicit object session with example

The pageContext object is an instance of a javax.servlet.jsp.PageContext object.

The pageContext objects has special access to manage attributes from following scopes :
1) Application Level – Scope: APPLICATION_SCOPE
2) HTTP Request – Scope: REQUEST_SCOPE
3) HTTP Session – Scope: SESSION_SCOPE
4) JSP Page – Scope: PAGE_SCOPE

Methods of pageContext implicit object :
1) findAttribute(java.lang.String name)
2) getAttribute (java.lang.String name)
3) getAttribute (java.lang.String name, int Scope)
4) removeAttribute(String AttributeName, int Scope)
5) setAttribute(String AttributeName, Object AttributeValue, int Scope)
6) setAttribute(String AttributeName, Object AttributeValue)

As mentioned above, there are 2 flavors of setAttribute method. If Scope is not passed as an attribute, then the attribute is set to current JSP page i.e. by default the scope is PAGE_SCOPE.

There is a slight difference between the methods findAttribute(java.lang.String name), getAttribute (java.lang.String name), getAttribute (java.lang.String name, int Scope). Click here to know the difference

Example:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
 pageContext.setAttribute("Interest Rate", 10,
   PageContext.SESSION_SCOPE);
 pageContext.setAttribute("Loan Amount", 10000);

 // Searches for the named attribute in all the 4 scopes i.e. <b>page, request, session (if valid), 
 // and application scope(s)</b> 
 pageContext.findAttribute("Loan Amount");

 // Return the object associated with the name in the <b>page scope</b> only
 pageContext.getAttribute("Loan Amount");

 // Return the object associated with the name in the specified scope
 pageContext.getAttribute("Loan Amount", PageContext.REQUEST_SCOPE);
%>
</body>
</html>

PageContext findAttribute vs getAttribute methods

PageContext findAttribute vs getAttribute methods

In PageContext class, there are three methods:
1) findAttribute(java.lang.String name)
2) getAttribute (java.lang.String name)
3) getAttribute (java.lang.String name, int Scope)

There is a slight difference among the three as mentioned below:
findAttribute searches for the named attribute in all the 4 scopes i.e. page, request, session (if valid), and application scope(s) in order and returns the value associated or null.

getAttribute return the object associated with the name in the page scope only or null if not found.

getAttribute(java.lang.String name, int scope) return the object associated with the name in the specified scope or null if not found.

JSP Implicit object session with example

JSP Implicit object session with example

In our last posts, we have learned
JSP implicit objects REQUEST with example,
JSP implicit objects OUT with example,
JSP Implicit Object CONFIG with example ,
JSP Implicit Object APPLICATION with example ,
JSP implicit object PageContext with example,
JSP implicit object RESPONSE with example

JSP session object is a type of javax.servlet.http.HttpSession class. Session object represents the data associated with the user for the complete session. If any attribute is set using the session object, it will be available to the user for that particular session unless the browser is not closed or session is not invalidated.

Whenever a user request for a particular JSP for the first time, the container automatically creates a session and holds the session attributes till it is active. This session object will be shared across pages visited by the user.

Session creation is an heavy operation. By default the session creation for the JSP page is true. Can we disable session creation for a JSP page? Click here to know

Following are the methods of a session :
1) setAttribute(String, object)
2) getAttribute(String name)
3) isNew() - The isNew() method of session object returns a true value if the session is new. If the session is not new, then a false value is returned.
4) invalidate() - It kills the session associated with the user.

Example :

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 Session Timeout :
 <%=session.getMaxInactiveInterval()%>

 <%
  session.setAttribute("loanInterestRate", 10);
 %>
</body>
</html>

JSP disable or prevent session creation

JSP disable or prevent session creation

Yes. We can disable session creation for a JSP.

Session creation is a heavy process. When a user visits an application for the first time, the container automatically associates a session to that user. But in some cases, session object is not at all required. Following are the examples :

1) Login Page
2) Help Page
3) Logout Page
4) Pages with frames. In this case, all the frames will create their own session which would be very heavy operation.

and much more. In such cases, a JSP page should declare a page directive "<%@ page session="false" %>". In this case, a user session will not be created.

Example :

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" session="false"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
</body>
</html>

JSP implicit object application with example

JSP implicit object application with example

In our last posts, we have learned
JSP implicit objects REQUEST with example,
JSP implicit objects OUT with example,
JSP Implicit Object CONFIG with example ,
JSP Implicit object session with example ,
JSP implicit object PageContext with example,
JSP implicit object RESPONSE with example,

JSP implicit object application is an object of type javax.servlet.http.ServletContext.

The application object belongs to all the JSP pages within the web application.

Difference between config and application object :
1) config object (as explained in earlier post) belongs to a particular JSP page within the web application whereas application object belongs to the all the JSP pages within the web application.

application object is used when some information needs to be shared to all the JSP pages within the application.

Basic example of the same are :
1) Website Visit Counters
2) Admin Name and Id

If any one JSP updates some application values, it will be visible to all the JSP pages.

Methods of implicit object application
1) getAttribute(String name)
2) getAttributeNames
3) setAttribute(String objName, Object object)
4) removeAttribute(String objName)

Declaring variable in deployment descriptor(web.xml) :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 id="WebApp_ID" version="2.5">
 <display-name>JSPProject</display-name>
 
 <context-param>
  <param-name>adminName</param-name>
  <param-value>John</param-value>
 </context-param>
</web-app>


JSP page using application object:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
 String adminName = (String) application.getAttribute("adminName");
 out.println("Welcome " + adminName);
%>
</body>
</html>

Implicit Objects in JSP

Implicit Objects in JSP

In JSP, there are 9 implicit objects as mentioned below :

1) request
2) out
3) response
4) config
5) application
6) session
7) pageContext
8) page
9) exception

JSP Implicit Object config with example

JSP Implicit Object config with example

In our last posts, we have learned
JSP implicit objects REQUEST with example,
JSP implicit objects OUT with example ,
JSP Implicit Object APPLICATION with example ,
JSP Implicit object session with example ,
JSP implicit object PageContext with example,
JSP implicit object RESPONSE with example

In JSP, config is an implicit object of class javax.servlet.ServletConfig. Like we can configure parameters for a Servlet in web.xml or deployment descriptor, in the same way we can configure parameters for particular JSP.

As configuration parameters for a Servlet belongs to that Servlet only, configuration parameters for a JSP page belongs to that JSP page only.

web.xml

 <servlet>
  <servlet-name>homeJSP</servlet-name>
  <jsp-file>/homeJSP.jsp</jsp-file>
  <init-param>
   <param-name>firstName</param-name>
   <param-value>John</param-value>
  </init-param>
 </servlet>

 <servlet-mapping>
  <servlet-name>homeJSP</servlet-name>
  <url-pattern>/homeJSP</url-pattern>
 </servlet-mapping>


homeJSP.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%   
String firstName = config.getInitParameter("firstName");  
out.print("First Name is - " + firstName);  
%>  
</body>
</html>


When you hit homeJSP.jsp, the config parameter firstName value will be displayed on the browser.

JSP implicit object response with example

JSP implicit object response with example

In our last posts, we have learned
JSP implicit objects REQUEST with example,
JSP implicit objects OUT with example,
JSP Implicit Object CONFIG with example ,
JSP Implicit Object APPLICATION with example ,
JSP Implicit object session with example ,
JSP implicit object PageContext with example

Today we will learn, JSP implicit object response.

The response object is an instance of class implementing an javax.servlet.http.HttpServletResponse interface. The response object handles the output that need to be send to the client browser.

Basic example of response is a login page. If the user is validated successfully, the response is the Home page else the response is the Login Page with error message. In case of error, the method sendError is called by passing the HTTP Error Code. For List of Error Codes, click here.

Methods of response object:
1) setContentType : This method sets the content type for the page. Examples are
HTML - "text/html",
XML - "text/xml"
CSV - "text/csv"

2) sendRedirect(String URL) : This method takes the URL parameter in a String format and navigates the client to the URL passed.

3) sendError(int status_code) : This method returns the error back to the client with the Error Code passed. If the response is already committed, then it throws an IllegalStateException exception.

4) getWriter() : This method provides an instance of PrintWriter class. This is use to send the text response back to the client.

5) addCookie(Cookie cookie) : This method is used to add the cookie to the response . For multiple cookies, call this method more than once passing different cookies object

6) addHeader(String name, String value) : This method is used to write the header to the response. If the header is already present, then value is added to the existing header values.

Sample Example :

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Implicit Object 'response' Example</title>
</head>
<body>

<%
response.sendRedirect("http://javacodeimpl.blogspot.com");
%>
</body>
</html>

Marquee in HTML

Marquee allows a scrolling text in an HTML file.

Attributes :
1) behavior - This attribute defines the way the text will scroll withing the marquee. Values can ve scroll, slide or alternate.

2) bgcolor - To set the background color for marquee.

3) direction - Sets the direction for the text to scroll, slide. Values can be left, right, up down. If no direction is specified than default is left

4) height - To set the height of marquess

5) scrollamount - Sets the amount of scrolling at each interval in pixels. The default value is 6.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <style type='text/css'>

.CSSTableGenerator {
 margin:0px;padding:0px;
 width:50%;
 box-shadow: 10px 10px 5px #888888;
 border:1px solid #000000;
 
 -moz-border-radius-bottomleft:0px;
 -webkit-border-bottom-left-radius:0px;
 border-bottom-left-radius:0px;
 
 -moz-border-radius-bottomright:0px;
 -webkit-border-bottom-right-radius:0px;
 border-bottom-right-radius:0px;
 
 -moz-border-radius-topright:0px;
 -webkit-border-top-right-radius:0px;
 border-top-right-radius:0px;
 
 -moz-border-radius-topleft:0px;
 -webkit-border-top-left-radius:0px;
 border-top-left-radius:0px;
}.CSSTableGenerator table{
    border-collapse: collapse;
        border-spacing: 0;
 width:100%;
 height:100%;

 margin:0px;padding:0px;
}.CSSTableGenerator tr:last-child td:last-child {
 -moz-border-radius-bottomright:0px;
 -webkit-border-bottom-right-radius:0px;
 border-bottom-right-radius:0px;
}
.CSSTableGenerator table tr:first-child td:first-child {
 -moz-border-radius-topleft:0px;
 -webkit-border-top-left-radius:0px;
 border-top-left-radius:0px;
}
.CSSTableGenerator table tr:first-child td:last-child {
 -moz-border-radius-topright:0px;
 -webkit-border-top-right-radius:0px;
 border-top-right-radius:0px;
}.CSSTableGenerator tr:last-child td:first-child{
 -moz-border-radius-bottomleft:0px;
 -webkit-border-bottom-left-radius:0px;
 border-bottom-left-radius:0px;
}.CSSTableGenerator tr:hover td{
 
}
.CSSTableGenerator tr:nth-child(odd){ background-color:#e5e5e5; }
.CSSTableGenerator tr:nth-child(even)    { background-color:#ffffff; }.CSSTableGenerator td{
 vertical-align:middle;
 
 
 border:1px solid #000000;
 border-width:0px 1px 1px 0px;
 text-align:left;
 padding:7px;
 font-size:14px;
 font-family:Times New Roman;
 font-weight:normal;
 color:#000000;
}.CSSTableGenerator tr:last-child td{
 border-width:0px 1px 0px 0px;
}.CSSTableGenerator tr td:last-child{
 border-width:0px 0px 1px 0px;
}.CSSTableGenerator tr:last-child td:last-child{
 border-width:0px 0px 0px 0px;
}
.CSSTableGenerator tr:first-child td{
  background:-o-linear-gradient(bottom, #cccccc 5%, #b2b2b2 100%); background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #cccccc), color-stop(1, #b2b2b2) );
 background:-moz-linear-gradient( center top, #cccccc 5%, #b2b2b2 100% );
 filter:progid:DXImageTransform.Microsoft.gradient(startColorstr="#cccccc", endColorstr="#b2b2b2"); background: -o-linear-gradient(top,#cccccc,b2b2b2);

 background-color:#cccccc;
 border:0px solid #000000;
 text-align:center;
 border-width:0px 0px 1px 1px;
 font-size:20px;
 font-family:Times New Roman;
 font-weight:bold;
 color:#000000;
}
.CSSTableGenerator tr:first-child:hover td{
 background:-o-linear-gradient(bottom, #cccccc 5%, #b2b2b2 100%); background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #cccccc), color-stop(1, #b2b2b2) );
 background:-moz-linear-gradient( center top, #cccccc 5%, #b2b2b2 100% );
 filter:progid:DXImageTransform.Microsoft.gradient(startColorstr="#cccccc", endColorstr="#b2b2b2"); background: -o-linear-gradient(top,#cccccc,b2b2b2);

 background-color:#cccccc;
}
.CSSTableGenerator tr:first-child td:first-child{
 border-width:0px 0px 1px 0px;
}
.CSSTableGenerator tr:first-child td:last-child{
 border-width:0px 0px 1px 1px;
}

</style>
</head>

<body>





<div class="CSSTableGenerator" >
<table>
<tr><td>Marquee Behaviour - Scroll</td></tr>
<tr><td> <marquee behavior="scroll" direction="left" scrollamount="2">Slower From Left</marquee></td></tr>
<tr><td><marquee behavior="scroll" direction="left" scrollamount="6">Faster From Left</marquee></td></tr>
<tr><td><marquee behavior="scroll" direction="right" >Faster From Right</marquee></td></tr>
<tr><td><marquee behavior="scroll" direction="up">Goes Up</marquee></td></tr>
<tr><td><marquee behavior="scroll" direction="down">Goes Down</marquee></td></tr>
</table>
</div>
<br/>
<br/>

<div class="CSSTableGenerator" >
<table>
<tr><td>Marquee Behaviour - Slide</td></tr>
<tr><td><marquee behavior="slide" direction="right">Slide from right. Stop at left</marquee></td></tr>
<tr><td><marquee behavior="slide" direction="left">Slide from left. Stop at right</marquee></td></tr>
</table>
</div>
<br/>
<br/>

<div class="CSSTableGenerator" >
<table>
<tr><td>Marquee Behaviour - Alternate</td></tr>
<tr><td><marquee behavior="alternate" direction="left">Alternate from Left</marquee></td></tr>
<tr><td><marquee behavior="alternate" direction="right">Alternate from Right</marquee></td></tr>
</table>
</div>
<br/>
<br/>

</body>
</html>

JSP implicit objects REQUEST with example

JSP implicit objects REQUEST with example
In all there are 9 implicit JSP objects.

In our last posts, we have learned
JSP implicit objects OUT with example,
JSP Implicit Object CONFIG with example ,
JSP Implicit Object APPLICATION with example ,
JSP Implicit object session with example ,
JSP implicit object PageContext with example,
JSP implicit object RESPONSE with example,

The request object is an instance of class implementing an javax.servlet.http.HttpServletRequest interface. This object holds the client request that is being sent either through Post or Get request.

Request object is used to fetch the following information:
1) Request Parameters - Returns the value of a request parameter as a String, or null if the parameter does not exist. It uses the getParameter() method to access the request parameter.
2) Header Information - Returns the value of an HTTP header. The method getHeaderNames() can be used to determine what headers are available.
3) Cookies - Get the array of cookies from the request using getCookies() method.
4) Query String - Uses the getQueryString() method to get the query string, if any, passed in the request. Returns null if no query string is passed. Query String is passed after the URL by appending a '?' mark. Multiple query strings are seperated using the '&' symbol.
For ex : Consider a URL : http://localhost:8080/StockQuote/jsp/ImplicitObjectRequest.jsp?firstName=Kumar&secondName=Bhatia
In this, request.getQueryString() will give "firstName=Kumar&secondName=Bhatia"
5) Request URI - Method getRequestURI() gives the "/StockQuote/jsp/ImplicitObjectRequest.jsp" from above URL. It helps in getting which page is getting called. Gets the URI to the current JSP page.
etc.

Below is the sample example usage of few methods from the request object:
 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
   pageEncoding="ISO-8859-1"%>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
 <html>  
 <head>  
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
 <title>Implicit Object 'request' Example</title>  
 </head>  
 <body>  
 <%-- request object example --%>  
 <strong>Request User-Agent</strong>: <%=request.getHeader("User-Agent") %><br><br>  
 <strong>Request Parameter Names</strong>: <%=request.getParameterNames() %><br><br>  
 <strong>Request Cookies</strong>: <%=request.getCookies() %><br><br>  
 <strong>Request Query String</strong>: <%=request.getQueryString() %><br><br>  
 <strong>Request URI</strong>: <%=request.getRequestURI() %><br><br>  
 </body>  
 </html>  
 

Output is :

Request User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.2; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)

Request Parameter Names: org.apache.tomcat.util.http.Parameters$NamesEnumeration@ac86bb

Request Cookies: [Ljavax.servlet.http.Cookie;@c16890

Request Query String: firstName=Kumar&secondName=Bhatia

Request URI: /StockQuote/jsp/ImplicitObjectRequest.jsp