Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Colorize String Results in JSP?

In my jsp file below last <%=rs.getString("results")%> as results returns 3 different results as W,L and D.
How can I show W for green, L for red and D for yellow for using if condition and/or other statement?

If any problem I added code to this link: https://codeshare.io/EBbOyO

Thanks in advance to those who will help.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    <table class="table ">
  <thead>
    <tr class="d-flex">
      <th class="text-center col-xs-1" style="width:1%" scope="col">Week</th>
      <th class="text-center col-xs-1"  scope="col">Match Date</th>
      <th  class="text-left  col-xs-1" scope="col">Home</th>
      <th  class="text-left  col-xs-1" scope="col">Away</th>
      <th class="text-center col-xs-1" style="width:5%"  scope="col">Home Score</th>
      <th class="text-center col-xs-1" style="width:5%"  scope="col">Away Score</th>
            <th class="text-center col-xs-1" style="width:5%"  scope="col">Result</th>
    </tr>

<%
    if(request.getParameter("uid")!=null)
    {
        int id=Integer.parseInt(request.getParameter("uid")); 
        String dburl="jdbc:mysql://localhost:3306/teams"; 
        String dbusername="root";
        String dbpassword="Fener2013"; 

        try
        {
            Class.forName("com.mysql.cj.jdbc.Driver"); 
            Connection con=DriverManager.getConnection(dburl,dbusername,dbpassword); 
            PreparedStatement pstmt=null; //create statement

            pstmt=con.prepareStatement("select m.week, m.match_date, t1.team_name hometeam, m.home_score as homescore, t2.team_name awayteam,m.away_score as awayscore,l.league_name,  CASE WHEN m.home_score > m.away_score THEN 'W' WHEN m.home_score < m.away_score THEN 'L' WHEN m.home_score = m.away_score THEN 'D' END AS results from matches m join teams  t1 on m.home_team_id = t1.team_id join teams  t2 on m.away_team_id = t2.team_id join   leagues l on l.league_id = m.league_id where  m.home_team_id = ? or m.away_team_id = ?");          
            pstmt.setInt(1,id);
            pstmt.setInt(2,id); 
            ResultSet rs=pstmt.executeQuery(); 

            while(rs.next())
            {
                %> 
  <tbody>
    <tr class="d-flex">
      <td class="something col-xs-1" style="width:1%"><%=rs.getString("m.week")%></td>
      <td class="something col-xs-1" ><%=rs.getString("m.match_date")%></td>
      <td class="text-left col-xs-1" ><%=rs.getString("hometeam")%></td>
      <td class="text-left col-xs-1" ><%=rs.getString("awayteam")%></td>
      <td class="something col-xs-1" style="width:5%"  ><%=rs.getString("homescore")%></td>
      <td class="something col-xs-1" style="width:5%"  ><%=rs.getString("awayscore")%></td>
<!--     Below Strings returns 3 different result as W,L and D. How can I show W for green, L for red and D for yellow for using if or other statement? -->
         
    <td class="something col-xs-1" style="width:5%"> <span class="label label-success"><%=rs.getString("results")%></span></td>
    
    </tr>
         </tbody>
                <%
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
%>
</table> 

>Solution :

<span class='label label-success color-<%=rs.getString("results")%>'><%=rs.getString("results")%></span>

You can pass result into class, and add styling

<style>
.color-W{
color:green;
}
.color-L{
color:red;
}
.color-D{
color:yellow;
}
</style>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading