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.
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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.