gpt4 book ai didi

java - 如何创建从 Netbeans 中的 Java servlet 返回到 index.html 或其他页面的链接?

转载 作者:行者123 更新时间:2023-12-02 04:19:06 26 4
gpt4 key购买 nike

我想做的就是从 JSP 链接回 HTML 页面。我在网上找不到任何简单的例子。另外我应该把它放在哪里,在 doGet、doPost 或其他地方?我希望能够将其设为一个按钮,但此时我将采用任何有效的链接。

 import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet(urlPatterns =
{
"/a"
})
public class a extends HttpServlet
{


protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet a</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet a at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}


@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}


@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}


@Override
public String getServletInfo()
{
return "Short description";
}

}

最佳答案

in HTML you would do <a href="url">link text</a> in the servlet you would do

尝试response :

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<a href=\"index.html\">Back</a>");
out.close();

上面的代码将生成index.html的链接。

关于java - 如何创建从 Netbeans 中的 Java servlet 返回到 index.html 或其他页面的链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32983983/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com