PDA

View Full Version : Problem with setAttribute and getAttribute for passing a url between pages


pmjwales
08-31-2005, 07:19 PM
I am trying to pass a variable into a setAttribute(name, value) tag, I just can't get it to work.

I am trying to pass a constructed url into the session so that a number of pages can use it as a return to page.

The first page is initially submitted from a form.

The script I am using in the first page is:
<head>
<SCRIPT LANGUAGE="JavaScript">
var pageurl = "http://<%=request.getServerName()%>:<%=request.getServerPort()%><%=request.getParameter("_action")%>?wo=<%=request.getParameter("wo")%>&assetno=<%=request.getParameter("assetno")%>&assetlocn=<%=request.getParameter("assetlocn")%>&assetgrpcode=<%=request.getParameter("assetgrpcode")%>&originator=<%=request.getParameter("originator")%>&flt_code=<%=request.getParameter("flt_code")%>&work_area=<%=request.getParameter("work_area")%>&work_type=<%=request.getParameter("work_type")%>&status=<%=request.getParameter("status")%>&priority=<%=request.getParameter("priority")%>&workreq=<%=request.getParameter("workreq")%>&corr_action=<%=request.getParameter("corr_action")%>&start=<%=request.getParameter("start")%>&end=<%=request.getParameter("end")%>&wkaw_dates=<%=request.getParameter("wkaw_dates")%>&open_closed=<%=request.getParameter("open_closed")%>&readonly=<%=request.getParameter("readonly")%>";
</script>
</head>

<body >
<SCRIPT LANGUAGE="JavaScript">
<!--
window.alert("pageurl= "+pageurl);
//-->
</script>

<%session.setAttribute("listpage", pageurl);%>

Then on the subsequent pages I am using :

<%=session.getAttribute("listpage")%>

To retrieve the url.

Can anyone help, am I doing this the best way and what have I got wrong in the above syntax.

Thanks very much for any help

glenngv
09-01-2005, 06:28 AM
I think you're confused with server-side scripting (JSP) and client-side scripting (Javascript).
JSP is executed first on the server to generate HTML/CSS/Javascript. JSP just treats them as any other strings and does not evaluate them as variables or objects. Then the browser on the user's end renders/executes the generated HTML/CSS/Javascript output of JSP. In other words, you can't have a Javascript variable being retrieved directly by JSP.

The solution to your problem is to have a JSP variable hold the page url. Actually, I don't know why you are storing the page url in javascript. :confused:
<%
String pageurl = http://" + request.getServerName() + ":" + request.getServerPort() + request.getParameter("_action")
+ "?wo=" + request.getParameter("wo")
+ "&assetno=" + request.getParameter("assetno")
+ "&assetlocn=" + ...;

session.setAttribute("listpage", pageurl);
%>

Or you can simply form the querystring with a loop.
<%
String paramName = null;
StringBuffer pageurl = new StringBuffer();
try {
//form the url querystring
for (Enumeration e = request.getParameterNames() ; e.hasMoreElements() ;) {
paramName = (String) e.nextElement();
pageurl.append("&");
pageurl.append(paramName);
pageurl.append("=");
pageurl.append(URLEncoder.encode(request.getParameter(paramName), "UTF-8"));
}
}
catch(UnsupportedEncodingException e){ }

if (pageurl.length()>0){ //replace the first & with ?
pageurl.replace(0, 0, "?");
}
//insert the base url backwards
pageurl.insert(request.getParameter("_action"));
pageurl.insert(request.getServerPort());
pageurl.insert(request.getServerName());
pageurl.insert("http://");
session.setAttribute("listpage", pageurl.toString());
%>

pmjwales
09-01-2005, 09:58 AM
Couldn't get the loop option to work, but did get the first option working.

Your description of how javascript and jsp works helped alot.

For the loop option I had a number of errors starting with :

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 125 in the jsp file: /content/open/reports/am_review/workSearch.jsp

Generated servlet error:
[javac] Compiling 1 source file

E:\Tomcat41\work\Standalone\localhost\mviusers\content\open\reports\am_review\workSearch_jsp.java:13 7: cannot resolve symbol
symbol : class Enumeration
location: class org.apache.jsp.workSearch_jsp
for (Enumeration e = request.getParameterNames() ; e.hasMoreElements() ;) {
^

If it is something in the setup let me know as this solution would help me in other pages.

Thanks again