Home > Software design >  How to Colorize String Results in JSP?
How to Colorize String Results in JSP?

Time:02-02

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.

    <table >
  <thead>
    <tr >
      <th  style="width:1%" scope="col">Week</th>
      <th   scope="col">Match Date</th>
      <th   scope="col">Home</th>
      <th   scope="col">Away</th>
      <th  style="width:5%"  scope="col">Home Score</th>
      <th  style="width:5%"  scope="col">Away Score</th>
            <th  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 >
      <td  style="width:1%"><%=rs.getString("m.week")%></td>
      <td  ><%=rs.getString("m.match_date")%></td>
      <td  ><%=rs.getString("hometeam")%></td>
      <td  ><%=rs.getString("awayteam")%></td>
      <td  style="width:5%"  ><%=rs.getString("homescore")%></td>
      <td  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  style="width:5%"> <span ><%=rs.getString("results")%></span></td>
    
    </tr>
         </tbody>
                <%
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
%>
</table> 

CodePudding user response:

<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>
  •  Tags:  
  • Related