R
R
random2015-01-03 21:23:59
Java
random, 2015-01-03 21:23:59

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


Where Phonebook.java is a servlet, looks like this
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();
        }
    }
}


And this is what pom.xml looks like
<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>


After doing: mvn package
Then: mvn tomcat:run localhost:8080/phonebook

But it displays index.jsp for me, how can I make it output my serlet?
And where else should I put the JDBC PostgreSQL library (postgresql-9.3-1102.jdbc41.jar)? Or does it download itself?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolai Pavlov, 2015-01-03
@demon123

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 question

Ask a Question

731 491 924 answers to any question