Java Servlet JAVA EE
HttpServlet is a base class to be extended to create an HTTP servlet suitable for handling HTTP request and providing responses. We usually override methods like doGet(..), doPost(..) etc.
Examples
Accessing exceptions information in Servlet. package com.logicbig.example;
import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter;
@WebServlet(name = "errorHandlerServlet", urlPatterns = {"/errorHandler"}, loadOnStartup = 1) public class ErrorHandlerServlet extends HttpServlet {
@Override protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter writer = resp.getWriter();
//we can instead use constants like RequestDispatcher.ERROR_EXCEPTION Exception exception = (Exception) req.getAttribute( "javax.servlet.error.exception"); writer.printf("exception: %s%n", exception);
Class exceptionClass = (Class) req.getAttribute( "javax.servlet.error.exception_type"); writer.printf("exception_type: %s%n", exceptionClass);
Integer code = (Integer) req.getAttribute( "javax.servlet.error.status_code"); writer.printf("status_code: %s%n", code);
String errorMessage = (String) req.getAttribute( "javax.servlet.error.message"); writer.printf("message: %s%n", errorMessage);
String requestUri = (String) req.getAttribute( "javax.servlet.error.request_uri");
resp.getWriter().printf("request_uri: %s%n", requestUri);
String servletName = (String) req.getAttribute( "javax.servlet.error.servlet_name"); writer.printf("servlet_name: %s%n", servletName);
} }
Original Post
Overriding getLastModified() to make use of 'Last-Modified' and 'If-Modified-Since' headers. package com.logicbig.example;
import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime;
@WebServlet(name = "testServlet2", urlPatterns = {"/test2"}, loadOnStartup = 1) public class MyServlet2 extends HttpServlet {
@Override protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html"); PrintWriter writer = resp.getWriter(); writer.write("<h4>My Servlet 2</h4>"); writer.write(LocalDateTime.now().toString()); writer.write("<br/><a href='test2'>test2</a>"); }
@Override protected long getLastModified (HttpServletRequest req) { return getLastModifiedMillis(); }
private static long getLastModifiedMillis () { ZonedDateTime zdt = ZonedDateTime.of(LocalDateTime.of(2017, 1, 1, 10, 30, 20), ZoneId.of("GMT")); return zdt.toInstant().toEpochMilli(); } }
Original Post
Instead of overriding getLastModified(), manually setting 'Last-Modified' and 'If-Modified-Since' headers. package com.logicbig.example;
import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime;
@WebServlet(name = "testServlet1", urlPatterns = {"/test1"}, loadOnStartup = 1) public class MyServlet1 extends HttpServlet {
@Override protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
long lastModifiedFromBrowser = req.getDateHeader("If-Modified-Since"); long lastModifiedFromServer = getLastModifiedMillis();
if (lastModifiedFromBrowser != -1 && lastModifiedFromServer <= lastModifiedFromBrowser) { //setting 304 and returning with empty body resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED); return; }
resp.addDateHeader("Last-Modified", lastModifiedFromServer); //uncomment to force revalidate on all actions //resp.addHeader("Cache-Control", "no-cache"); resp.setContentType("text/html"); PrintWriter writer = resp.getWriter(); writer.write("<h4>My Servlet 1</h4>"); writer.write(LocalDateTime.now().toString()); writer.write("<br/><a href='test1'>test1</a>"); }
private static long getLastModifiedMillis () { //Using hard coded value, in real scenario there should be for example // last modified date of this servlet or of the underlying resource ZonedDateTime zdt = ZonedDateTime.of(LocalDateTime.of(2017, 1, 8, 10, 30, 20), ZoneId.of("GMT")); return zdt.toInstant().toEpochMilli(); } }
Original Post
Working with 'ETag' and 'If-None-Match' headers. package com.logicbig.example;
import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.time.LocalDateTime;
@WebServlet(urlPatterns = "/test") public class MyServlet extends HttpServlet {
@Override protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String eTagFromBrowser = req.getHeader("If-None-Match"); String eTagFromServer = getETag();
if (eTagFromServer.equals(eTagFromBrowser)) { //setting 304 and returning with empty body resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED); return; }
resp.addHeader("ETag", getETag()); resp.setContentType("text/html"); PrintWriter writer = resp.getWriter(); writer.write("<h4>My Servlet</h4>"); writer.write(LocalDateTime.now().toString()); writer.write("<br/><a href='test'>test</a>"); }
private String getETag () { //Using hard coded value, in real scenario this value might be auto-generated //from the resource content, for example, by using a hash function. return "\"version1\""; } }
Original Post
|
|