Answer the question
In order to leave comments, you need to log in
Can't deal with maven?
Created a project: maven-archetype-webapp
With this directory
├── pom.xml
└── src
└── main
├── java
└── com
└── myphonebook
└── Phonebook.java
├── resources
└── webapp
├── index.jsp
└── WEB-INF
└── web.xml
package com.myphonebook;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Samsung
*/
@WebServlet(urlPatterns = {"/"})
public class Phonebook extends HttpServlet {
private Connection c = null;
private String sql = null;
private Statement stmt = null;
public void init(ServletConfig conf) {
try {
// Регистрируем JDBC драйвер
Class.forName("org.postgresql.Driver");
// Соединение с базой данных
c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres", "123456");
stmt = c.createStatement();
// Создаем таблицу в базе
sql = "CREATE TABLE PHONEBOOK "
+ "(ID INT PRIMARY KEY NOT NULL,"
+ " first_name VARCHAR(20) NOT NULL, "
+ " last_name VARCHAR(20) NOT NULL, "
+ " PHONE VARCHAR(20) NOT NULL, "
+ " EMAIL VARCHAR(50))";
stmt.executeUpdate(sql);
// Задаем значения для нашей таблицы
sql = "INSERT INTO PHONEBOOK ";
stmt.executeUpdate(sql);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=windows-1251");
PrintWriter out = response.getWriter();
try {
// Вывод данных из базы в HTML таблицу
sql = "SELECT * FROM PHONEBOOK";
ResultSet rs = stmt.executeQuery(sql);
out.println("<html>");
out.println("<head><style>table,th,td{border:1px solid black;border-collapse:collapse;}th,td{padding:5px;text-align:left;}</style>");
out.println("<title>phonebook</title></head>");
out.println("<body><h1>Phonebook</h1><table>");
out.println("<tr><th>ID</th><th>First name</th><th>Last name</th><th>PHONE</th><th>EMAIL</th></tr>");
while (rs.next()) {
out.println(
"<td>" + rs.getString(1) + "</td>"
+ "<td>" + rs.getString(2) + "</td>"
+ "<td>" + rs.getString(3) + "</td>"
+ "<td>" + rs.getString(4) + "</td>"
+ "<td>" + rs.getString(5) + "</td></tr>"
);
}
out.println("</table></body>");
out.println("</html>");
rs.close();
} catch (Exception ex) {
out.println("<h1>Error: </h1>" + ex.getClass().getName() + ": " + ex.getMessage());
} finally {
out.close();
}
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>alisher</groupId>
<artifactId>phonebook</artifactId>
<packaging>war</packaging>
<version>1</version>
<name>phonebook Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.3-1102-jdbc41</version>
</dependency>
</dependencies>
<build>
<finalName>phonebook</finalName>
</build>
</project>
Answer the question
In order to leave comments, you need to log in
add index.jsp
to web.xml
And map your servlet to index.jsp
Yes, postgres will be downloaded from mvn central
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question