0

I have a spring project and everything worked fine.

My gradle dependencies compile has the following

compile 'org.springframework:spring-core:4.1.2.RELEASE',
        'org.springframework:spring-web:4.1.2.RELEASE',
        'org.springframework:spring-webmvc:4.1.2.RELEASE',
        'org.springframework:spring-orm:4.1.2.RELEASE',
        'org.springframework:spring-context:4.1.2.RELEASE',
        'org.springframework:spring-tx:4.1.2.RELEASE',
        'commons-dbcp:commons-dbcp:1.4',
        'mysql:mysql-connector-java:5.1.6',
        'org.hibernate:hibernate-core:4.3.7.Final'

And my web.xml is

<web-app
    id="WebApp_ID"
    version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
    http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Test Restful Application</display-name>

    <servlet>
        <servlet-name>test-servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>test-servlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/test-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <welcome-file-list>
        <welcome-file>/WEB-INF/view/default.jsp</welcome-file>
    </welcome-file-list>

    <error-page>
        <error-code>404</error-code>
        <location>/WEB-INF/view/default.jsp</location>
    </error-page>

</web-app>

And the beans are

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.example" />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
        id="dataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url"
            value="jdbc:mysql://localhost:3306/test" />
        <property name="username" value="root" />
        <property name="password" value="password" />
    </bean>

    <bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
        id="sessionFactory">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:hibernate.cfg.xml" />
    </bean>

    <tx:annotation-driven />
    <bean class="org.springframework.orm.hibernate4.HibernateTransactionManager"
        id="transactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean class="com.example.dao.TestDAO" id="testDAO">
        <constructor-arg ref="sessionFactory" />
    </bean>

</beans>

And everything work just wonderfully.

However I wanted to see if I can get ip address of visitors.

So I looked up and found out that I need to use HttpServletRequest and to use that I need javax.servlet so I modified gradle.build and added 'javax.servlet:servlet-api:2.5'. I have not modified a single line of code except build.gradle, and now I am getting HTTP Status 500 - Unable to compile class for JSP: org.apache.jasper.JasperException: Unable to compile class for JSP:

Can anyone explain why and how I got this and what cause it and how I can fix it? It is not making sense to me how adding a single library can cause everything to stop working.

Thanks to anyone for their help, I am losing my mind without being able to find a solution.

EDIT:

This is the stack trace

Stacktrace:] with root cause
 org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: [42] in the generated java file: [system\tomcat\Tomcat_(1)_web\work\Catalina\localhost\ROOT\org\apache\jsp\WEB_002dINF\view\default_jsp.java]
The method getDispatcherType() is undefined for the type HttpServletRequest

Stacktrace:
    at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
    at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:199)
    at org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:450)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:361)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:336)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:323)
    at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:564)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1081)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

Apparently it views the jsp as .java file.

EDIT2:

This is my jsp file

<html>
<head>
    <title>Error</title>
</head>
<body>
<h3>Wrong page</h3>
</body>
</html>
Quillion
  • 6,346
  • 11
  • 60
  • 97

2 Answers2

1

How to fix: revert your changes. You already have dependency on javax.servlet.

javax.servlet:javax.servlet-api:3.0.1 is a dependency of org.springframework:spring-web:4.1.2.RELEASE (look at section 'Depends On'), so it's available to you as a transitive dependency.

Why you've got this: you have two conflicting dependencies of the same jar (HttpServletRequest in Servlet API 3.0 does have getDispatcherType() method, while in API 2.5 it doesn't). From Gradle User Guide:

Conflicting versions of the same jar should be detected and either resolved or cause an exception. If you don't use transitive dependency management, version conflicts are undetected and the often accidental order of the classpath will determine what version of a dependency will win. On a large project with many developers changing dependencies, successful builds will be few and far between as the order of dependencies may directly affect whether a build succeeds or fails (or whether a bug appears or disappears in production).

Alex Filatov
  • 4,768
  • 2
  • 16
  • 10
0

"Apparently it views the jsp as .java file.".

No. It translates the JSP into Java and then compiles that. If your JSP contains errors, then it is quite likely that those errors will result in Java code that has compilation errors in it.

What you need to do is to look at the generated source code for the JSP, and figure out what the Java compilation errors are saying. Then figure out the corresponding error in your JSP, and fix the problem there.

In this case, it appears that your JSP is trying to call a non-existent method on the request object. The compiler is correct. There is no method called getDispatcherType() in the HttpServletRequest API, so your JSP code that is attempting to call it is incorrect.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • I edited the question and added my jsp file. Is there really something that I am doing that is so wrong? – Quillion Dec 04 '14 at 02:41
  • Are you sure that you have the correct JSP file? Does it bear any resemblance to the `default_jsp.java` file that the compiler is talking about? – Stephen C Dec 04 '14 at 02:45
  • Yes, my default.jsp will show error page. Which is why I mapped it to error page 404 in my web.xml. This is why I am baffled. The code I have is not really that complex at all, which is why I am baffled as to why it does not work. – Quillion Dec 04 '14 at 02:46
  • An example would be one project I have under https://github.com/CarefreeCoding/Gradle-Spring-Hibernate-example it works perfectly, the second I try to add 'javax.servlet:servlet-api:2.5' it also stops working – Quillion Dec 04 '14 at 02:50
  • Well, you shouldn't normally add the servlet-api JAR file to runtime classpath of a servlet. The web container should already have all of those APIs on the classpath ... and it will use a version that is most appropriate to the web container. – Stephen C Dec 04 '14 at 03:13
  • So how would you suggest I go about obtaining the ip address of the visitor without having any linking to the javax.servlet? – Quillion Dec 04 '14 at 03:14
  • The problem is getting Gradle to NOT include the servlet-api JAR file in the WAR file it generates. I'm not a Gradle user. – Stephen C Dec 04 '14 at 03:19