java 使用 Servlet 创建超链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7091084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:35:10  来源:igfitidea点击:

Create hyperlink using Servlet

javaservlets

提问by hook38

I am new to Servlet, and this might be a dumb question, but I've been searching for a solution online for a long time, but still cannot find an answer. I am trying to insert a link using servlet. Everyday said simply use:

我是 Servlet 的新手,这可能是一个愚蠢的问题,但我已经在网上搜索了很长时间的解决方案,但仍然找不到答案。我正在尝试使用 servlet 插入链接。每天说简单地使用:

//response is the HttpServiceResponse
PrintWriter out = response.getWriter();
out.println("<A HREF=\"http://www.something.com\">link</A>");

however, every time when I do that, all the < turn into '&lt'; which was really annoying. Please let me know how to solve this? I am running my code on eclipse(Indigo), would that be a factor?

但是,每次我这样做时,所有 < 都会变成 '<'; 这真的很烦人。请让我知道如何解决这个问题?我在 eclipse(Indigo) 上运行我的代码,这会是一个因素吗?

Thank you

谢谢

回答by Jigar Joshi

Well this isnt' the way to use Servlet.. if you just need to put static link don't use java simple HTML is enough .

好吧,这不是使用 Servlet 的方式。如果您只需要放置静态链接,请不要使用 java 简单的 HTML 就足够了。

Or in case you need dynamic URL then

或者,如果您需要动态 URL,则

from Servlet

来自 Servlet

request.setAttribute("urlID",someValue);
//forward the request to jsp

on jsp

在jsp上

<a href="http://staticPartOfURl?id=${urlID}"> click me</a>

回答by jirka.pinkas

Your code should definitely work. I just tested it on Eclipse Indigo and bare Apache Tomcat 7. There must be something else that does the translation (for example some filter, servlet etc.).

您的代码绝对可以工作。我刚刚在 Eclipse Indigo 和裸 Apache Tomcat 7 上对其进行了测试。必须有其他东西可以进行转换(例如某些过滤器、servlet 等)。

Jigar Joshi is also right - you shouldn't try to it this way anyway. Much cleaner is splitting this code to two parts - Servlet and JSP as shown.

Jigar Joshi 也是对的——无论如何你都不应该尝试这样做。更清晰的是将此代码分为两部分 - Servlet 和 JSP,如图所示。