Question

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?

Was it helpful?

Solution

Use an EL expression:

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top