简体   繁体   中英

Set an html page as servlet response

I've defined a webservlet as follows:

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

You just forward request to jsp page

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

You updated question from jsp to 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

response.sendRedirect("/yourHtml.html")

or you still can forward request to html

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

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