minor formatting cleanup
This commit is contained in:
parent
33f7b1a3b9
commit
a4c02d6275
@ -8,24 +8,14 @@ import javax.servlet.http.HttpServletRequest;
|
|||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
public class ExampleOne extends HttpServlet {
|
public class ExampleOne extends HttpServlet {
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||||
response.setContentType("text/html");
|
response.setContentType("text/html");
|
||||||
PrintWriter out = response.getWriter();
|
PrintWriter out = response.getWriter();
|
||||||
out.println(
|
out.println("<!DOCTYPE html><html>" + "<head>" + "<meta charset=\"UTF-8\" />" + "<title>HTML Rendered by Servlet</title>" + "</head>" + "<body>" + "<h1>HTML Rendered by Servlet</h1></br>" + "<p>This page was rendered by the ExampleOne Servlet!</p>"
|
||||||
"<!DOCTYPE html><html>" +
|
+ "</body>" + "</html>");
|
||||||
"<head>" +
|
}
|
||||||
"<meta charset=\"UTF-8\" />" +
|
|
||||||
"<title>HTML Rendered by Servlet</title>" +
|
|
||||||
"</head>" +
|
|
||||||
"<body>" +
|
|
||||||
"<h1>HTML Rendered by Servlet</h1></br>" +
|
|
||||||
"<p>This page was rendered by the ExampleOne Servlet!</p>" +
|
|
||||||
"</body>" +
|
|
||||||
"</html>"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -7,18 +7,14 @@ import javax.servlet.http.HttpServlet;
|
|||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
@WebServlet(
|
@WebServlet(name = "ExampleThree", description = "JSP Servlet With Annotations", urlPatterns = { "/ExampleThree" })
|
||||||
name = "ExampleThree",
|
|
||||||
description = "JSP Servlet With Annotations",
|
|
||||||
urlPatterns = {"/ExampleThree"}
|
|
||||||
)
|
|
||||||
public class ExampleThree extends HttpServlet {
|
public class ExampleThree extends HttpServlet {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||||
String message = request.getParameter("message");
|
String message = request.getParameter("message");
|
||||||
request.setAttribute("text", message);
|
request.setAttribute("text", message);
|
||||||
request.getRequestDispatcher("/jsp/ExampleThree.jsp").forward(request, response);
|
request.getRequestDispatcher("/jsp/ExampleThree.jsp").forward(request, response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ import org.springframework.web.servlet.ModelAndView;
|
|||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
public class ErrorController {
|
public class ErrorController {
|
||||||
|
|
||||||
@RequestMapping(value = "500Error", method = RequestMethod.GET)
|
@RequestMapping(value = "500Error", method = RequestMethod.GET)
|
||||||
public void throwRuntimeException() {
|
public void throwRuntimeException() {
|
||||||
throw new NullPointerException("Throwing a null pointer exception");
|
throw new NullPointerException("Throwing a null pointer exception");
|
||||||
@ -34,19 +34,18 @@ public class ErrorController {
|
|||||||
errorMsg = "Http Error Code : 404. Resource not found";
|
errorMsg = "Http Error Code : 404. Resource not found";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// Handle other 4xx error codes.
|
// Handle other 4xx error codes.
|
||||||
case 500: {
|
case 500: {
|
||||||
errorMsg = "Http Error Code : 500. Internal Server Error";
|
errorMsg = "Http Error Code : 500. Internal Server Error";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// Handle other 5xx error codes.
|
// Handle other 5xx error codes.
|
||||||
}
|
}
|
||||||
errorPage.addObject("errorMsg", errorMsg);
|
errorPage.addObject("errorMsg", errorMsg);
|
||||||
return errorPage;
|
return errorPage;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getErrorCode(HttpServletRequest httpRequest) {
|
private int getErrorCode(HttpServletRequest httpRequest) {
|
||||||
return (Integer) httpRequest
|
return (Integer) httpRequest.getAttribute("javax.servlet.error.status_code");
|
||||||
.getAttribute("javax.servlet.error.status_code");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,15 +14,15 @@ import com.baeldung.spring.service.RawDBDemoGeoIPLocationService;
|
|||||||
@Controller
|
@Controller
|
||||||
public class GeoIPTestController {
|
public class GeoIPTestController {
|
||||||
private RawDBDemoGeoIPLocationService locationService;
|
private RawDBDemoGeoIPLocationService locationService;
|
||||||
|
|
||||||
public GeoIPTestController() throws IOException {
|
public GeoIPTestController() throws IOException {
|
||||||
locationService
|
locationService = new RawDBDemoGeoIPLocationService();
|
||||||
= new RawDBDemoGeoIPLocationService();
|
|
||||||
}
|
}
|
||||||
@RequestMapping(value="/GeoIPTest", method = RequestMethod.POST)
|
|
||||||
|
@RequestMapping(value = "/GeoIPTest", method = RequestMethod.POST)
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public GeoIP getLocation(
|
public GeoIP getLocation(@RequestParam(value = "ipAddress", required = true) String ipAddress) throws Exception {
|
||||||
@RequestParam(value="ipAddress", required=true) String ipAddress) throws Exception {
|
|
||||||
|
|
||||||
return locationService.getLocation(ipAddress);
|
return locationService.getLocation(ipAddress);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,6 @@ import org.springframework.web.context.support.ServletContextResource;
|
|||||||
|
|
||||||
import javax.servlet.ServletContext;
|
import javax.servlet.ServletContext;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
@ -5,15 +5,15 @@ public class GeoIP {
|
|||||||
private String city;
|
private String city;
|
||||||
private String latitude;
|
private String latitude;
|
||||||
private String longitude;
|
private String longitude;
|
||||||
|
|
||||||
public GeoIP() {
|
public GeoIP() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public GeoIP(String ipAddress) {
|
public GeoIP(String ipAddress) {
|
||||||
this.ipAddress = ipAddress;
|
this.ipAddress = ipAddress;
|
||||||
}
|
}
|
||||||
|
|
||||||
public GeoIP(String ipAddress, String city, String latitude, String longitude) {
|
public GeoIP(String ipAddress, String city, String latitude, String longitude) {
|
||||||
this.ipAddress = ipAddress;
|
this.ipAddress = ipAddress;
|
||||||
this.city = city;
|
this.city = city;
|
||||||
@ -52,5 +52,5 @@ public class GeoIP {
|
|||||||
public void setLongitude(String longitude) {
|
public void setLongitude(String longitude) {
|
||||||
this.longitude = longitude;
|
this.longitude = longitude;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -9,18 +9,18 @@ import com.maxmind.geoip2.DatabaseReader;
|
|||||||
import com.maxmind.geoip2.exception.GeoIp2Exception;
|
import com.maxmind.geoip2.exception.GeoIp2Exception;
|
||||||
import com.maxmind.geoip2.model.CityResponse;
|
import com.maxmind.geoip2.model.CityResponse;
|
||||||
|
|
||||||
public class RawDBDemoGeoIPLocationService{
|
public class RawDBDemoGeoIPLocationService {
|
||||||
private DatabaseReader dbReader;
|
private DatabaseReader dbReader;
|
||||||
|
|
||||||
public RawDBDemoGeoIPLocationService() throws IOException {
|
public RawDBDemoGeoIPLocationService() throws IOException {
|
||||||
File database = new File("your-path-to-db-file");
|
File database = new File("your-path-to-db-file");
|
||||||
dbReader = new DatabaseReader.Builder(database).build();
|
dbReader = new DatabaseReader.Builder(database).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
public GeoIP getLocation(String ip) throws IOException, GeoIp2Exception {
|
public GeoIP getLocation(String ip) throws IOException, GeoIp2Exception {
|
||||||
InetAddress ipAddress = InetAddress.getByName(ip);
|
InetAddress ipAddress = InetAddress.getByName(ip);
|
||||||
CityResponse response = dbReader.city(ipAddress);
|
CityResponse response = dbReader.city(ipAddress);
|
||||||
|
|
||||||
String cityName = response.getCity().getName();
|
String cityName = response.getCity().getName();
|
||||||
String latitude = response.getLocation().getLatitude().toString();
|
String latitude = response.getLocation().getLatitude().toString();
|
||||||
String longitude = response.getLocation().getLongitude().toString();
|
String longitude = response.getLocation().getLongitude().toString();
|
||||||
|
@ -10,22 +10,21 @@ import com.maxmind.geoip2.DatabaseReader;
|
|||||||
import com.maxmind.geoip2.exception.GeoIp2Exception;
|
import com.maxmind.geoip2.exception.GeoIp2Exception;
|
||||||
import com.maxmind.geoip2.model.CityResponse;
|
import com.maxmind.geoip2.model.CityResponse;
|
||||||
|
|
||||||
|
|
||||||
public class GeoIpIntegrationTest {
|
public class GeoIpIntegrationTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenIP_whenFetchingCity_thenReturnsCityData() throws IOException, GeoIp2Exception {
|
public void givenIP_whenFetchingCity_thenReturnsCityData() throws IOException, GeoIp2Exception {
|
||||||
File database = new File("your-path-to-db-file");
|
File database = new File("your-path-to-db-file");
|
||||||
DatabaseReader dbReader = new DatabaseReader.Builder(database).build();
|
DatabaseReader dbReader = new DatabaseReader.Builder(database).build();
|
||||||
|
|
||||||
InetAddress ipAddress = InetAddress.getByName("your-public-ip");
|
InetAddress ipAddress = InetAddress.getByName("your-public-ip");
|
||||||
CityResponse response = dbReader.city(ipAddress);
|
CityResponse response = dbReader.city(ipAddress);
|
||||||
|
|
||||||
String countryName = response.getCountry().getName();
|
String countryName = response.getCountry().getName();
|
||||||
String cityName = response.getCity().getName();
|
String cityName = response.getCity().getName();
|
||||||
String postal = response.getPostal().getCode();
|
String postal = response.getPostal().getCode();
|
||||||
String state = response.getLeastSpecificSubdivision().getName();
|
String state = response.getLeastSpecificSubdivision().getName();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user