We help IT Professionals succeed at work.
Get Started

passing a Map from javascript to Java

Rohit Bajaj
Rohit Bajaj asked
Last Modified: 2014-09-05
I have the following code in a jsp:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="/WEB-INF/classes/affiliate.tld" prefix="affiliate" %>
<%@ taglib uri="/WEB-INF/yatraTagLib.tld" prefix="yatra" %>

<c:if test="${(tenantScope eq 'dom') && (pageID eq 'search') && (trackAffiliate == true)}">
	<affiliate:scriptTag affiliateurl='${trackerBaseUrl}/affiliateLogger?version=DFTL&PAGE_TYPE=CONFIRM&PRODUCT_TYPE=AIR&logger=TRUE' paramMap='${affiliationParamMap}' />					      		

Open in new window

Here the affiliationParamMap is being generated in java code and is passed to the jsp in a model.

Affiliate.tld :

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag 
Library 1.2//EN"

Open in new window

package com.yatra.platform.commons.utils;

import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

import org.apache.log4j.Logger;

import com.yatra.platform.commons.property.PropertyManager;

public class YatraAffiliateHandler implements Tag{
	private static Logger logger = Logger.getLogger(YatraAffiliateHandler.class); 
	String affiliateurl;
	Map<String,String> paramMap;
	PageContext pageContext;
	public String getAffiliateurl() {
		return affiliateurl;

	public void setAffiliateurl(String affiliateurl) {
		this.affiliateurl = affiliateurl;

	public Map<String, String> getParamMap() {
		return paramMap;

	public void setParamMap(Map<String, String> paramMap) {
		this.paramMap = paramMap;

	public int doEndTag() throws JspException {
		// TODO Auto-generated method stub
		return 0;

	public int doStartTag() throws JspException {
		 try {
			 String result ="";
				 result = YatraUtils.getURLResponse(
				 logger.debug("Affiliation result : "+ result);
			 }catch (Exception e) {
				logger.error("Unable to get affilation string",e);
	  } catch (IOException ioe) {
		  throw new JspException("Exception caught :", ioe);
	  return SKIP_BODY;

	public Tag getParent() {
		// TODO Auto-generated method stub
		return null;

	public void release() {
		// TODO Auto-generated method stub

	public void setPageContext(PageContext pageContext) {
		this.pageContext = pageContext;

	public void setParent(Tag arg0) {
		// TODO Auto-generated method stub
	private String generateRequestParams(){
		StringBuilder builder = new StringBuilder();
			for(Entry<String,String> entry : paramMap.entrySet()){
		return builder.length()>2?builder.substring(1):"";

	private int getConnectionTimeout(){
		PropertyManager propertyManager = (PropertyManager)YatraSpringFactory.getBean("propertyManager");
		return propertyManager.getPropertyAsInt("affiliate.request.time.out");
	public String getCookies() {
		HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
		String text="";
 	    Cookie[] cookies = request.getCookies();
		  if(cookies != null){
			  for(int i = 0; i < cookies.length;i++){
		      		text = text + ";";
			  text = text + cookies[i].getName() + "=" + cookies[i].getValue();
		return text;

Open in new window

Now i want to move the generation of affiliationParamMap to javascript.
How do i do it  and make the map work in jsp.

Watch Question
Chief Technology Officer
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE