JSP EL Headers Example

Click here to download eclipse supported ZIP file




 

    
/**
 
 */
package com.cv.jsp.el.attribute;

/**
 @author Chandra Vardhan
 *
 */
public class Dog {
  
  String name;
  

  public String getName() {
    return name;
  }


  public void setName(String name) {
    this.name = name;
  }


  /**
   
   */
  public Dog() {
    // TODO Auto-generated constructor stub
  }

}


 

    
/**
 
 */
package com.cv.jsp.el.attribute;

/**
 @author Chandra Vardhan
 *
 */
public class Person {
  private String name;
  private int empId;  
  private Dog dog;
  
  @Override
  public String toString() {
    return "Person [name=" + name + ", empID=" + empId + ", dog=" + dog
        "]";
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((dog == null: dog.hashCode());
    result = prime * result + empId;
    result = prime * result + ((name == null: name.hashCode());
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    Person other = (Personobj;
    if (dog == null) {
      if (other.dog != null)
        return false;
    else if (!dog.equals(other.dog))
      return false;
    if (empId != other.empId)
      return false;
    if (name == null) {
      if (other.name != null)
        return false;
    else if (!name.equals(other.name))
      return false;
    return true;
  }

  


  public Dog getDog() {
    return dog;
  }

  public void setDog(Dog dog) {
    this.dog = dog;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public int getEmpId() {
    return empId;
  }

  public void setEmpId(int empId) {
    this.empId = empId;
  }

  /**
   
   */
  public Person() {
  }

}


 

    
package com.cv.jsp.el.attribute;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

/**
 * Servlet implementation class ServletAttributeImpl
 
 *  @author Chandra Vardhan
 */
public class ServletAttributeImpl extends HttpServlet {

  private static final Logger LOGGER = Logger
      .getLogger(ServletAttributeImpl.class);

  protected void doPost(HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException {
    LOGGER.info("LOGGER : ServletAttributeImpl doGet(HttpServletRequest,HttpServletResponse)");
    // PrintWriter pw = res.getWriter();
    res.setContentType("text/html");
    String name = req.getParameter("name");
    String id = req.getParameter("empId");
    Person person = new Person();
    person.setName(name);
    person.setEmpId(Integer.parseInt(id));
    Dog dog = new Dog();
    dog.setName("Jokey");
    person.setDog(dog);
    req.setAttribute("person", person);

    // Arraylist
    List<String> al = new ArrayList<String>();
    al.add("Chandra");
    al.add("Shravya");
    al.add("Vardhan");

    req.setAttribute("list", al);

    Map<String, String> map = new HashMap<String, String>();
    map.put("key1""Baapu");
    map.put("key2""Akka");
    map.put("key3""Amma");
    req.setAttribute("map", map);
    Map<Person, String> map2 = new HashMap<Person, String>();
    map2.put(person, "person1");
    req.setAttribute("personMap", map2);
    String[] companies = "Amazon""Salesforce""Google""Microsoft" };
    req.setAttribute("companies", companies);
    RequestDispatcher rd = req.getRequestDispatcher("result.jsp");
    rd.forward(req, res);
  }

  /**
   @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
   *      response)
   */
  protected void doGet(HttpServletRequest request,
      HttpServletResponse responsethrows ServletException, IOException {
    LOGGER.info("LOGGER : ServletAttributeImpl doGet(HttpServletRequest ,HttpServletResponse )");
    doPost(request, response);
  }

}





<!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=US-ASCII">
<title>Login Form</title>
<style type="text/css">
</style>
</head>
<body>
<br>
<br>
<div align="center">
<form action="resultBestUI.jsp" method="post" id='test'>
<fieldset>
<legend>Plz enter login credentials...</legend>
<table align="center">
<tr>
<td><label>Enter Name : </label></td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td><label>Enter Employee Id : </label></td>
<td><input type="text" name="empId" /></td>
</tr>
<tr>
<td>Food one : </td>
<td><input type="text" name="f1"></td>
</tr>
<tr>
<td>Food two : </td>
<td><input type="text" name="f1"></td>
</tr>
<tr>
<td align="right"><input type="submit" value="Submit" /></td>
</tr>
</table>
</fieldset>
</form>
</div>
</body>
</html>



<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+" : : //"+request.getServerName()+" : : "+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'result.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
Name is  : : ${param.name}
<br />
Id  : : ${param.id}
<br />
food  : : ${paramValues.f1[0]}
<br />
food  : : ${paramValues.f1[1]}
<br />
Name is  : : ${paramValues.name[0]} <br/>
Host is : : <%= request.getHeader("host") %><br />
Method is : : <%= request.getMethod()  %><br />
Headers are : : <% 
String string ;
Enumeration e=request.getHeaderNames();
while(e.hasMoreElements()) {
Object object=e.nextElement();
//System.out.print(object);
 string = (String)object;
System.out.println(string);
request.setAttribute("str",string);
}


%><br />
Host is from EL1 header.referer : : ${header.referer}
<br />
Host is from EL1 accept - lang : : ${header["accept-language"]}
<br />
Host is from EL1 encoding : : ${header["accept-encoding"]}
<br />
Host is from EL1 agent : : ${header.user-agent}
<br />
Host is from EL1 header.host : : ${header.host}
<br />
Host is from EL1 header.connection  : :  ${header.connection}
<br />
Host is from EL1 header.cookie  : :  ${header.cookie}
<br />
Host is from EL2 : : ${header.host}
<br />
HTTP Method is  : : ${pageContext.request.method}<br/>
</body>
</html>



<%@ page language="java" import="com.cv.jsp.el.*"
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>Example of headers in jsp using EL...</title>
</head>
<body>
<fieldset>
<legend>Result page...</legend>
<table align="center">
<tr>
<td><label>I am from param name : : </label></td>
<td><font color="RED">${param.name}</font></td>
</tr>
<tr>
<td><label>I am from param empId : : </label></td>
<td><font color="RED">${param.empId }</font></td>
</tr>
<tr>
<td><font color="Blue">Accessing the param values from
Expression Language(EL) : </font></td>
</tr>
<tr>
<td><label>I am from param food 0 : : </label></td>
<td><font color="RED">${paramValues.f1[0]}</font></td>
</tr>
<tr>
<td><label>I am from paramValues.name[0]} : : </label></td>
<td><font color="RED"> ${paramValues.name[0]}</font></td>
</tr>
<tr>
<td><label>I am from param food 1 : : </label></td>
<td><font color="RED">${paramValues.f1[1]}</font></td>
</tr>
<tr>
<td><font color="Blue">Accessing request method type
from Expression Language(EL) : </font></td>
</tr>
<tr>
<td><label>I am from request method : : </label></td>
<td><font color="RED">${pageContext.request.method}</font></td>
</tr>
<tr>
<td><label>I am from request.getHeader("host"): : </label></td>
<td><font color="RED"><%= request.getHeader("host") %></font></td>
</tr>
<tr>
<td><label>I am from request.getMethod() : : </label></td>
<td><font color="RED"> <%= request.getMethod()  %></font></td>
</tr>
<tr>
<td><font color="Blue">Accessing header host from
Expression Language(EL) : </font></td>
</tr>
<tr>
<td><label>I am from header host : : </label></td>
<td><font color="RED">${header["host"]}</font></td>
</tr>
<tr>
<td><label>I am from {header.referer} : : </label></td>
<td><font color="RED">${header.referer}</font></td>
</tr>
</tr>
<tr>
<td><label>I am from {header["accept-language"]} : : </label></td>
<td><font color="RED">${header["accept-language"]}</font></td>
</tr>
</tr>
<tr>
<td><label>I am from {header["accept-encoding"]} : : </label></td>
<td><font color="RED">${header["accept-encoding"]}</font></td>
</tr>
</tr>
<tr>
<td><label>I am from {header.user-agent} : : </label></td>
<td><font color="RED">${header.user-agent}</font></td>
</tr>
<tr>
<td><label>I am from {header.host} : : </label></td>
<td><font color="RED">${header.host}</font></td>
</tr>
<tr>
<td><label>I am from {header.connection} : : </label></td>
<td><font color="RED">${header.connection}</font></td>
</tr>
<tr>
<td><label>I am from {header.cookie} : : </label></td>
<td><font color="RED">${header.cookie}</font></td>
</tr>
<tr>
<td><label>I am from {header.host} : : </label></td>
<td><font color="RED">${header.host}</font></td>
</tr>
</table>
</fieldset>
</body>
</html>





#By default enabling Console appender
# Root logger option
log4j.rootLogger=INFO, stdout

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p [%c]:%L -->> %m%n

# Redirect log messages to a log file
#log4j.appender.file=org.apache.log4j.RollingFileAppender
#log4j.appender.file.File=C:\\servlet-application.log
#log4j.appender.file.MaxFileSize=5MB
#log4j.appender.file.MaxBackupIndex=10
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n




	
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>JSPELHeadersProject</artifactId> <version>1.0</version> <packaging>war</packaging> <properties> <log4j-version>1.2.16</log4j-version> <java-version>1.8</java-version> </properties> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> <configuration> <warSourceDirectory>WebContent</warSourceDirectory> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>3.0-alpha-1</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> </dependencies> <groupId>com.cv.jsp.el.cookie</groupId> </project>


 
<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>Test Servlet</display-name> <welcome-file-list> <welcome-file>login.html</welcome-file> </welcome-file-list> </web-app>


1 comment:

  1. As we know there are many companies which are converting into Big data service providers. with the right direction we can definitely predict the future.

    ReplyDelete