Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.domain.controller;
- import ru.domain.dao.PersonDAO;
- import ru.domain.person.Person;
- import javax.servlet.RequestDispatcher;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- import java.sql.SQLException;
- @WebServlet(name = "PersonController", urlPatterns = {"/PersonController"})
- public class PersonController extends HttpServlet {
- private static final long serialVersionUID = 1L;
- private PersonDAO persondao;
- public PersonController() {
- super();
- persondao = new PersonDAO();
- }
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String insert_or_edit = "/Person.jsp";
- String list_person = "/ListPerson.jsp";
- String forward;
- String action = request.getParameter("action");
- if (action.equalsIgnoreCase("delete")) {
- // the other page is sending the person id, so we can get here and
- // call the remove method
- int personid = Integer.parseInt(request.getParameter("personId"));
- // we remove the person from the database
- persondao.removePerson(personid);
- // set the forward string to list and put all persons in request
- // attribute so we can use them inside the other page
- forward = list_person;
- try {
- request.setAttribute("persons", persondao.getPersons());
- } catch (SQLException e) {
- e.printStackTrace();
- }
- // if the user is trying to edit a person
- } else if (action.equalsIgnoreCase("edit")) {
- forward = insert_or_edit;
- int personid = Integer.parseInt(request.getParameter("personId"));
- try {
- Person person = persondao.getPersonById(personid);
- request.setAttribute("person", person);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- } else if (action.equalsIgnoreCase("listPerson")) {
- forward = list_person;
- try {
- request.setAttribute("persons", persondao.getPersons());
- } catch (SQLException e) {
- e.printStackTrace();
- }
- } else
- forward = insert_or_edit;
- RequestDispatcher view = request.getRequestDispatcher(forward);
- view.forward(request, response);
- }
- protected void doPost(HttpServletRequest request,
- HttpServletResponse response)
- throws IOException {
- Person person = new Person();
- person.setName(request.getParameter("name"));
- person.setPhone(request.getParameter("phone"));
- person.setProfession(request.getParameter("profession"));
- String personid = request.getParameter("personId");
- System.out.println("ola");
- System.out.println(personid);
- // what i'm trying to mean here is: if the personid coming from the
- // request is null
- // then, the user is trying to add someone, otherwise, he's trying to
- // update someone
- if (personid == null || personid.isEmpty()) {
- persondao.addPerson(person);
- } else {
- person.setPersonId(Integer.parseInt(personid));
- persondao.updatePerson(person);
- }
- response.sendRedirect(request.getContextPath() + "/index.jsp");
- }
- }
- package ru.domain.dao;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- class ConnectionClass {
- Connection getConnection() throws SQLException {
- try {
- DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
- } catch (Exception e) {
- e.printStackTrace();
- }
- String url = "jdbc:mysql://localhost:3306/sakila" +
- "?verifyServerCertificate=false" +
- "&useSSL=false" +
- "&requireSSL=false" +
- "&useLegacyDatetimeCode=false" +
- "&" +
- "&serverTimezone=UTC";
- return DriverManager.getConnection(
- url,
- "root",
- "root");
- }
- }
- package ru.domain.dao;
- import ru.domain.person.Person;
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- public class PersonDAO {
- private Connection connection;
- public PersonDAO() {
- ConnectionClass con = new ConnectionClass();
- try {
- connection = con.getConnection();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- ArrayList<Person> persons;
- try {
- persons = getPersons();
- System.out.println();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void addPerson(Person person) {
- String query = "insert into person(name, phone, profession) "
- + " values('" + person.getName() + "', '" + person.getPhone()
- + "', '" + person.getProfession() + "')";
- executeQuery(query);
- }
- private void executeQuery(String query) {
- try {
- Statement stmt = connection.createStatement();
- stmt.executeUpdate(query);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void removePerson(int personid) {
- String query = "delete from person where person.idperson = " + personid + " ";
- executeQuery(query);
- }
- public void updatePerson(Person person) {
- String query = "update person set person.name='"
- + person.getName() + "', person.phone='"
- + person.getPhone()
- + "', person.profession='" + person.getProfession()
- + "' where person.idperson = " + person.getPersonId() + " ";
- executeQuery(query);
- }
- public ArrayList<Person> getPersons() throws SQLException {
- String query = "select * from person";
- ArrayList<Person> persons = new ArrayList<>();
- Statement stmt = connection.createStatement();
- ResultSet res = stmt.executeQuery(query);
- while (res.next()) {
- Person person = new Person();
- person.setName(res.getString("name"));
- person.setPhone(res.getString("phone"));
- person.setProfession(res.getString("profession"));
- person.setPersonId(res.getInt("idperson"));
- persons.add(person);
- }
- return persons;
- }
- public Person getPersonById(int personid) throws SQLException {
- Person person = new Person();
- String query = "select * from person where person.idperson = " + personid + " ";
- Statement stmt = connection.createStatement();
- ResultSet res = stmt.executeQuery(query);
- if (res.next()) {
- person.setName(res.getString("name"));
- person.setPhone(res.getString("phone"));
- person.setProfession(res.getString("profession"));
- person.setPersonId(res.getInt("idperson"));
- }
- return person;
- }
- }
- package ru.domain.person;
- public class Person {
- public String name;
- public String phone;
- public String profession;
- public int personId;
- public int getPersonId() { return personId; }
- public void setPersonId(int personId) {
- this.personId = personId;
- }
- public String getName() { return name; }
- public void setName(String name) { this.name = name; }
- public String getPhone() { return phone; }
- public void setPhone(String phone) { this.phone = phone; }
- public String getProfession() { return profession; }
- public void setProfession(String profession) { this.profession = profession; }
- }
- <!DOCTYPE web-app PUBLIC
- "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
- "http://java.sun.com/dtd/web-app_2_3.dtd" >
- <web-app>
- <display-name>Archetype Created Web Application</display-name>
- </web-app>
- <%--<%@ page contentType="text/html;charset=UTF-8" language="java" %>--%>
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- </head>
- <body>
- <jsp:forward page="/PersonController?action=listperson"/>
- </body>
- </html>
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8" %>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta content="width=device-width, initial-scale=1, maximum-scale=1" name="viewport">
- <title>Test page - ListPerson</title>
- <link href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
- <link href="http://netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">
- <link href="bootstrap.css" rel="stylesheet">
- </head>
- <body>
- <div class="wrap">
- <section>
- <div class="container">
- <table class="table table-hover">
- <thead>
- <tr>
- <th>#</th>
- <th>Name</th>
- <th>Phone Number</th>
- <th>Profession</th>
- </tr>
- </thead>
- <tbody>
- <c:forEach items="${persons}" var="person">
- <tr>
- <td><c:out value="${person.personId}"/></td>
- <td><c:out value="${person.name}"/></td>
- <td><c:out value="${person.phone}"/></td>
- <td><c:out value="${person.profession}"/></td>
- <td>
- <a href="PersonController?action=edit&personId=<c:out value="${person.personId}"/>">Update</a>
- </td>
- <td>
- <a href="PersonController?action=delete&personId=<c:out value="${person.personId}"/>">Delete</a>
- </td>
- </tr>
- </c:forEach>
- </tbody>
- </table>
- <a href="PersonController?action=insert" role="button" class="btn btn-info btn-lg" data-toggle="modal">
- Add new person</a>
- </div>
- </section>
- </div>
- <script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
- <script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
- </body>
- </html>
- <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/xsd/maven-4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>ru.domain</groupId>
- <artifactId>small_crud3</artifactId>
- <version>1.0-SNAPSHOT</version>
- <packaging>war</packaging>
- <name>small_crud3 Maven Webapp</name>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <!--<maven.compiler.source>1.7</maven.compiler.source>-->
- <!--<maven.compiler.target>1.7</maven.compiler.target>-->
- </properties>
- <dependencies>
- <!-- https://mvnrepository.com/artifact/jstl/jstl -->
- <dependency>
- <groupId>jstl</groupId>
- <artifactId>jstl</artifactId>
- <version>1.2</version>
- </dependency>
- <!--https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.11-->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>8.0.11</version>
- </dependency>
- <dependency>
- <groupId>javax</groupId>
- <artifactId>javaee-api</artifactId>
- <version>6.0</version>
- <scope>provided</scope>
- </dependency>
- </dependencies>
- <build>
- <finalName>small_crud3</finalName>
- <!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
- <plugins>
- <!--<plugin>-->
- <!--<artifactId>maven-clean-plugin</artifactId>-->
- <!--<version>3.0.0</version>-->
- <!--</plugin>-->
- <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
- <!--<plugin>-->
- <!--<artifactId>maven-resources-plugin</artifactId>-->
- <!--<version>3.0.2</version>-->
- <!--</plugin>-->
- <plugin>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>3.6.0</version>
- <configuration>
- <source>1.8</source>
- <target>1.8</target>
- </configuration>
- </plugin>
- <!--<plugin>-->
- <!--<artifactId>maven-surefire-plugin</artifactId>-->
- <!--<version>2.20.1</version>-->
- <!--</plugin>-->
- <plugin>
- <artifactId>maven-war-plugin</artifactId>
- <version>3.2.0</version>
- </plugin>
- <!--<plugin>-->
- <!--<artifactId>maven-install-plugin</artifactId>-->
- <!--<version>2.5.2</version>-->
- <!--</plugin>-->
- <!--<plugin>-->
- <!--<artifactId>maven-deploy-plugin</artifactId>-->
- <!--<version>2.8.2</version>-->
- <!--</plugin>-->
- <plugin>
- <groupId>org.apache.tomcat.maven</groupId>
- <artifactId>tomcat7-maven-plugin</artifactId>
- <version>2.2</version>
- <executions>
- <execution>
- <id>start-tomcat</id>
- <phase>pre-integration-test</phase>
- <goals>
- <goal>run</goal>
- </goals>
- </execution>
- <execution>
- <id>stop-tomcat</id>
- <phase>post-integration-test</phase>
- <goals>
- <goal>shutdown</goal>
- </goals>
- </execution>
- </executions>
- <configuration>
- <port>8080</port>
- <path>/</path>
- <fork>true</fork>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>exec-maven-plugin</artifactId>
- <version>1.5.0</version>
- <executions>
- <execution>
- <phase>integration-test</phase>
- <goals>
- <goal>exec</goal>
- </goals>
- </execution>
- </executions>
- <configuration>
- <executable>run_in_browser.bat</executable>
- </configuration>
- </plugin>
- </plugins>
- </build>
- </project>
- @echo off
- start /d "C:Program Files (x86)GoogleChromeApplication" chrome.exe -incognito http://localhost:8080
- pause
- "C:Program FilesJavajdk-10.0.1binjava.exe" -Dmaven.multiModuleProjectDirectory=C:espgcodingIntellijIdeaProjectssmall_crud3 -Dmaven.home=C:UsersespgAppDataLocalJetBrainsToolboxappsIDEA-Uch-0181.4892.42pluginsmavenlibmaven3 -Dclassworlds.conf=C:UsersespgAppDataLocalJetBrainsToolboxappsIDEA-Uch-0181.4892.42pluginsmavenlibmaven3binm2.conf -javaagent:C:UsersespgAppDataLocalJetBrainsToolboxappsIDEA-Uch-0181.4892.42libidea_rt.jar=63920:C:UsersespgAppDataLocalJetBrainsToolboxappsIDEA-Uch-0181.4892.42bin -Dfile.encoding=UTF-8 -classpath C:UsersespgAppDataLocalJetBrainsToolboxappsIDEA-Uch-0181.4892.42pluginsmavenlibmaven3bootplexus-classworlds-2.5.2.jar org.codehaus.classworlds.Launcher -Didea.version=2018.1.3 verify
- [INFO] Scanning for projects...
- [INFO]
- [INFO] ------------------------------------------------------------------------
- [INFO] Building small_crud3 Maven Webapp 1.0-SNAPSHOT
- [INFO] ------------------------------------------------------------------------
- [INFO]
- [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ small_crud3 ---
- [INFO] Using 'UTF-8' encoding to copy filtered resources.
- [INFO] skip non existing resourceDirectory C:espgcodingIntellijIdeaProjectssmall_crud3srcmainresources
- [INFO]
- [INFO] --- maven-compiler-plugin:3.6.0:compile (default-compile) @ small_crud3 ---
- [INFO] Changes detected - recompiling the module!
- [INFO] Compiling 4 source files to C:espgcodingIntellijIdeaProjectssmall_crud3targetclasses
- [INFO]
- [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ small_crud3 ---
- [INFO] Using 'UTF-8' encoding to copy filtered resources.
- [INFO] skip non existing resourceDirectory C:espgcodingIntellijIdeaProjectssmall_crud3srctestresources
- [INFO]
- [INFO] --- maven-compiler-plugin:3.6.0:testCompile (default-testCompile) @ small_crud3 ---
- [INFO] No sources to compile
- [INFO]
- [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ small_crud3 ---
- [INFO] No tests to run.
- [INFO]
- [INFO] --- maven-war-plugin:3.2.0:war (default-war) @ small_crud3 ---
- [INFO] Packaging webapp
- [INFO] Assembling webapp [small_crud3] in [C:espgcodingIntellijIdeaProjectssmall_crud3targetsmall_crud3]
- [INFO] Processing war project
- [INFO] Copying webapp resources [C:espgcodingIntellijIdeaProjectssmall_crud3srcmainwebapp]
- [INFO] Webapp assembled in [437 msecs]
- [INFO] Building war: C:espgcodingIntellijIdeaProjectssmall_crud3targetsmall_crud3.war
- [INFO]
- [INFO] >>> tomcat7-maven-plugin:2.2:run (start-tomcat) > process-classes @ small_crud3 >>>
- [INFO]
- [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ small_crud3 ---
- [INFO] Using 'UTF-8' encoding to copy filtered resources.
- [INFO] skip non existing resourceDirectory C:espgcodingIntellijIdeaProjectssmall_crud3srcmainresources
- [INFO]
- [INFO] --- maven-compiler-plugin:3.6.0:compile (default-compile) @ small_crud3 ---
- [INFO] Nothing to compile - all classes are up to date
- [INFO]
- [INFO] <<< tomcat7-maven-plugin:2.2:run (start-tomcat) < process-classes @ small_crud3 <<<
- [INFO]
- [INFO] --- tomcat7-maven-plugin:2.2:run (start-tomcat) @ small_crud3 ---
- [INFO] Running war on http://localhost:8080/
- [INFO] Creating Tomcat server configuration at C:espgcodingIntellijIdeaProjectssmall_crud3targettomcat
- [INFO] create webapp with contextPath:
- мая 13, 2018 12:45:34 ПП org.apache.coyote.AbstractProtocol init
- INFO: Initializing ProtocolHandler ["http-bio-8080"]
- мая 13, 2018 12:45:34 ПП org.apache.catalina.core.StandardService startInternal
- INFO: Starting service Tomcat
- мая 13, 2018 12:45:34 ПП org.apache.catalina.core.StandardEngine startInternal
- INFO: Starting Servlet Engine: Apache Tomcat/7.0.47
- мая 13, 2018 12:45:41 ПП org.apache.catalina.util.SessionIdGenerator createSecureRandom
- INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [410] milliseconds.
- [INFO]
- [INFO] --- exec-maven-plugin:1.5.0:exec (default) @ small_crud3 ---
- мая 13, 2018 12:45:41 ПП org.apache.coyote.AbstractProtocol start
- INFO: Starting ProtocolHandler ["http-bio-8080"]
- ��� �த������� ������ ���� ������� . . .
- мая 13, 2018 12:45:54 ПП org.apache.jasper.compiler.TldLocationsCache tldScanJar
- INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
- мая 13, 2018 12:46:03 ПП org.apache.coyote.AbstractProtocol pause
- INFO: Pausing ProtocolHandler ["http-bio-8080"]
- [INFO]
- [INFO] --- tomcat7-maven-plugin:2.2:shutdown (stop-tomcat) @ small_crud3 ---
- мая 13, 2018 12:46:03 ПП org.apache.catalina.core.StandardService stopInternal
- INFO: Stopping service Tomcat
- мая 13, 2018 12:46:03 ПП org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc
- SEVERE: The web application [] registered the JDBC driver [com.mysql.cj.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
- мая 13, 2018 12:46:04 ПП org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc
- SEVERE: The web application [] registered the JDBC driver [com.mysql.cj.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
- мая 13, 2018 12:46:04 ПП org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
- SEVERE: The web application [] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak.
- WARNING: An illegal reflective access operation has occurred
- WARNING: Illegal reflective access by org.apache.catalina.loader.WebappClassLoader (file:/C:/Users/espg/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/7.0.47/tomcat-embed-core-7.0.47.jar) to field java.lang.Thread.threadLocals
- WARNING: Please consider reporting this to the maintainers of org.apache.catalina.loader.WebappClassLoader
- WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
- WARNING: All illegal access operations will be denied in a future release
- мая 13, 2018 12:46:04 ПП org.apache.coyote.AbstractProtocol stop
- INFO: Stopping ProtocolHandler ["http-bio-8080"]
- мая 13, 2018 12:46:08 ПП org.apache.catalina.loader.WebappClassLoader findResourceInternal
- INFO: Illegal access: this web application instance has been stopped already. Could not load . The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
- мая 13, 2018 12:46:09 ПП org.apache.tomcat.util.net.AbstractEndpoint shutdownExecutor
- [INFO] ------------------------------------------------------------------------
- [INFO] BUILD SUCCESS
- [INFO] ------------------------------------------------------------------------
- [INFO] Total time: 59.169 s
- [INFO] Finished at: 2018-05-13T12:46:09+03:00
- WARNING: The executor associated with thread pool [http-bio-8080] has not fully shutdown. Some application threads may still be running.
- [INFO] Final Memory: 30M/94M
- [INFO] ------------------------------------------------------------------------
- мая 13, 2018 12:46:09 ПП org.apache.catalina.loader.WebappClassLoader findResourceInternal
- INFO: Illegal access: this web application instance has been stopped already. Could not load . The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
- Process finished with exit code 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement