daily pastebin goal
15%
SHARE
TWEET

Untitled

a guest Apr 21st, 2018 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Exception
  2.  
  3. javax.servlet.ServletException: Servlet execution threw an exception
  4.     org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  5. Root Cause
  6.  
  7. java.lang.NoClassDefFoundError: org/hibernate/service/ServiceRegistry
  8.     net.chemodurov.productfromthemanufacturer.dao.HibernateUserDAOImpl.validate(HibernateUserDAOImpl.java:59)
  9.     net.chemodurov.productfromthemanufacturer.controller.LoginServlet.doGet(LoginServlet.java:24)
  10.     net.chemodurov.productfromthemanufacturer.controller.LoginServlet.doPost(LoginServlet.java:13)
  11.     javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
  12.     javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
  13.     org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  14. Root Cause
  15.  
  16. java.lang.ClassNotFoundException: org.hibernate.service.ServiceRegistry
  17.     org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1308)
  18.     org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1136)
  19.     net.chemodurov.productfromthemanufacturer.dao.HibernateUserDAOImpl.validate(HibernateUserDAOImpl.java:59)
  20.     net.chemodurov.productfromthemanufacturer.controller.LoginServlet.doGet(LoginServlet.java:24)
  21.     net.chemodurov.productfromthemanufacturer.controller.LoginServlet.doPost(LoginServlet.java:13)
  22.     javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
  23.     javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
  24.     org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  25. Note The full stack trace of the root cause is available in the server logs.
  26.    
  27. <%@ page contentType="text/html;charset=UTF-8"%>
  28. <html>
  29. <head>
  30.     <title>Login page</title>
  31. </head>
  32. <body>
  33.         <form action="${pageContext.request.contextPath}/login"method="POST">
  34.             <input type="text" placeholder="name" name = "name"/>
  35.             <input type="password" placeholder="password" name="password"/>
  36.             <button>login</button>
  37.             <p class="message">Not registered? <a href="registration.jsp">Create an account</a></p>
  38.         </form>
  39. </body>
  40. </html>
  41.    
  42. import javax.servlet.RequestDispatcher;
  43. import java.io.IOException;
  44. import java.io.PrintWriter;
  45.  
  46. public class LoginServlet extends javax.servlet.http.HttpServlet {
  47.     protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
  48.         doGet(request, response);
  49. }
  50.  
  51.     protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
  52.         UserDAO userDAO = new HibernateUserDAOImpl();
  53.         response.setContentType("text/html;charset=UTF-8");
  54.         PrintWriter out = response.getWriter();
  55.  
  56.         String username = request.getParameter("name");
  57.         String password = request.getParameter("password");
  58.  
  59.         if (userDAO.validate(username, password)) {
  60.             RequestDispatcher rs = request.getRequestDispatcher("/manufacturers");
  61.             rs.forward(request, response);
  62.         }
  63.         else {
  64.             out.println("Access denied!");
  65.             RequestDispatcher rs =     request.getRequestDispatcher("index.jsp");
  66.             rs.include(request, response);
  67.         }
  68.     }
  69. }
  70.    
  71. import javax.persistence.*;
  72.  
  73. @Entity
  74. @Table(name = "users")
  75. public class User {
  76.     private long id;
  77.     private String name;
  78.     private String password;
  79.  
  80.     @Id
  81.     @GeneratedValue(strategy = GenerationType.IDENTITY)
  82.     @Column(name = "id")
  83.     public long getId() {
  84.         return id;
  85.     }
  86.  
  87.     public void setId(long id) {
  88.         this.id = id;
  89.     }
  90.  
  91.     @Column(name = "name")
  92.     public String getName() {
  93.         return name;
  94.     }
  95.  
  96.     public void setName(String name) {
  97.         this.name = name;
  98.     }
  99.  
  100.     @Column(name = "password")
  101.     public String getPassword() {
  102.         return password;
  103.     }
  104.  
  105.     public void setPassword(String password) {
  106.         this.password = password;
  107.     }
  108. }
  109.    
  110. import org.hibernate.Session;
  111. import org.hibernate.query.Query;
  112.  
  113. import java.util.HashSet;
  114. import java.util.Set;
  115.  
  116. public class HibernateUserDAOImpl implements UserDAO {
  117.  
  118.     @Override
  119.     public void add(User entity) {
  120.         Session session = HibernateSessionFactory.getSessionFactory().openSession();
  121.         session.save(entity);
  122.         session.close();
  123.     }
  124.  
  125.     @Override
  126.     public User getById(Long id) {
  127.         Session session = HibernateSessionFactory.getSessionFactory().openSession();
  128.         User product = session.get(User.class, id);
  129.         session.close();
  130.         return product;
  131.     }
  132.  
  133.     @Override
  134.     public void update(User entity) {
  135.         Session session = HibernateSessionFactory.getSessionFactory().openSession();
  136.         session.beginTransaction();
  137.         session.update(entity);
  138.         session.flush();
  139.         session.close();
  140.     }
  141.  
  142.     @Override
  143.     public void delete(Long id) {
  144.         Session session = HibernateSessionFactory.getSessionFactory().openSession();
  145.         session.beginTransaction();
  146.         User product = session.get(User.class, id);
  147.         session.delete(product);
  148.         session.flush();
  149.         session.close();
  150.     }
  151.  
  152.     @Override
  153.     public Set<User> getAll() {
  154.         Set<User> products;
  155.         Session session = HibernateSessionFactory.getSessionFactory().openSession();
  156.         products = new HashSet<User>(session.createQuery("FROM User").list());
  157.         session.close();
  158.         return products;
  159.     }
  160.  
  161.     @Override
  162.     public boolean validate(String name, String password) {
  163.         Session session = HibernateSessionFactory.getSessionFactory().openSession();
  164.         Query query = session.createQuery("from User where name=:username and password=:password");
  165.         query.setParameter("username", name);
  166.         query.setParameter("password", password);
  167.         boolean result = !query.list().isEmpty();
  168.         session.close();
  169.         return result;
  170.     }
  171. }
  172.    
  173. import org.hibernate.SessionFactory;
  174. import org.hibernate.boot.Metadata;
  175. import org.hibernate.boot.MetadataSources;
  176. import org.hibernate.boot.registry.StandardServiceRegistry;
  177. import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
  178.  
  179. public class HibernateSessionFactory {
  180.     private static StandardServiceRegistry registry;
  181.     private static SessionFactory sessionFactory;
  182.  
  183.     public static SessionFactory getSessionFactory() {
  184.         if (sessionFactory == null) {
  185.             try {
  186.                 // Create registry
  187.                 registry = new StandardServiceRegistryBuilder()
  188.                         .configure()
  189.                         .build();
  190.  
  191.                 // Create MetadataSources
  192.                 MetadataSources sources = new MetadataSources(registry);
  193.  
  194.                 // Create Metadata
  195.                 Metadata metadata = sources.getMetadataBuilder().build();
  196.  
  197.                 // Create SessionFactory
  198.                 sessionFactory = metadata.getSessionFactoryBuilder().build();
  199.  
  200.             } catch (Exception e) {
  201.                 e.printStackTrace();
  202.                 if (registry != null) {
  203.                     StandardServiceRegistryBuilder.destroy(registry);
  204.                 }
  205.             }
  206.         }
  207.         return sessionFactory;
  208.     }
  209.  
  210.     public static void shutdown() {
  211.         if (registry != null) {
  212.             StandardServiceRegistryBuilder.destroy(registry);
  213.         }
  214.     }
  215.    
  216. <?xml version="1.0" encoding="UTF-8"?>
  217. <project xmlns="http://maven.apache.org/POM/4.0.0"
  218.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  219.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  220.     <modelVersion>4.0.0</modelVersion>
  221.  
  222.     <groupId>ProductsFromTheManufacturer</groupId>
  223.     <artifactId>net.chemodurov. productsfromthemanufacturer</artifactId>
  224.     <version>1.0-SNAPSHOT</version>
  225.     <dependencies>
  226.  
  227.         <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
  228.         <dependency>
  229.             <groupId>mysql</groupId>
  230.             <artifactId>mysql-connector-java</artifactId>
  231.             <version>5.1.6</version>
  232.         </dependency>
  233.  
  234.         <dependency>
  235.             <groupId>org.hibernate</groupId>
  236.             <artifactId>hibernate-core</artifactId>
  237.             <version>5.2.16.Final</version>
  238.         </dependency>
  239.  
  240.         <dependency>
  241.             <groupId>javax.xml.bind</groupId>
  242.             <artifactId>jaxb-api</artifactId>
  243.             <version>2.2.11</version>
  244.         </dependency>
  245.  
  246.         <dependency>
  247.             <groupId>com.sun.xml.bind</groupId>
  248.             <artifactId>jaxb-core</artifactId>
  249.             <version>2.2.11</version>
  250.         </dependency>
  251.  
  252.         <dependency>
  253.             <groupId>com.sun.xml.bind</groupId>
  254.             <artifactId>jaxb-impl</artifactId>
  255.             <version>2.2.11</version>
  256.         </dependency>
  257.         <!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
  258.         <dependency>
  259.             <groupId>javax.servlet</groupId>
  260.             <artifactId>servlet-api</artifactId>
  261.             <version>2.5</version>
  262.             <scope>provided</scope>
  263.         </dependency>
  264.  
  265.  
  266.  
  267.     </dependencies>
  268.  
  269.  
  270.     <build>
  271.         <finalName>ProductManufacturerSystem</finalName>
  272.         <plugins>
  273.             <plugin>
  274.                 <groupId>org.apache.tomcat.maven</groupId>
  275.                 <artifactId>tomcat7-maven-plugin</artifactId>
  276.                 <version>2.2</version>
  277.                 <configuration>
  278.                     <port>8080</port>
  279.                     <path>/</path>
  280.                 </configuration>
  281.             </plugin>
  282.             <plugin>
  283.                 <groupId>org.apache.maven.plugins</groupId>
  284.                 <artifactId>maven-compiler-plugin</artifactId>
  285.                 <configuration>
  286.                     <source>1.8</source>
  287.                     <target>1.8</target>
  288.                 </configuration>
  289.             </plugin>
  290.         </plugins>
  291.     </build>
  292.  
  293.  
  294. </project>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top