diff --git a/spring-mvc-xml/src/main/java/com/baeldung/jsp/ExampleOne.java b/spring-mvc-xml/src/main/java/com/baeldung/jsp/ExampleOne.java index 0b153bf8ec..6744570639 100644 --- a/spring-mvc-xml/src/main/java/com/baeldung/jsp/ExampleOne.java +++ b/spring-mvc-xml/src/main/java/com/baeldung/jsp/ExampleOne.java @@ -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( - "" + - "
" + - "" + - "This page was rendered by the ExampleOne Servlet!
" + - "" + - "" - ); - } + + 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("" + "" + "" + "This page was rendered by the ExampleOne Servlet!
" + + "" + ""); + } } \ No newline at end of file diff --git a/spring-mvc-xml/src/main/java/com/baeldung/jsp/ExampleThree.java b/spring-mvc-xml/src/main/java/com/baeldung/jsp/ExampleThree.java index 49fefcffde..7269f917b4 100644 --- a/spring-mvc-xml/src/main/java/com/baeldung/jsp/ExampleThree.java +++ b/spring-mvc-xml/src/main/java/com/baeldung/jsp/ExampleThree.java @@ -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); + } } diff --git a/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/ErrorController.java b/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/ErrorController.java index 96556bd5b1..6ae1023374 100644 --- a/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/ErrorController.java +++ b/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/ErrorController.java @@ -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"); } } diff --git a/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/GeoIPTestController.java b/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/GeoIPTestController.java index 16de4e56f5..eeaddcf8e0 100644 --- a/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/GeoIPTestController.java +++ b/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/GeoIPTestController.java @@ -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); } } diff --git a/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/ImageController.java b/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/ImageController.java index ef8d1214df..fc46c07e06 100644 --- a/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/ImageController.java +++ b/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/ImageController.java @@ -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; diff --git a/spring-mvc-xml/src/main/java/com/baeldung/spring/form/GeoIP.java b/spring-mvc-xml/src/main/java/com/baeldung/spring/form/GeoIP.java index 19f56867a1..4373303107 100644 --- a/spring-mvc-xml/src/main/java/com/baeldung/spring/form/GeoIP.java +++ b/spring-mvc-xml/src/main/java/com/baeldung/spring/form/GeoIP.java @@ -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; } - + } diff --git a/spring-mvc-xml/src/main/java/com/baeldung/spring/service/RawDBDemoGeoIPLocationService.java b/spring-mvc-xml/src/main/java/com/baeldung/spring/service/RawDBDemoGeoIPLocationService.java index af3ce8cfb3..04443466c9 100644 --- a/spring-mvc-xml/src/main/java/com/baeldung/spring/service/RawDBDemoGeoIPLocationService.java +++ b/spring-mvc-xml/src/main/java/com/baeldung/spring/service/RawDBDemoGeoIPLocationService.java @@ -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(); diff --git a/spring-mvc-xml/src/test/java/com/baeldung/geoip/GeoIpIntegrationTest.java b/spring-mvc-xml/src/test/java/com/baeldung/geoip/GeoIpIntegrationTest.java index 2edaa125b7..0e957f3400 100644 --- a/spring-mvc-xml/src/test/java/com/baeldung/geoip/GeoIpIntegrationTest.java +++ b/spring-mvc-xml/src/test/java/com/baeldung/geoip/GeoIpIntegrationTest.java @@ -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(); - + } - + }