简体   繁体   中英

How do I create a hyperlink in java?

I'm going through the google app engine tutorials

I'm very new to google app engine, java and web programming in general. So my question is, at the bottom of the page it says to add a link to allow the user to log out. So far I've got this:

public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {
    UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();

    if(user != null){
        resp.setContentType("text/plain");
        resp.getWriter().println("Hello, " + user.getNickname());

        String logoutLink = String.format("<a href=\"%s\">Click here to log out.</a>",
            userService.createLogoutURL(req.getRequestURI()));
        resp.getWriter().println(logoutLink);
    }else {
        resp.sendRedirect(userService.createLoginURL(req.getRequestURI()));
    }
}

However instead of a link, the full string is printed to the screen including the tags. When I look at the page source, I have no tags or any of the other stuff that goes with a webpage. I guess that makes sense considering I've done nothing to output any of that. Do I just do a bunch of resp.GetWriter().println() statements to output the rest of the webpage, or is there something else I don't know about?

Thanks!

您需要将内容类型更改为text/html而不是text/plain<a href链接是一个html元素,然后只有浏览器才会将此链接作为链接。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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