-1

I have a simple Java class in which I invoke a call to a JSP page, by launching a browser. I have this part working, but now I want to pass variables from the simple Java class to the JSP page. How can I do that?

Here is my code:

public static void openURL(String url) {
    String osName = System.getProperty("os.name");
    if (osName.startsWith("Windows")) {
        Runtime.getRuntime().exec(
                "rundll32 url.dll,FileProtocolHandler " + url);
    } else {
        String[] browsers = { "firefox", "opera", "konqueror", "epiphany",
                "mozilla", "netscape" };
        String browser = null;
        for (int count = 0; count < browsers.length && browser == null; count++) {
            if (Runtime.getRuntime()
                    .exec(new String[] { "which", browsers[count] })
                    .waitFor() == 0) {
                browser = browsers[count];
            }
        }
        Runtime.getRuntime().exec(new String[] { browser, url });
    }
}

Please help me.

Perception
  • 79,279
  • 19
  • 185
  • 195
tm1
  • 97
  • 1
  • 3
  • 14
  • How do you "call" the JSP? HTTP? – SJuan76 Mar 27 '12 at 09:03
  • possible duplicate of [How do I pass information from a servlet to a JSP page](http://stackoverflow.com/questions/2261420/how-do-i-pass-information-from-a-servlet-to-a-jsp-page) – Thilo Mar 27 '12 at 09:10
  • no Thilo.I want to pass the variables from the java main class not from servlet – tm1 Mar 27 '12 at 09:39

1 Answers1

3

If you call the JSP page through HTTP (I guess so), then you have to send the variables using GET or POST parameters.

For example, if your JSP page's URL is http://localhost:8080/webapp/my.jsp, you can call:

http://localhost:8080/webapp/my.jsp?param1=value1&param2=value2 
huelbois
  • 6,762
  • 1
  • 19
  • 21
  • No.i am call jsp page using java main class not GET or POST.so request,session parameteres are not used. – tm1 Mar 27 '12 at 09:09
  • public static void openURL(String url) { String osName = System.getProperty("os.name"); if (osName.startsWith("Windows")) {Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);} else {String[] browsers = {"firefox", "opera", "konqueror","epiphany", "mozilla", "netscape"}; String browser = null; for (int count = 0; count < browsers.length && browser == null;count++) {if (Runtime.getRuntime().exec(new String[]{"which", browsers[count]}).waitFor() == 0) {browser = browsers[count];}} Runtime.getRuntime().exec(new String[]{browser, url}); }} – tm1 Mar 27 '12 at 09:17
  • in above openURL method,i will pass the the url of the jsp page like "http://localhost:8080/WebService/register.jsp" – tm1 Mar 27 '12 at 09:20
  • 1
    If you invoke a browser with an url and add parameters like @huelbois mentioned, the browser sends the parameters via get. So you simply have to add the parameters. But why do you want to call the jsp with an external (Browser-)process? You could use an HTTPRequest. see http://stackoverflow.com/questions/5946932/httprequest-get-data-in-java – Andreas Mar 27 '12 at 10:30
  • Is there any reason why you call this URL using heavy-weight calls to external browser, instead of using a java library like HTTPClient ? – huelbois Mar 27 '12 at 10:45
  • cause i want to pass machine info.like harddisk serial number to jsp page.and jsp page is at server side and java program that fetch machine info.is at client side. – tm1 Mar 27 '12 at 11:19
  • so i want to pass harddisk serial number to jsp page.so how can i pass variable to jsp page? – tm1 Mar 27 '12 at 11:25
  • I still think that's something you don't need a browser for. Let's say that your java class on client side (call the class 'Client') has a method to fetch hdd serial : String getHddSerial(). From Client, you can call openURL("http://localhost:8080/WebService/register.jsp?hddserial="+getHddSerial()); (it's a very simplified sample, you may need to URL encode the serial). Well, openURL could use HTTP Client library instead of calling a browser, to open this URL and send the HDD serial to the java Server in the "hddserial" parameter. – huelbois Mar 27 '12 at 12:44
  • Where to get HTTPClient : http://hc.apache.org/httpclient-3.x/. Of course, there are many other java http client libraries. – huelbois Mar 27 '12 at 12:45
  • Thanks huelbois.i can access the variable through url rewrite method like you post in answer. – tm1 Mar 28 '12 at 06:59