how to access files inside web inf folder
Direct accessing to
web-inf is not possible by client. And its
restricted. Files
inside the WEB-INF is only accessible by the
server.To access
files inside web-inf we need to create a
java servlet.
using that servlet
we can redirect the request to specified file
inside the web-inf
folder. Here we are using a sample pure jsp
project that uses a
login page 'login.jsp'. And its inside the
WEB-INF folder.
Inside WEB-INF/jsp/login.jsp.
This example code
will show how toredirect the request
to
login.jsp inside the web-inf folder.To do this
firstly we need
to modify the web.xml.
After modifying the
web.xml create a class names
LoginController.java'and
place the class inside'servletmapping'
package.
Code
is
given
below
pckage
servletmapping;
import
java.io.IOException;
import
javax.servlet.RequestDispatcher;
import
javax.servlet.ServletException;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
public
class LoginController extends HttpServlet {
@Override
protected void
doGet(HttpServletRequest req, HttpServletResponse resp)
throws
ServletException, IOException{
String
location = "/WEB-INF/jsp/login.jsp";
//
location of jsp file inside the web inf
RequestDispatcher
rd = getServletContext().getRequestDispatcher(location);
rd.forward(req,
resp);
}
}
This is how we redirect the
request inside the WEB-INF folder.
when we request '/app/login.jsp'
then the corresponding
servlet handle the request(that
we are defined inside the
web.xml) and serve the file
inside the web inf.And it is not
possible to access the file by
typing the exact
location inside the server that
is 'WEB-INF/jsp/login.jsp'
No comments:
Post a Comment