How to get to application context path without using scriplets in JSP files?

StackOverflow https://stackoverflow.com/questions/23154665

  •  05-07-2023
  •  | 
  •  

سؤال

So I have a code like this in my jsp file:

<a href="<%= getServletConfig().getServletContext().getContextPath() %>/registerMe.jsp"
               class="btn">Not a member? Register..</a>

And I know that using scriplets is bad practice in JSP files. How can I avoid such a situation then?

هل كانت مفيدة؟

المحلول

Use an EL expression:

<a href="${pageContext.servletContext.contextPath}/registerMe.jsp"
           class="btn">Not a member? Register..</a>

نصائح أخرى

You can use request.getContextPath() in your action class, and you can pass that to JSP as a string using request, or you can use bean to get that in JSP.

Application scoped objects are stored as attributes of the ServletContext. If the "function call" has access to the ServletContext, then it can just grab them as follows:

Bean bean = (Bean) servletContext.getAttribute("beanname");

I of course expect that the "function" is running in the servlet context. I.e. it is (in)directly executed by a servlet the usual way.

You can also try this link. It have example with full explanation.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top