简体   繁体   English

将html页面设置为servlet响应

[英]Set an html page as servlet response

I've defined a webservlet as follows: 我已经定义了一个Webservlet如下:

@WebServlet(name = "HomeWebServlet", urlPatterns = "/")
public class HomeWebServlet extends HttpServlet {

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

 }

How can I set as response an html page ( index.html ) placed in src/main/webapp/resources folder? 如何将放置在src/main/webapp/resources文件夹中的html页面( index.html )设置为响应?

You just forward request to jsp page 您只是forward请求forward到jsp页面

String nextJSP = "/yourJsp.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
dispatcher.forward(request, response);

You updated question from jsp to html 您已将问题从jsp更新为html

in that case you just need to redirect user to goto HTML, since src/main/webapp is in public web space it would be available to user directly 在这种情况下,您只需要将用户重定向到HTML,因为src / main / webapp位于公共网站空间中,因此用户可以直接使用它

response.sendRedirect("/yourHtml.html")

or you still can forward request to html 或者您仍然可以将请求转发到html

String nextHTML = "/yourHtml.html";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextHTML);
dispatcher.forward(request, response);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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