Denying direct access to jsp pages
Asked Answered
L

4

15

I'm using struts 1.3 for my application and all jsp pages are forwarded through controller (action class). But If I access the jsp page directly, I'm able to access it. How do I prevent this?

Liberal answered 2/9, 2011 at 10:58 Comment(0)
F
15

put all your jsp-s inside WEB-INF folder (for example in WEB-INF/jsp folder) and dont forget to change your mapping regarding location of jsp-s.

Fipple answered 2/9, 2011 at 11:9 Comment(2)
+1 - WEB-INF hiding is the most commonly used method. If you read docs and examples they often use this setup.Almazan
Seems Jetty doesn't allow this to work, unfortunately. Maybe a bad setting on my end?Alcantar
H
3

Filters are used to bypass or interrupt the requests , so use the filters to restrict the request , if it not contains .do in url. Below is the good tutorial for filters

Filters

Hern answered 2/9, 2011 at 11:9 Comment(0)
A
2

I think the best option would be to put your web pages in the WEB-INF folder - that way they won't be directly accessible but then in your servlets you can have something like:

public class ControllerServlet extends HttpServlet {

    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        String userPath = request.getServletPath();

        // if category page is requested
        if (userPath.equals("/category")) {
            // TODO: Implement category request

        // if cart page is requested
        } else if (userPath.equals("/viewCart")) {
            // TODO: Implement cart page request

            userPath = "/cart";

        // if checkout page is requested
        } else if (userPath.equals("/checkout")) {
            // TODO: Implement checkout page request

        // if user switches language
        } else if (userPath.equals("/chooseLanguage")) {
            // TODO: Implement language request

        }

        // use RequestDispatcher to forward request internally
        String url = "/WEB-INF/view" + userPath + ".jsp";

        try {
            request.getRequestDispatcher(url).forward(request, response);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

Taken from: http://netbeans.org/kb/docs/javaee/ecommerce/page-views-controller.html

Antonelli answered 2/9, 2011 at 11:9 Comment(0)
N
1

You can use filters and restrict the request with url which ask for .jsp pages and only allow requests which ask for .do

Neuron answered 2/9, 2011 at 11:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.