在正在运行的java程序的后台执行jsp程序

时间:2013-01-24 03:46:28

标签: java html jsp tomcat servlets

我只想在点击运行的java程序上的按钮时执行我的jsp程序,它不需要是可见的,我说的jsp程序是用于打印的,一旦它被加载到浏览器中它只会弹出打印对话框确认框,所以再一次不需要显示,一旦单击我的java程序中的按钮,就会弹出打印对话框并将其显示出来。顺便说一句,我在这个网站上是新手,也只知道java的基础知识,所以我不知道该怎么做,但我喜欢这样做,只需要一个jsp页面的链接localhost,类似的东西,

先谢谢好友!希望你能帮助我!...

1 个答案:

答案 0 :(得分:0)

这应该有效,不能确定它是否正常,请向我保证结果

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;


    public class callURL {
    public static void main(String[] args) 
    {
        String url = "http://localhost:8080/OpenID/asd.html";
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        HttpResponse response;
        StringBuilder builder= new StringBuilder();
        try 
        {
            response = httpClient.execute(httpPost);
            BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
            char[] buf = new char[8000];
            int l = 0;
                while (l >= 0) 
                {
                    builder.append(buf, 0, l);
                    l = in.read(buf);
                }

        } 
        catch (ClientProtocolException e) 
        {
            e.printStackTrace();
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        } 
    }
}

这个java程序来执行你的jsp

你必须在你的jsp页面中添加这一行

<script>
window.location.href="http://localhost:8080/OpenID/asd.html"
</script>

where OpenId : Application Name
asd.html is your jsp page , the same jsp which you are calling from java program