Solved

need to call Servlet from JSP

Posted on 2006-06-14
8
338 Views
Last Modified: 2010-04-01
the following code works from command line, but I could not get it to work from tomcat.  the codeshould take 2 parameters and connect to remote webservice and return encrypted URL and redirect the user to the new site.   in my JSP, I did thefollowing:


imageURL="https://thewebsitewherewebservice";
            rNumber=12345678;
            password="mypassword";
            institutionID=1111;
            memberNumber =1234;
            String param=rNumber+","+password+","+institutionID+","+rNumber;
            System.out.println(param);
            myTestClass test = new myTestClass(String imageURL,String param);

tomcat return error:

error: File C:\summit\morris\homebanking\WEB-INF\classes\com\summitsite\homebanking\EmpireBillpaySingleSignonChannelData$MyX509TrustManager.class does not contain nested type com.summitsite.homebanking.EmpireBillpaySingleSignonChannelData. MyX509TrustManager as expected, but type EmpireBillpaySingleSignonChannelData$MyX509TrustManager. Please remove the file, or make sure it appears in the correct subdirectory of the class path.


An error occurred between lines: 26 and 142 in the jsp file: /jsp/Member/billpay.jsp

Generated servlet error:
C:\summit\morris\tomcat\work\test$jsp.java:685: Wrong number of arguments in constructor.
               myTestClass test = new myTestClass(imageURL,param);



he servlet code:

import java.io.*;
import java.net.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import com.sun.net.ssl.*;
import javax.net.ssl.SSLSocketFactory;
import javax.security.cert.*;
import javax.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.text.SimpleDateFormat;
import java.util.*;
import sun.misc.*;




public class myTestClass {
    public myTestClass() {
            java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
            System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");

    }

    public void init(String args){
        StringTokenizer token = new StringTokenizer (args,",");
        if (token.hasMoreTokens())
            imageURL=token.nextToken();
        if (token.hasMoreTokens())
            routingNumber=token.nextToken();
        if (token.hasMoreTokens())
            password=token.nextToken();
        if (token.hasMoreTokens())
            institutionID=token.nextToken();
        if (token.hasMoreTokens())
            errorURL=token.nextToken();
    }
    public String getRedirect(){
        if (imageURL!=null){
                  try {
                      javax.net.ssl.SSLSocketFactory sf = (javax.net.ssl.SSLSocketFactory)
                      javax.net.ssl.SSLSocketFactory.getDefault();
                      javax.net.ssl.SSLSocket sock = null;
                      X509TrustManager tm = new MyX509TrustManager();
                      KeyManager []km = null;
                      TrustManager []tma = {tm};
                      SSLContext sc = SSLContext.getInstance("SSL");
                      sc.init(km,tma,new java.security.SecureRandom());
                      SSLSocketFactory sf1 = sc.getSocketFactory();
                      HttpsURLConnection.setDefaultSSLSocketFactory(sf1);
                      
                      SimpleDateFormat df =  new SimpleDateFormat("yyyyMMddHHmmss");
                Date timestamp = new Date();
                      
                      String parameters="id="+routingNumber+"&pw="+password+"&corp="+institutionID+"&ts="+df.format(timestamp)+"&lgn="+memberNumber;
                      
                       String response=postData(imageURL,parameters);
                       print("myTestClass response="+response);
                       String reponseCode="00";
                       
                       if (response.startsWith("00")){ //good
                           StringTokenizer token = new StringTokenizer(response,"|");
                           if (token.hasMoreTokens())
                               reponseCode=token.nextToken();
                           if (token.hasMoreTokens()) {
                               redirectURL=token.nextToken();
                               return redirectURL;
                           }
                           return errorURL;
                       }
                    else {
                        return errorURL;
                    }
                  }
                  catch (Exception e){
                        e.printStackTrace();
                  return errorURL;
                  }
        }
        else
            return errorURL;

    }
    public String postData(String urlstr, String parameters) throws IOException      {
          HttpURLConnection hpcon = null;
            try{
                URL url = new URL(urlstr);
                print("myTestClass request="+url+", parameters="+parameters);
                  hpcon = (HttpURLConnection) url.openConnection();            
                  hpcon.setRequestMethod("POST");
                  hpcon.setDoInput(true);
                  hpcon.setDoOutput(true);
                  hpcon.setUseCaches (false);
                  
                  //Get the outputStream so we can send the query
                PrintWriter out = new PrintWriter(hpcon.getOutputStream());
               
                // POST the query string to the server
                out.print(parameters);
                out.close();
                  
                  // getting the response is required to force the request, otherwise it might not even be sent at all
                  BufferedReader in = new BufferedReader(new InputStreamReader(hpcon.getInputStream()));
                  String input;
                  StringBuffer response = new StringBuffer(256);
              
                  while((input = in.readLine()) != null) {
                        response.append(input + "\r");
                  }
                  in.close();
                  return response.toString();
          }
          catch(Exception e){
                  throw new IOException(e.getMessage());      
            }
            finally {
                if(hpcon != null){
                      hpcon.disconnect();      
                }
            }
      }
      public static void print(String str) {
            System.out.println("date "+new Date()+" - "+str);
      }
      protected void setMemberNumber(String mnum){
            memberNumber = mnum;
      }
      public static void main(String args[]){
            myTestClass ebp = new      myTestClass();
            ebp.init(args[0]);
            ebp.setMemberNumber(args[1]);
            print (ebp.getRedirect());
      }

