HttpServletRequest.getRequestURI
ユーザーがサーブレットにアクセスするときに入力したパスを取得できます。
これらの URI をホーム ディレクトリ内のファイルにマップするサーブレットを作成するにはどうすればよいですか。たとえば、ユーザーがサーブレットの URL を入力した場合
「http://localhost:8080/webbtechnologies/html/index.html」
ファイルを送る
C:\Users\User\My Documents\Web Technologies\html\index.html
ユーザーに。
これまでの私のコードは次のとおりです。
public class SimpleFileManagerServlet extends HttpServlet {
private String location;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html; charset=UTF-8");
resp.setStatus(HttpServletResponse.SC_OK);
PrintWriter out = resp.getWriter();
location = req.getRequestURI();
}
public static void main(String... args) throws Exception {
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.addServlet(SimpleFileManagerServlet.class, "/");
Server server = new Server(8080);
server.setHandler(context);
server.start();
server.join();
}
}