Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Basic Java IF...ELSE question

Status
Not open for further replies.

MrPhreaker

Programmer
Jun 20, 2003
19
GB
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(
&quot;<h1>No input for paramValue</h1>&quot;);
return;
}
//if ( xmlFile == null) {
// out.println(
// &quot;<h1>No input for xmlFile</h1>&quot;);
// return;
//}
//if ( xslFile == null) {
//out.println(
// &quot;<h1>No input for xslFile</h1>&quot;);
// return;
//}
//test
//private static final String XML = &quot;file:///C:/temp/JDeveloper9032/jdev/mywork/Workspace2/Project2/src/fooparam.xml&quot;;
//private static final String XSL = &quot;file:///C:/temp/JDeveloper9032/jdev/mywork/Workspace2/Project2/src/fooparam.xsl&quot;

// get the real path for xml and xsl files;
String ctx = getServletContext().getRealPath(&quot;&quot;) + FS;
xmlFile = &quot;file:///C:/temp/JDeveloper9032/jdev/mywork/Workspace2/Project2/src/people.xml&quot;;
if (paramValue == &quot;people&quot;){
xslFile = &quot;file:///C:/temp/JDeveloper9032/jdev/mywork/Workspace2/Project2/src/people.xsl&quot;;
}

if (paramValue == &quot;Bacon&quot;){
xslFile = &quot;file:///C:/temp/JDeveloper9032/jdev/mywork/Workspace2/Project2/src/Bacon.xsl&quot;;
}

if (paramValue == &quot;Guin&quot;){
xslFile = &quot;file:///C:/temp/JDeveloper9032/jdev/mywork/Workspace2/Project2/src/Guin.xsl&quot;;
}

if (paramValue == &quot;Miller&quot;){
xslFile = &quot;file:///C:/temp/JDeveloper9032/jdev/mywork/Workspace2/Project2/src/Miller.xsl&quot;;
}
if (paramValue == &quot;Roe&quot;){
xslFile = &quot;file:///C:/temp/JDeveloper9032/jdev/mywork/Workspace2/Project2/src/Roe.xsl&quot;;
}

else
{
xslFile = &quot;file:///C:/temp/JDeveloper9032/jdev/mywork/Workspace2/Project2/src/people.xsl&quot;;
}


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(&quot;param1&quot;, 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;
}
}
}

 
In java the &quot;==&quot; test is not used for testing if a String is the same characters as another String - it is used to test the &quot;equality&quot; of objects - not an objects value.

Replace all your statements like :

if (paramValue == &quot;Bacon&quot;){

with :

if (paramValue.equals(&quot;Bacon&quot;)){
 
Hi there

Thanks for the advice. I have a different problem now in that the only one that works is Roe (my last If).

I think it is something to do with the braces but I may be wrong....?
 
Your whole structure is a bit screwed . For all cases except for &quot;Roe&quot;, they will always get &quot;people.xsl&quot; beacsue right at the end, you are basically saying &quot;if paramValue is 'Roe', then use roe.xsl, and if it is not 'Roe', then use people.xsl&quot;.

Your structure should look more like :

Code:
     if (paramValue.equals( &quot;people&quot;)){
         xslFile = &quot;file:///C:/temp/JDeveloper9032/jdev/mywork/Workspace2/Project2/src/people.xsl&quot;;
     } else if (paramValue.equals( &quot;Bacon&quot;)) {
         xslFile = &quot;file:///C:/temp/JDeveloper9032/jdev/mywork/Workspace2/Project2/src/Bacon.xsl&quot;;
     } else if (paramValue.equals( &quot;Guin&quot;)) {
         xslFile = &quot;file:///C:/temp/JDeveloper9032/jdev/mywork/Workspace2/Project2/src/Guin.xsl&quot;;
     } else if (paramValue.equals( &quot;Miller&quot;)) {
         xslFile = &quot;file:///C:/temp/JDeveloper9032/jdev/mywork/Workspace2/Project2/src/Miller.xsl&quot;;
     } else if (paramValue.equals( &quot;Roe&quot;)) {
         xslFile = &quot;file:///C:/temp/JDeveloper9032/jdev/mywork/Workspace2/Project2/src/Roe.xsl&quot;;
     } else  {
        xslFile = &quot;file:///C:/temp/JDeveloper9032/jdev/mywork/Workspace2/Project2/src/people.xsl&quot;;
     }
 
Thank you, as you can tell my Java skills leave a lot to be desired. Give me PL/SQL and Oracle Forms any day of the week ! ;)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top