【问题标题】:how to remove jsessionid from html (converted from jsp)如何从html中删除jsessionid(从jsp转换而来)
【发布时间】:2012-07-18 12:15:22
【问题描述】:

我有一个(struts 2)动作,它被称为重定向到一个jsp。我正在尝试使用 HttpClient 将此 jsp 保存为 html。除了一件事之外,这很好用,服务器将jsessionid=RandomText 附加到 html 中存在另一个操作的任何地方。

如何从最终的 html 中删除 jsessionid?

来自渲染的 html 的示例:

<a href='/wah/destinationSEO.action;jsessionid=123ASDGA123?idDestination=22'>

这就是我将 jsp 保存到 html 的方式:

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet getRequest = new HttpGet("http://127.0.0.1:8080/wah/destinationSEO.action?idDestination=8");

HttpResponse response = httpClient.execute(getRequest);

if (response.getStatusLine().getStatusCode() != 200) {
    throw new RuntimeException("Failed : HTTP error code : "
            + response.getStatusLine().getStatusCode());
}
httpClient.getConnectionManager().shutdown();

String content = CharStreams.toString(new InputStreamReader(response.getEntity().getContent()));

String path="D:/wahSVN/trunk/src/main/webapp/seo.html";
File html = new File(path);
html.createNewFile();
Files.append(content, html, Charsets.UTF_8);

【问题讨论】:

    标签: java http jsp get apache-httpclient-4.x


    【解决方案1】:

    使用 Java RegularExpression 并从内容中替换 jsessionid=&lt;ID&gt; up to ?

    【讨论】:

    • 相反,我可以在请求或客户端中指定一些内容吗?另外,你能帮我表达一下吗?
    【解决方案2】:

    按照 amicnh 的建议,正则表达式成功了!我希望我可以在客户端/请求中指定一些内容,但是哦,好吧。

    对于其他任何人,这就是它所做的:

        // code as written above
        Pattern pattern = Pattern.compile(";jsessionid(=\\w+)");
        Matcher m = pattern.matcher(content);
    
        content = m.replaceAll("");
    
        Files.write(content, html, Charsets.UTF_8);
    

    这两个链接很有帮助:

    【讨论】:

      猜你喜欢
      • 2019-08-13
      • 1970-01-01
      • 2012-07-04
      • 2015-03-07
      • 2015-10-25
      • 1970-01-01
      • 2012-08-26
      • 1970-01-01
      相关资源
      最近更新 更多