Question

I have a problem with my school project. Im using hibernate,jsp and maven for dependency management. Hibernate and maven works fine but when I try to make a simple jsp page with login screen. Its not working. I think problem is with maven and how he works with project structure, because if I make test class everything working. Im not able to find anything about how maven works with webapps.

Sorry for my mistakes in english :).

my project structure:

├── src
│   └── main
│       ├── java
│       │    │
│       │    pro3
│       │     ├── model
│       │     │   ├── dao
│       │     │   │   ├── DaoInterface.java
│       │     │   │   └── HibernateDao.java
│       │     │   ├── hibernate
│       │     │   │   ├── hibernate.cfg.xml
│       │     │   │   ├── hibernate.reveng.xml
│       │     │   │   ├── HibernateUtil.java
│       │     │   │   ├── mapping
│       │     │   │   │   ├── Meal.hbm.xml
│       │     │   │   │   ├── ...
│       │     │   │   │   
│       │     │   │   └── Test.java
│       │     │   └── pojo
│       │     │       ├── Meal.java
│       │     │       ├── ...
│       │     │      
│       │     │      
│       │     └── utils
│       │         └── LoginHandler.java
│       └── webapp
│           └── index.jsp

Index.jsp:

<jsp:useBean id="login" scope="page" class="cz.fim.pro3.utils.LoginHandler" />
...
login.verifyUser(request.getParameter("userName"), request.getParameter("password"));

LoginHandler.java:

public class LoginHandler {
    DaoInterface dao = new HibernateDao();


    public boolean verifyUser(String userName,String password){
        return dao.verifyUser(userName, password);
    }
}

HibernateDao.java:

public boolean verifyUser(String userName, String password)


In my test class everything works fine, when I run it as java application.

Test:

public static void main(String[] args) {
    new Test();
    LoginHandler login = new LoginHandler();
    login.verifyUser("sdf", "asda");
}

pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                    </manifest>
                </archive>
                <webResources>
                    <resource>
                        <!-- this is relative to the pom.xml directory -->
                        <directory>src/main/webapp</directory>
                        <targetPath>/</targetPath>
                        <!-- enable filtering -->
                        <filtering>false</filtering>
                    </resource>
                </webResources>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.7</source>
                <target> 1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>

<packaging>war</packaging>

error trace:

Stacktrace:] with root cause
java.lang.NoSuchMethodError: org.hibernate.SessionFactory.openSession()Lorg/hibernate/Session;
    at cz.fim.pro3.model.dao.HibernateDao.verifyUser(HibernateDao.java:11)
    at cz.fim.pro3.utils.LoginHandler.verifyUser(LoginHandler.java:11)
    at org.apache.jsp.index_jsp._jspService(index_jsp.java:83)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
Was it helpful?

Solution

The first thing i can recommend is to change the locations of your files, cause in Maven you have to follow some conventions:

The Java files should be put into src/main/java plus the appropriate package name. The hibernate.cfg.xml file and the hibernate.reveng.xml do NOT belong to src/main/java they have to be located into src/main/resources plus the appropriate package name you like to put them into. Furthermore in hibernate the mapping should be handled by annotations and not by hbm files anymore. But if you have to stuck with the mappings files they have to be put into src/main/resources as well.

If you have some kind of tests (usually unit tests) like Test.java they have to be put into src/test/java any kind of resources which belong to the unit tests only have to be put into src/test/resources plus the appropriate package name. The jsp file have been located correctly into src/main/webapp.

Apart from the above you have to call the correct method in your HibernateDao.java line 11 where you are trying to call a method openSession which obviously does not exists.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top