I have a servlet which I want to return a resource, or at least the html from the resource, index.html
which is located in my webapps folder.
I'm very new and haven't been able to find anything. Here's my code I would appreceiate any help!
public static void main(String[] args){
Server server = new Server(8080);
ResourceHandler resource_handler = new ResourceHandler();
resource_handler.setDirectoriesListed(true);
resource_handler.setWelcomeFiles(new String[]{"index.html"});
resource_handler.setResourceBase("./target/classes/webapp");
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
ServletHolder indexHolder = new ServletHolder(new IndexServlet());
context.addServlet(indexHolder, "/index");
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[]{resource_handler, context, new DefaultHandler()});
server.setHandler(handlers);
try {
server.start();
} catch (Exception ex) {
ex.printStackTrace();
}
}
This is my current doGet
method. The print staement currently is the string value of the index.html
file that I would like the servlet to return.
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().print(
"<!doctype html>\n" +
"<html>\n" +
"<head>\n" +
"<meta charset=\"utf-8\">\n" +
"<title>Form Page</title>\n" +
"</head>\n" +
"<body>\n" +
" <form id=\"jetty-form\" name=\"user-form\" method=\"post\">\n" +
" <label for=\"username\">Username:</label>\n" +
" <input type=\"text\" name=\"username\" id=\"username\">\n" +
"<input type=\"submit\">" +
" </form>\n" +
"</body>\n" +
"</html>");
}