How to Apply CSS in out.println() method in JSP and Servlet

Hello friends, Today I am going to show you how to apply CSS in the out.println() method using JSP and servlet. In this example, we will apply CSS within the out.println() using JSP pages and Servlet Controller.

Pervious Post: How to display a list of objects in JSP through Servlet

In this example, I am going to show the status message, it’s conditionally using out.println() method and will apply the CSS in this method.

So, given the way you can add the style to your JSP page using out.println() method.

			if (request.getAttribute("status") != null) {
				if ((Boolean) request.getAttribute("status") == true) {
					out.print("<p class=\"success\">");
					out.print("Data Inserted Success");
				} else {
					out.print("<p class=\"failure\">");
					out.print("Please Fillup all the fields");

In my, styel.css page I added the CSS styling code and I just called the class in the out.println() method.

.success {
	color: green;

.failure {
	color: red;

Note: We can escape the quotation(“) using the slash (\) character.

Hope it helps!

Leave a Reply