How can I hide the JSP file in WEB-INF folder? [duplicate]
Asked Answered
S

2

7

In a simple Java web application, for example imagine you have a servlet TestServlet.java. In deployment description ( web.xml ) you can for example map the request coming to say /testpage to TestServlet so that when /testapplication/testpage is requested TestServlet handles the request. And you can for example write "Hello World" and send the response.

In directory structure ( the application that is deployed to the web server ), TestServlet.java will reside in:

webapps\testapplication\WEB-INF\classes\com\packagename\TestClass.java

which means there is no way to get to this file using the browser. ( Like entering a URL )

You can also get the request dispatcher and forward the request and response object to a JSP file like .getRequestDispatcher("/test.jsp"). But then the file will be in

webapps\testapplication\test.jsp

so connecting to http:\\server.com\test.jsp will also get this file.

I want to hide the file in WEB-INF folder so it can not be reached by the client except the mapping I have provided.

What is the appropriate way for doing this?

Sakai answered 7/11, 2013 at 18:24 Comment(3)
What do u mean by hide the file in WEB-INF folder? .Os
I want the file to go under WEB-INF folder. The jsp file.Sakai
As usual BalusC gives the correct answer: #4229535Sakai
O
10

I want to hide the file in WEB-INF folder so it can not be reached by the client.

Keep your jsp files under WEB-INF for example - (WEB-INF/jsp), so that by default Web Containers does not allow resources under WEB-INF folder to be accessed directly by clients, but the RequestDispatcher can access it.

request.getRequestDispatcher("/WEB-INF/test.jsp").forward(request, response);

Os answered 7/11, 2013 at 18:38 Comment(4)
But my question is, in java servlet, how should I give the path to the jsp file? Like getRequestDispatcher("WEB-INF/test.jsp") ? Is this the right way?Sakai
Your answer is not correct, if you correct it I will mark it as accepted.Sakai
Thank you @KorayTugay :). I missed out giving complete code snippet line. Now I have corrected it.Os
"by defaul Web Containers [do] not allow resources under WEB-INF folder to be accessed directly" - this does not seem to be the case for Jetty. Jetty happily serves up everything under WEB-INF.Failing
P
0

If you are using jsf, you can put this file in WEB-INF folder and use a navigation rule to mapping the request to the page. But if you are not, you can create a filter and map it to url "/test.jsp" using the <url-pattern> attribute and redirect to anywhere you want (like a page in WEB-INF).

Persecution answered 7/11, 2013 at 18:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.