B
B
Biaci_Anj2022-03-30 00:12:17
Java
Biaci_Anj, 2022-03-30 00:12:17

How to get rid of the repeated code in the servlet in this example?

In each servlet in doGet, I repeat this code to display the user's balance on each page.

BigDecimal balance = userServlet.getBalance();
request.setAttribute("balance", balance);


Is it normal? And is there a way to get it out somehow so as not to repeat itself?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Cheremisin, 2022-03-30
@Biaci_Anj

You can write a filter (implemented javax.servlet.Filter) into which to insert these lines - tutorials.jenkov.com/java-servlets/servlet-filters.html
And in general, you can put all the work with the user into the filter...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question