0

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:

//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?

Thank you

hook38
  • 3,899
  • 4
  • 32
  • 52

2 Answers2

4

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

Or in case you need dynamic URL then

from Servlet

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

on jsp

<a href="http://staticPartOfURl?id=${urlID}"> click me</a>
jmj
  • 237,923
  • 42
  • 401
  • 438
  • *Also See* :http://stackoverflow.com/questions/5818101/why-business-logic-should-be-moved-out-of-jsp – jmj Aug 17 '11 at 10:25
1

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.).

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.

jirka.pinkas
  • 918
  • 6
  • 21
  • Also tested it on a tomcat 5.5 from inside Eclipse, definitely no HTML escaping on the code that the OP published. – beny23 Aug 17 '11 at 10:39
  • I'm following through the tutorial on google app engine. I'm sure I won't do it this way in the future, but it was an very weird behavior that I encountered.... thanks for your post – hook38 Aug 17 '11 at 11:08
  • @user898378: Neverthess, interesting. Which server + version do you use, any frameworks in use? – home Aug 19 '11 at 07:01