Re: Help with Java servlet : getServletContext().



Hi Ross,
I did include the javax.servlet.http.HttpServlet.
Here is more code...if it helps.

import java.io.*;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
import java.util.Hashtable;
import java.util.EventObject;
import java.net.*;
import java.lang.String;
import java.io.StringWriter;
import java.lang.Integer;
import java.applet.*;
import javax.activation.DataHandler;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.GenericServlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
//import javax.servlet.ServletContext; 'this is a redundant class if
serverlet.* has already been imported.
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerConfigurationException;
import org.apache.xml.utils.SAXSourceLocator;
import org.apache.xml.utils.WrappedRuntimeException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import javax.xml.parsers.* ;
import javax.xml.parsers.DocumentBuilder;
...........
......
public int ProcessParams() {
transaction = "locmap";
template = "map.xsl";
url = GetParameter("url", "");

if(url == null || url.equals("")) {

Enumeration paramNames = req.getParameterNames();
int numParams = EnumSize(paramNames);

String trans = GetParameter("transaction",
"");

String city = GetParameter("city", "");
String stateProvince =
GetParameter("stateprovince", "");
String postalCode = GetParameter("postalcode",
"");
String recordId = GetParameter("recordid",
"");
String latitude = GetParameter("latitude",
"");
String longitude = GetParameter("longitude",
"");

String origcity = GetParameter("origcity",
"");
String origstateProvince =
GetParameter("origstateprovince", "");
String origpostalCode =
GetParameter("origpostalcode", "");
String origrecordId = GetParameter("origrecordid",
"");
String origlatitude = GetParameter("origlatitude",
"");
String origlongitude =
GetParameter("origlongitude", "");

String destcity = GetParameter("destcity",
"");
String deststateProvince =
GetParameter("deststateprovince", "");
String destpostalCode =
GetParameter("destpostalcode", "");
String destrecordId = GetParameter("destrecordid",
"");
String destlatitude = GetParameter("destlatitude",
"");
String destlongitude =
GetParameter("destlongitude", "");

if (trans != null &&
!trans.equals("") &&
!trans.equals("poimap") &&
nc_input.get("identifyicon") == null) {
if (trans.equals("route")) {

// route trans

// origin
if ((origcity == null ||
origcity.equals("") ||
origstateProvince == null ||
origstateProvince.equals("")) &&
(origpostalCode == null ||
origpostalCode.equals("")) &&
(origrecordId == null ||
origrecordId.equals("")) &&
(origlatitude == null ||
origlatitude.equals("")) &&
(origlongitude == null ||
origlongitude.equals(""))) {
error = 9000; // not enough info for the
origin

// destination
} else if ((destcity == null ||
destcity.equals("") ||
deststateProvince == null ||
deststateProvince.equals("")) &&
(destpostalCode == null ||
destpostalCode.equals("")) &&
(destrecordId == null ||
destrecordId.equals("")) &&
(destlatitude == null ||
destlatitude.equals("")) &&
(destlongitude == null ||
destlongitude.equals(""))) {
error = 9000; // not enough info for the
destination
}

} else {

// other type of trans
if ((city == null || city.equals("")
||
stateProvince == null ||
stateProvince.equals("")) &&
(postalCode == null ||
postalCode.equals("")) &&
(recordId == null ||
recordId.equals("")) &&
(latitude == null ||
latitude.equals("")) &&
(longitude == null ||
longitude.equals(""))) {
error = 9000; // not enough info for the
center
}

} // if (trans.equals("route"))
} // if (trans != null && (!(trans.equals(""))))


// EnumSize moved the pointer to the end of
paramNames.. reset
paramNames = req.getParameterNames();

while(paramNames.hasMoreElements()) {
String elem = (String)paramNames.nextElement();
String val = req.getParameter(elem);

if (elem.equals("template") &&
!(val == null || val.equals(""))) {
template = val + ".xsl";

} else if (elem.equals("transaction") &&
!(val == null || val.equals(""))) {
transaction = URLEncoder.encode(val);

} else {
if(! val.equals("")) {
// add to hashtable
passed.put(elem, val);

elem = URLEncoder.encode(elem);
val = URLEncoder.encode(val);

if(params.equals("")) {
params = "/?" + elem + "=" + val;
} else {
params += "&" + elem + "=" + val;
}
}
}
}

if (!(params == null || params.equals(""))) {
params += "&clientid=" + clientId;
passed.put("clientid", clientId);

params += "&passwd=" + passwd;
passed.put("passwd", passwd);

params += "&transaction=" + transaction;
passed.put("transaction", transaction);

url = "http://"; + server + params;
}

} else {

// Prepend the protocol and server to the returned url
url = "http://"; + server + "/?clientId=" + clientId +
"&" + url;

String tmpTemplate = GetParameter("template", "");
if (!(tmpTemplate == null || tmpTemplate.equals(""))) {
template = tmpTemplate + ".xsl";
}
}

xsltFile = new File(getServletContext().getRealPath("xslt/"
+ template));
if(!xsltFile.canRead()) {
System.out.println("Error reading XSLT template file '"
+ xsltFile + "'.");
return 0;
}

return 1;
} // ProcessParams



Thanks
Vidya

Ross Bamford wrote:
> On Fri, 2005-04-29 at 07:37 -0700, Vidya wrote:
> > I am newbie to Java, am trying to get a program working, and I have
> > imported the java packages below along with many other imports :
> > import javax.servlet.GenericServlet;
> > ....
> > xsltFile = new File(getServletContext().getRealPath("xslt/" +
> > template));
> > if(!xsltFile.canRead()) {
> > System.out.println("Error reading XSLT template
file '"
> > + xsltFile + "'.");
> > return 0;
> > }
> >
> > I get the following error, can anyone tell me what the problem is?
> >
> > com\netod\util\MyClass.java:243: cannot resolve symbol
> > symbol : method getServletContext ()
> > location: class com.netod.util.MyClass
> > xsltFile = new
File(getServletContext().getRealPath("xslt/"
> > + template));
> >
> > thanks for any help
> >
>
> Without seeing more of your code, but as a "newbie to Java" I think
it's
> a fair guess you're after writing a Web servlet, rather than creating
a
> whole new type of servlet (such as FTP)?
>
> If this is the case, instead of GenericServlet you should extend
> javax.servlet.http.HttpServlet.
>
> I'm not sure where the not found is coming from - perhaps you're
> implementing straight Servlet (which doesn't have it) ? If changing
to
> HTTPServlet doesn't help, post more of your code.
>
> Ross
>
> --
> [Ross A. Bamford] [ross AT the.website.domain]
> Roscopeco Open Tech ++ Open Source + Java + Apache + CMF
> http://www.roscopec0.f9.co.uk/ + info@xxxxxxxxxxxxxxxxxx

.



Relevant Pages