0

I have a project on

https://github.com/CarefreeCoding/Gradle-Spring-Hibernate-example

I would like to be able to get visitor's ip address. To do that I need to use HttpServletRequest as I have found out through searching.

However javax.servlet library does not seem to be found.

I have tried including 'javax.servlet:servlet-api:2.5' in my build.gradle script, however that breaks my project as explain in this question.

So the question is. How can I use only libraries from

    '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'

to obtain ip address of the visitor? Or to at least access HttpServletRequest somehow.

Community
  • 1
  • 1
Quillion
  • 6,346
  • 11
  • 60
  • 97

2 Answers2

1

Modify build.gradle with in the following way:

providedCompile 'javax.servlet:javax.servlet-api:3.1.0' //configuration taken from war plugin

//below dependencies added because the app wasn't starting properly
compile 'ch.qos.logback:logback-classic:1.0.13'
compile 'ch.qos.logback:logback-core:1.0.13'
compile 'log4j:log4j:1.2.17'
compile 'org.slf4j:slf4j-api:1.7.5'
compile 'org.slf4j:jcl-over-slf4j:1.7.5'
compile 'org.slf4j:jul-to-slf4j:1.7.5'
compile 'org.slf4j:slf4j-log4j12:1.7.5'

then modify src/main/java/example/controllers/IndexController.java:

package example.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/")
public class IndexController {
    @RequestMapping(method = RequestMethod.GET)
    public String printWelcome(ModelMap model, HttpServletRequest request) {
        System.out.println("Request " + request.getRemoteAddr());
        model.addAttribute("message", "Hello World!!!");
        return "indexPage";
    }
}

It prints Request 0:0:0:0:0:0:0:1

Opal
  • 81,889
  • 28
  • 189
  • 210
0

As stated in the other question, you already have servlet-api in the project, no need to add javax.servlet:servlet-api:2.5 to your project definition.

Therefore, you shall be able to access HttpServletRequest.

However, as mentioned in the other question, you have version 3.0.1 of servlet-api, which does not include the getRemoteAddr() method, if that is what you wanted to use.

Community
  • 1
  • 1
Michal
  • 2,353
  • 1
  • 15
  • 18
  • So what would you recommend I use to call `getRemoteAddr()`? – Quillion Dec 06 '14 at 23:14
  • If you can afford to choose your container, then the other answer is correct, you ugrade servlet api to 3.1, use getRemoteAddr, but have to make sure your container supports 3.1. Otherwise you might have a look at the header - as of stackoverflow.com/questions/527638/getting-the-client-ip-address-remote-addr-http-x-forwarded-for-what-else-coul - which you can get from http request, the getRemoteAddr() impl in 3.1 cannot do much more else. – Michal Dec 06 '14 at 23:20
  • I can use anything I want since this is just for fun project I am working on for learning purposes. However what is this container you speak of? – Quillion Dec 06 '14 at 23:42
  • With container I refer to the application server, Tomcat would be an example. – Michal Dec 07 '14 at 09:26