質問

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