简体   繁体   English

在Java中执行JSP

[英]Executing JSP within java

I am trying to execute a JSP on my local machine by making a URL connection but i can't seem to get it to execute, i thought it could be the java not finding the JSP but i can't seem to find out why this could be as the URL to the JSP looks fine, anybody have an idea why it isn't executing? 我正在尝试通过建立URL连接在本地计算机上执行JSP,但我似乎无法执行它,我认为可能是Java未找到JSP,但我似乎找不到原因可能是因为JSP的URL看起来不错,所以有人知道为什么它没有执行吗? My code fragment and webapp structure is below. 我的代码片段和webapp结构如下。

Thanks. 谢谢。

       URL url = new URL("http://127.0.0.1/folder1/folder2/folder3/test.jsp");
       URLConnection connection = url.openConnection();
       connection.setDoOutput(true);
       OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
       out.write("id=" + id);
       out.close();

       BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
       String output;
       StringBuilder sb = new StringBuilder();
       boolean started = false;

       while ((output = in.readLine()) != null) {
           if (!output.trim().equals("")) {
                started = true;
           }
            if (started) {
                sb.append(output);
                sb.append("\r\n");
            }
        }
        in.close();
        System.out.println(sb.toString());

My Webapp has a similar structure to the following. 我的Webapp具有与以下类似的结构。

 webapp
    /WEB-INF
         /classes
         web.xml
    /folder1
       /folder2
          /folder3
             test.jsp

I think you might intend to do the following (without the outputting part): 我认为您可能打算执行以下操作(不包括输出部分):

URL url = new URL("http://127.0.0.1/folder1/folder2/folder3/test.jsp?id=" + id);

Use a StringBuilder for output - it is much more efficient. 使用StringBuilder进行output -效率更高。


Should text.jsp do both: ask in a form for id , and handle the form, then do not use: 如果text.jsp一举两得:问的形式id ,并处理的形式,那么就不要使用:

if (request.getMethod().equals("POST")) {
    String id = request.getParameter("id");
    ... handle given id
    return;
}
... show form

but use the GET/POST agnostic/irrelevant code: 但请使用与GET / POST无关的代码:

String id = request.getParameter("id");
if (id != null) {
    ... handle given id
    return;
}
... show form

So that the HTTP GET you are doing works. 这样您正在执行的HTTP GET就可以了。

I think you are missing your application name. 我认为您缺少应用程序名称。 If you are running your app inside tomcat then default port is 8080. 如果要在tomcat中运行应用程序,则默认端口为8080。

http://127.0.0.1:<port>/<web-app-name>/folder1/folder2/folder3/test.jsp

I give a troubleshooting approach below, given I don't know much about what failure message you are receiving. 鉴于我对您收到的故障消息了解不多,下面提供一种故障排除方法。

  1. I would try accessing another web page other than the URL you provided; 我会尝试访问您提供的URL以外的其他网页; just to prove your URLConnection approach is correct and that it is an issue with access to the JSP file. 只是为了证明您的URLConnection方法是正确的,并且这是访问JSP文件的问题。

  2. Look into the actual message/page being returned by your request. 查看您的请求返回的实际消息/页面。 Is it a 404 page? 是404页吗? If so then your URL is off. 如果是这样,则您的网址已关闭。

  3. And ensure you can access that URL directly in your browser. 并确保您可以直接在浏览器中访问该URL。

原来是由于从**http**://localhost/folder1/folder2/folder3/test.jsp重定向到**https**://localhost/folder1/folder2/folder3/test.jsp而导致的问题,当我在浏览器中打开JSP时,这并不是最容易发现的重定向,非常令人讨厌的是url.openConnection()似乎没有任何反应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM