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

时间:2015-10-07 04:47:52

标签: java html jsp servlets netbeans

我要做的就是从JSP链接到HTML页面。我无法在网上找到一个简单的例子。我还会把它放在doGetdoPost或其他地方?我希望能够将它设为按钮,但此时我将采用任何有用的链接。

 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";
}

}

1 个答案:

答案 0 :(得分:2)

  在HTML中,您可以在servlet中执行<a href="url">link text</a>   会做的

试试response

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

以上代码将生成指向index.html的链接。