    private String memberNumber="";
    private String password="";
    private String imageURL="";
    private String institutionID="";
    private String redirectURL="";
    private String errorURL="";
    private String routingNumber="";
   
    class MyX509TrustManager implements X509TrustManager {
            public boolean isClientTrusted(java.security.cert.X509Certificate[] chain) {
                  return true;
            }
            public boolean isServerTrusted(java.security.cert.X509Certificate[] chain) {
                  return true;
            }
            public java.security.cert.X509Certificate[] getAcceptedIssuers(){
                  return null;
            }
      }
}


Thanks



0
Comment
Question by:salasadi
  • 4
  • 2
8 Comments
 
LVL 8

Expert Comment

by:colr__
ID: 16902847
The myTestClass  class takes no args, but you are attepting to supply 2 of them.

colr__
0
 
LVL 8

Expert Comment

by:colr__
ID: 16902859
The line in your JSP:

myTestClass test = new myTestClass(String imageURL,String param);

Yet your constructor onluy accepts no args:

public myTestClass()
0
 

Author Comment

by:salasadi
ID: 16902893
what I need to do toi fix it.  can you give me an example --I am not Java expert :(
0
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 8

Expert Comment

by:colr__
ID: 16902905
In the JSP, change the line

myTestClass test = new myTestClass(String imageURL,String param);

to the folloiwng:

myTestClass test = new myTestClass();
0
 

Author Comment

by:salasadi
ID: 16903474
if I do that in the JSP, how can I pass the URL and the parameters the servlet need?

0
 
LVL 8

Accepted Solution

by:
colr__ earned 125 total points
ID: 16903581
Thats a seperate issue. The way myTestClass is implemented, it cannot take any paramaters on construction - if you need it to pass in paramaters then your going to have to change the class myTestClass. The way it is just now, you wont be able to get it to compile as it as you've seen.

I also notice that you've said that myTestClass is a servlet, but it doesnt appear to be so. In order for a class to be a servlet it has to extend the HttpServlet class.

Unfortunately if your not a java expert, your going to find this quite difficult - you'll need to know the java language before you can propoperly take control of servlets and JSPs and the like.

colr__
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Fisheye tool 2 111
problem with  angularjs dropdwon search at object property (name) level 2 99
Reading data  from excel sheet and displaying 1 83
null output 3 25
Each year, investment in cloud platforms grows more than 20% (https://www.immun.io/hubfs/Immunio_2016/Content/Marketing/Cloud-Security-Report-2016.pdf?submissionGuid=a8d80a00-6fee-4b85-81db-a4e28f681762) as an increasing number of companies begin to…
This article outlines why you need to choose a backup solution that protects your entire environment – including your VMware ESXi and Microsoft Hyper-V virtualization hosts – not just your virtual machines.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question