Веб приложение, Java EE. Среда NetBeans + сервер Apache Tomcat 6. Приложение работает непосредственно с базой на PostgreSQL. Компилю, стартовая страница запускается, где нужно ввести логин и пароль, которые хранятся в одной из таблиц в БД. На выходе получаю то, что на скрине. Подозреваю, что приложение не коннектится к БД, хотя Hibernate в наличии и JDBC драйвер тоже установлен. Помогите, пожалуйста, разобраться с проблемой...
Вот код класса Database:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
138.
139.
140.
141.
142.
143.
144.
145.
146.
147.
148.
149.
150.
151.
152.
153.
154.
155.
156.
157.
158.
159.
160.
161.
162.
163.
164.
165.
166.
167.
168.
169.
170.
171.
172.
173.
174.
175.
176.
177.
178.
179.
180.
181.
182.
183.
184.
185.
186.
187.
188.
189.
190.
191.
192.
193.
194.
195.
196.
197.
198.
199.
200.
201.
202.
203.
204.
205.
206.
207.
208.
209.
210.
211.
212.
213.
214.
215.
216.
217.
218.
219.
220.
221.
222.
223.
224.
225.
226.
227.
228.
229.
230.
231.
232.
233.
234.
235.
236.
237.
238.
239.
240.
241.
242.
243.
244.
245.
246.
247.
248.
249.
250.
251.
252.
253.
254.
255.
256.
257.
258.
259.
260.
261.
262.
263.
264.
package courseterm;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import tables.Commentary;
import tables.Song;
import tables.Style;
import tables.User;
import dao.HibernateUtil;
public class Database {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
private String commitStatus = "update";
private String status;
public boolean connected = false;
private String login;
private static Database instance;
public Database(String login, String password) {
connect(login, password);
}
public Database() {
}
public void connect(String login, String password)
throws ExceptionInInitializerError {
this.login = login;
Configuration config = new Configuration();
config.configure("hibernate.cfg.xml");
config.setProperty("hibernate.connection.username", login);
config.setProperty("hibernate.connection.password", password);
config.setProperty("hibernate.hbm2ddl.auto", commitStatus);
try {
serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(config.getProperties()).build();
sessionFactory = config.buildSessionFactory(serviceRegistry);
connected = true;
setStatus("Соединение успешно");
} catch (Throwable ex) {
connected = false;
setStatus("Неудалось создать объект sessionFactory:"
+ ex.getMessage());
throw new ExceptionInInitializerError(ex);
}
}
public String getLoginName() {
return login;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = "Status: " + status;
}
public void disconnect() {
if (!sessionFactory.isClosed()) {
sessionFactory.close();
login = null;
connected = false;
setStatus("Подключение отключено.");
}
if (serviceRegistry != null) {
StandardServiceRegistryBuilder.destroy(serviceRegistry);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public void addStyle(Style style) {
try {
HibernateUtil.getStyleDAO().addStyle(style);
} catch (SQLException e) {
setStatus(e.getLocalizedMessage());
e.printStackTrace();
}
}
public void addSong(Song song) {
try {
HibernateUtil.getSongDAO().addSong(song);
} catch (SQLException e) {
setStatus(e.getLocalizedMessage());
e.printStackTrace();
}
}
public static Database getInstance() {
Database localInstance = instance;
if (localInstance == null) {
synchronized (Database.class) {
localInstance = instance;
if (localInstance == null) {
instance = localInstance = new Database();
}
}
}
return localInstance;
}
public List<?> getStylesByNameEng(String stylename) {
List<?> styles = null;
try {
styles = HibernateUtil.getStyleDAO().getStyleByNameEng(stylename);
} catch (SQLException e) {
setStatus(e.getLocalizedMessage());
e.printStackTrace();
}
return styles;
}
public List<?> getStylesByNameRus(String stylename) {
List<?> styles = null;
try {
styles = HibernateUtil.getStyleDAO().getStyleByNameRus(stylename);
} catch (SQLException e) {
setStatus(e.getLocalizedMessage());
e.printStackTrace();
}
return styles;
}
public List<?> getSongs() {
List<?> songs = null;
try {
songs = HibernateUtil.getSongDAO().getSongs();
} catch (SQLException e) {
setStatus(e.getLocalizedMessage());
e.printStackTrace();
}
return songs;
}
public List<?> getStyles() {
List<?> styles = null;
try {
styles = HibernateUtil.getStyleDAO().getStyles();
} catch (SQLException e) {
setStatus(e.getLocalizedMessage());
e.printStackTrace();
}
return styles;
}
public List<?> getLastSongs() {
List<?> songs = null;
try {
songs = HibernateUtil.getSongDAO().getLastSongs();
} catch (SQLException e) {
setStatus(e.getLocalizedMessage());
e.printStackTrace();
}
return songs;
}
public List<?> getCommentsBySongId(String id) {
List<?> comments = null;
try {
comments = HibernateUtil.getCommentaryDAO().getCommentsBySongId(
Long.parseLong(id));
} catch (NumberFormatException | SQLException e) {
setStatus(e.getLocalizedMessage());
e.printStackTrace();
}
return comments;
}
public List<?> getUserByLogin() {
List<?> user = null;
try {
user = HibernateUtil.getUserDAO().getUserByLogin(
Database.getInstance().getLoginName());
} catch (SQLException e) {
setStatus(e.getLocalizedMessage());
e.printStackTrace();
}
return user;
}
public Song getSongById(String id) {
Song song = null;
try {
song = HibernateUtil.getSongDAO().getSongById(Long.parseLong(id));
} catch (NumberFormatException | SQLException e) {
setStatus(e.getLocalizedMessage());
e.printStackTrace();
}
return song;
}
public void addComment(Commentary comment) {
try {
HibernateUtil.getCommentaryDAO().addComment(comment);
} catch (SQLException e) {
setStatus(e.getLocalizedMessage());
e.printStackTrace();
}
}
public void updateUser(User user) {
try {
HibernateUtil.getUserDAO().updateUser(user);
} catch (SQLException e) {
setStatus(e.getLocalizedMessage());
e.printStackTrace();
}
}
public void deleteSong(Song song) {
try {
HibernateUtil.getCommentaryDAO().deleteCommentsBySongId(song.getId());
HibernateUtil.getSongDAO().deleteSong(song);
} catch (SQLException e) {
setStatus(e.getLocalizedMessage());
e.printStackTrace();
}
}
public Commentary getCommentById(Long id) {
Commentary comment = null;
try {
comment = HibernateUtil.getCommentaryDAO().getCommentById(id);
} catch (SQLException e) {
setStatus(e.getLocalizedMessage());
e.printStackTrace();
}
return comment;
}
public void deleteComment(Commentary comment) {
try {
HibernateUtil.getCommentaryDAO().deleteComment(comment);
} catch (SQLException e) {
setStatus(e.getLocalizedMessage());
e.printStackTrace();
}
}
public List<?> getTopUserList() {
List<?> users = null;
try {
users = HibernateUtil.getUserDAO().getTopUserList();
} catch (SQLException e) {
setStatus(e.getLocalizedMessage());
e.printStackTrace();
}
return users;
}
}
А это сервлет, отвечающий за логин:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
package servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import courseterm.Database;
public class LoginServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 7638796169158385551L;
private Database database = Database.getInstance();
private String homeUrl = "http://localhost:8080/SongInfo/";
private String css1 = "<link type="text/css" rel="stylesheet" href="http://localhost:8080/SongInfo/css/global.css"/>";
private String css2 = "<link type="text/css" rel="stylesheet" href="http://localhost:8080/SongInfo/css/login.css"/>";
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.write("<html><head></head><body>");
out.write("<div id="connected">");
if (database.connected) {
out.write("true");
} else {
out.write("false");
}
out.write("</div>");
out.write("</body></html>");
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
out.println(css1);
out.println(css2);
out.println("<div class='center'>");
String submitLogin = request.getParameter("submitLogin");
String login = request.getParameter("login");
String pass = request.getParameter("password");
String logoutsub = request.getParameter("logoutsub");
if (submitLogin != null) {
if (!submitLogin.isEmpty()) {
if (login.isEmpty() || pass.isEmpty()) {
out.write("Некоторые поля не заполнены, перенаправляю на домашнюю страницу...");
out.println("</div>");
response.setHeader("Refresh", "3; URL=" + homeUrl);
return;
}
try {
database.connect(login, pass);
out.write("Вход успешен! <br>");
out.write("Привет " + database.getLoginName() + " !<br>");
out.write("Перенаправление на главную страницу через 3 секунды...");
out.println("</div>");
response.setHeader("Refresh", "3; URL=" + homeUrl
+ "main.jsp");
} catch (ExceptionInInitializerError ex) {
out.write("Введенный логин или пароль не правильный");
out.write("<br>Перенаправление на главную страницу через 3 секунды...");
out.println("</div>");
response.setHeader("Refresh", "3; URL=" + homeUrl
+ "main.jsp");
}
}
} else if (!logoutsub.isEmpty()) {
if (database.connected)
database.disconnect();
out.write("Выход из системы успешен, перенаправление на домашнюю страницу через 3 секунды...");
out.println("</div>");
response.setHeader("Refresh", "3; URL=" + homeUrl);
}
}
}