0

this is my JSP code for using a database to fetch the end time of a meal and compare it to the system time. I've used an if statement to see if time is before the end time, redirect to another page but show an error if time exceeds the end time of a meal.

Here's the catch, The if statement works but not in the way that I just explained.

Any ideas? Thanks in advance...

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <%
        String meal = request.getParameter("meal");

        Connection con = null;
        Statement st = null;
        ResultSet rs = null;

        try{
            con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/eoms?autoReconnect=true&useSSL=false","root","eoms");
                 st=con.createStatement();

               String select="SELECT * FROM eoms.schedule;";

                rs = st.executeQuery(select);

                String mealName = null;
                Time dueTime = null;
                Time time = null;
                int found = 0;

                while(rs.next()){
                    mealName = rs.getString(1);
                    dueTime = rs.getTime(3);
                    java.util.Date d = new java.util.Date();
                    time = new Time(d.getTime());

                    if(meal.equalsIgnoreCase(mealName) && time.before(dueTime)){
                        session.setAttribute("mealName", mealName);
                        con.close();
                        found = 1;
                        response.sendRedirect("header.jsp");
                    }
                    else
                        continue;
                }
                if(found == 0){
                    session.setAttribute("orderror",meal+mealName+time+dueTime+ "The time for taking the order is over...");
                    con.close();
                    response.sendRedirect("placeOrder.jsp");
                }
        }
        catch(Exception e){
            out.println(e.getMessage());
        }
        %>
    </body>
</html>
  • 1
    Add `break;` after `response.sendRedirect("header.jsp");`. --- And remove that dumb, redundant, superfluous `else continue;`, since the loop continues automatically in the very next line `}` anyway. – Andreas Mar 10 '20 at 14:20
  • 3
    It's a better idea to make the DB connection from the server side instead of doing it all on a JSP page. You can do the processing within a servlet, setting the desired session attributes and just try to fetch them in a JSP from sessionScope built-int variable. This will increase the readability. – ptr92zet Mar 10 '20 at 14:25
  • thanks guys but the problem was solved by using `LocalTime` in place of `getTime()` that I was using. Can you tell the reason behind it? – Arslan Haider Mar 10 '20 at 18:29

1 Answers1

0

as @Andreas mentioned in the comment, add break after response.sendRedirect("header.jsp") and remove else continue; .

For the rest, I suggest to use java.time.LocalTime to get the actual time and convert it into java.sql.Time using this Time.valueOf(java.time.LocalTime.now())

Replace the last java code with this

 <%
        String meal = request.getParameter("meal");

        Connection con = null;
        Statement st = null;
        ResultSet rs = null;

        try{
            con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/eoms?autoReconnect=true&useSSL=false","root","eoms");
            st=con.createStatement();

            String select="SELECT * FROM eoms.schedule;";

            rs = st.executeQuery(select);

            String mealName = null;
            Time dueTime = null;
            Time time = null;
            int found = 0;

            while(rs.next()){
                mealName = rs.getString(1);
                dueTime = rs.getTime(3);
                time = Time.valueOf( java.time.LocalTime.now() );
                if(meal.equalsIgnoreCase(mealName) && time.before(dueTime)){
                    session.setAttribute("mealName", mealName);
                    con.close();
                    found = 1;
                    response.sendRedirect("header.jsp");
                    break;
                }
            }
            if(found == 0){
                session.setAttribute("orderror",meal+mealName+time+dueTime+ "The time for taking the order is over...");
                con.close();
                response.sendRedirect("placeOrder.jsp");
            }
        }
        catch(Exception e){
            out.println(e.getMessage());
        }
%>
Bashir
  • 1,964
  • 5
  • 15
  • 34
  • thanks a lot @Bashir. turns out the problem was only that I had to use `LocalTime` instead of `getTime()` . But can you tell the logic behind it? – Arslan Haider Mar 10 '20 at 18:26
  • The `getTime()` method of `java.util.Date` returns the number of milliseconds since January 1, 1970, 00:00:00 GTM and `LocalTime.now()` is used to obtain the current time from the system clock in the default time-zone. – Bashir Mar 10 '20 at 19:33
  • Can `Date` and `LocalDate` be compared for equality? I'm trying but cannot do it. Can you suggest some way? Thanks – Arslan Haider Mar 12 '20 at 12:50
  • convert `Date` to `LocalDate` , and then compare them https://stackoverflow.com/questions/21242110/convert-java-util-date-to-java-time-localdate – Bashir Mar 12 '20 at 13:17