We help IT Professionals succeed at work.
Get Started

passing a Map from javascript to Java

Rohit Bajaj
Rohit Bajaj asked
on
1,435 Views
Last Modified: 2014-09-05
Hi,
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}' />					      		
</c:if>

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"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
  <tlib-version>1.0</tlib-version>
  <jsp-version>1.1</jsp-version>
  <short-name>yatra</short-name>
  <tag>
 	 <name>scriptTag</name>
  	 <tag-class>com.yatra.platform.commons.utils.YatraAffiliateHandler</tag-class>
 	 <body-content>empty</body-content>
  	<attribute> 
  	      <name>affiliateurl</name>
  	       <required>true</required>
  	       <rtexprvalue>true</rtexprvalue> 
    </attribute> 
	<attribute> 
  	      <name>paramMap</name>
  	       <required>false</required>
  	       <type>java.util.Map</type>
  	       <rtexprvalue>true</rtexprvalue>
  	 </attribute>
   </tag>
</taglib>

Open in new window


YatraAffiliateHandler.java
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;
	}

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

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

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

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

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

	@Override
	public void setParent(Tag arg0) {
		// TODO Auto-generated method stub
		
	}
	
	private String generateRequestParams(){
		StringBuilder builder = new StringBuilder();
		if(paramMap!=null){
			for(Entry<String,String> entry : paramMap.entrySet()){
				builder.append("&").append(entry.getKey()).append("=").append(entry.getValue());
			}
		}
		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++){
		      	if(i>0){
		      		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.

Thanks
Comment
Watch Question
Chief Technology Officer
Commented:
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