3

I have x.jsp that can either be served as the whole response, or being included in several other jsp files.

So how can I know inside x.jsp if it is being included by another file or not?

joragupra
  • 692
  • 1
  • 12
  • 23
Paolo
  • 2,461
  • 5
  • 31
  • 45

3 Answers3

1

You can check the value of the attribute javax.servlet.include.request_uri in your x.jsp page. If set, the jsp page was included.

Further details here: Java HttpServletRequest get URL in browsers URL bar

Community
  • 1
  • 1
Laurent Legrand
  • 1,104
  • 6
  • 6
0

You can have a hidden field in the 'other files' and if you are including x.jsp you should set a value for it, and within x.jsp when you wanna know if its including you should check if that hidden fields value == 'value'.

Elye M.
  • 2,667
  • 4
  • 30
  • 43
0

If x.jps have knowledge about inclusion, it means that some best practices is ignored:

  • MVC pattern is thrown away because your x.jsp is not just a view, but also some kind of controller.
  • Possibly the Single responsibility principle is broken. Try split x.jsp to y.jsp and z.jsp, or introduce a variable which will contain value for appropriate case
  • It possibly increases coupling because x.jsp needs additional data about enviroment.

Of course these practices are not mandatory but you need a strong reason why you ignore them.

Please give us more information about your case or rephrase you question

Edit

My answer is abstract as your question is abstract. Get us your jsp, what should be changed and I hope you get better answer.

ADS
  • 708
  • 3
  • 14
  • Basically, it's better if the caller gives the thing it is calling all the information needed instead of having it reach back to the caller later. This is true in writing pure Java code too. You can use a program like grepwin to do a search and replace in all the JSP files. – Sarel Botha Jan 17 '14 at 17:34
  • Architecture (and bad habits) made us go away from "pure" MVC. (Pls don't tell me to refactor, it would cost hundreds of men days). Jsp files are not classes, and about coupling and so on take a look at useBean tag or session/request variables. Each Jsp file is more like a procedure call than an object. Thanks for your answer but it is not helpful for me. – Paolo Jan 18 '14 at 14:40