MrPhreaker
Programmer
Hi all
Please can you answer this for me as I cannot seem to solve it :
I have a JAVA servlet which is meant to pick a particular XSL file based on the parameter I send it.
However, whatever parameter I send it, it seems to pick people.xsl file.
Please can you advise ?
Thanks
Greg
public class UseStylesheetParamServlet extends HttpServlet {
/**
* String representing the file separator characters for the System.
*/
public final static String FS = System.getProperty("file.separator"
;
PrintWriter out;
String xslFile, xmlFile, paramValue;
public void doGet(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException {
try {
res.setContentType("text/html; charset=UTF-8"
;
out = res.getWriter();
paramValue = req.getParameter("PVAL"
;
// xmlFile = req.getParameter("XML"
;
// xslFile = req.getParameter("XSL"
;
if (paramValue == null) {
out.println(
"<h1>No input for paramValue</h1>"
;
return;
}
//if ( xmlFile == null) {
// out.println(
// "<h1>No input for xmlFile</h1>"
;
// return;
//}
//if ( xslFile == null) {
//out.println(
// "<h1>No input for xslFile</h1>"
;
// return;
//}
//test
//private static final String XML = "file:///C:/temp/JDeveloper9032/jdev/mywork/Workspace2/Project2/src/fooparam.xml";
//private static final String XSL = "file:///C:/temp/JDeveloper9032/jdev/mywork/Workspace2/Project2/src/fooparam.xsl"
// get the real path for xml and xsl files;
String ctx = getServletContext().getRealPath(""
+ FS;
xmlFile = "file:///C:/temp/JDeveloper9032/jdev/mywork/Workspace2/Project2/src/people.xml";
if (paramValue == "people"
{
xslFile = "file:///C:/temp/JDeveloper9032/jdev/mywork/Workspace2/Project2/src/people.xsl";
}
if (paramValue == "Bacon"
{
xslFile = "file:///C:/temp/JDeveloper9032/jdev/mywork/Workspace2/Project2/src/Bacon.xsl";
}
if (paramValue == "Guin"
{
xslFile = "file:///C:/temp/JDeveloper9032/jdev/mywork/Workspace2/Project2/src/Guin.xsl";
}
if (paramValue == "Miller"
{
xslFile = "file:///C:/temp/JDeveloper9032/jdev/mywork/Workspace2/Project2/src/Miller.xsl";
}
if (paramValue == "Roe"
{
xslFile = "file:///C:/temp/JDeveloper9032/jdev/mywork/Workspace2/Project2/src/Roe.xsl";
}
else
{
xslFile = "file:///C:/temp/JDeveloper9032/jdev/mywork/Workspace2/Project2/src/people.xsl";
}
out.println(paramValue);
out.println(xslFile);
out.println(xmlFile);
TransformerFactory tFactory =
TransformerFactory.newInstance();
Transformer transformer =
tFactory.newTransformer(new StreamSource(xslFile));
// Set the stylesheet parameter (named param1).
transformer.setParameter("param1", paramValue);
// Perform the transformation.
transformer.transform(new StreamSource(xmlFile),
new StreamResult(out));
}
catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
catch (TransformerException e) {
e.printStackTrace(out);
return;
}
}
}
Please can you answer this for me as I cannot seem to solve it :
I have a JAVA servlet which is meant to pick a particular XSL file based on the parameter I send it.
However, whatever parameter I send it, it seems to pick people.xsl file.
Please can you advise ?
Thanks
Greg
public class UseStylesheetParamServlet extends HttpServlet {
/**
* String representing the file separator characters for the System.
*/
public final static String FS = System.getProperty("file.separator"
PrintWriter out;
String xslFile, xmlFile, paramValue;
public void doGet(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException {
try {
res.setContentType("text/html; charset=UTF-8"
out = res.getWriter();
paramValue = req.getParameter("PVAL"
// xmlFile = req.getParameter("XML"
// xslFile = req.getParameter("XSL"
if (paramValue == null) {
out.println(
"<h1>No input for paramValue</h1>"
return;
}
//if ( xmlFile == null) {
// out.println(
// "<h1>No input for xmlFile</h1>"
// return;
//}
//if ( xslFile == null) {
//out.println(
// "<h1>No input for xslFile</h1>"
// return;
//}
//test
//private static final String XML = "file:///C:/temp/JDeveloper9032/jdev/mywork/Workspace2/Project2/src/fooparam.xml";
//private static final String XSL = "file:///C:/temp/JDeveloper9032/jdev/mywork/Workspace2/Project2/src/fooparam.xsl"
// get the real path for xml and xsl files;
String ctx = getServletContext().getRealPath(""
xmlFile = "file:///C:/temp/JDeveloper9032/jdev/mywork/Workspace2/Project2/src/people.xml";
if (paramValue == "people"
xslFile = "file:///C:/temp/JDeveloper9032/jdev/mywork/Workspace2/Project2/src/people.xsl";
}
if (paramValue == "Bacon"
xslFile = "file:///C:/temp/JDeveloper9032/jdev/mywork/Workspace2/Project2/src/Bacon.xsl";
}
if (paramValue == "Guin"
xslFile = "file:///C:/temp/JDeveloper9032/jdev/mywork/Workspace2/Project2/src/Guin.xsl";
}
if (paramValue == "Miller"
xslFile = "file:///C:/temp/JDeveloper9032/jdev/mywork/Workspace2/Project2/src/Miller.xsl";
}
if (paramValue == "Roe"
xslFile = "file:///C:/temp/JDeveloper9032/jdev/mywork/Workspace2/Project2/src/Roe.xsl";
}
else
{
xslFile = "file:///C:/temp/JDeveloper9032/jdev/mywork/Workspace2/Project2/src/people.xsl";
}
out.println(paramValue);
out.println(xslFile);
out.println(xmlFile);
TransformerFactory tFactory =
TransformerFactory.newInstance();
Transformer transformer =
tFactory.newTransformer(new StreamSource(xslFile));
// Set the stylesheet parameter (named param1).
transformer.setParameter("param1", paramValue);
// Perform the transformation.
transformer.transform(new StreamSource(xmlFile),
new StreamResult(out));
}
catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
catch (TransformerException e) {
e.printStackTrace(out);
return;
}
}
}