You need to separate view from logic, meaning move that database connectivity to a class that will be called by servlet and do not do this in JSP. Check out MVC tutorial at the top of JSP section for more suitable approach