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