Merge pull request #6032 from j7-juarez/BAEL-2481
java web application without web.xml - servlets 3.0
This commit is contained in:
commit
effe32c7c6
|
@ -0,0 +1,35 @@
|
||||||
|
package com.baeldung.filters;
|
||||||
|
|
||||||
|
import javax.servlet.Filter;
|
||||||
|
import javax.servlet.FilterChain;
|
||||||
|
import javax.servlet.FilterConfig;
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.ServletRequest;
|
||||||
|
import javax.servlet.ServletResponse;
|
||||||
|
import javax.servlet.annotation.WebFilter;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@WebFilter(urlPatterns = "/uppercase")
|
||||||
|
public class EmptyParamFilter implements Filter {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(FilterConfig filterConfig) throws ServletException {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
|
||||||
|
FilterChain filterChain) throws IOException, ServletException {
|
||||||
|
String inputString = servletRequest.getParameter("input");
|
||||||
|
|
||||||
|
if (inputString != null && inputString.matches("[A-Za-z0-9]+")) {
|
||||||
|
filterChain.doFilter(servletRequest, servletResponse);
|
||||||
|
} else {
|
||||||
|
servletResponse.getWriter().println("Missing input parameter");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void destroy() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.baeldung.listeners;
|
||||||
|
|
||||||
|
import javax.servlet.ServletContext;
|
||||||
|
import javax.servlet.ServletContextEvent;
|
||||||
|
import javax.servlet.ServletContextListener;
|
||||||
|
import javax.servlet.annotation.WebListener;
|
||||||
|
|
||||||
|
@WebListener
|
||||||
|
public class AppListener implements ServletContextListener {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void contextInitialized(ServletContextEvent event) {
|
||||||
|
ServletContext context = event.getServletContext();
|
||||||
|
context.setAttribute("counter", 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void contextDestroyed(ServletContextEvent event) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package com.baeldung.listeners;
|
||||||
|
|
||||||
|
import javax.servlet.ServletContext;
|
||||||
|
import javax.servlet.ServletRequestEvent;
|
||||||
|
import javax.servlet.ServletRequestListener;
|
||||||
|
import javax.servlet.annotation.WebListener;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
@WebListener
|
||||||
|
public class RequestListener implements ServletRequestListener {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void requestInitialized(ServletRequestEvent event) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void requestDestroyed(ServletRequestEvent event) {
|
||||||
|
HttpServletRequest request = (HttpServletRequest)event.getServletRequest();
|
||||||
|
if (!request.getServletPath().equals("/counter")) {
|
||||||
|
ServletContext context = event.getServletContext();
|
||||||
|
context.setAttribute("counter", (int)context.getAttribute("counter") + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.baeldung.servlets;
|
||||||
|
|
||||||
|
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(urlPatterns = "/counter", name = "counterServlet")
|
||||||
|
public class CounterServlet extends HttpServlet {
|
||||||
|
|
||||||
|
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
||||||
|
PrintWriter out = response.getWriter();
|
||||||
|
|
||||||
|
int count = (int)request.getServletContext().getAttribute("counter");
|
||||||
|
|
||||||
|
out.println("Request counter: " + count);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
package com.baeldung.servlets;
|
||||||
|
|
||||||
|
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(urlPatterns = "/uppercase", name = "uppercaseServlet")
|
||||||
|
public class UppercaseServlet extends HttpServlet {
|
||||||
|
|
||||||
|
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
||||||
|
String inputString = request.getParameter("input").toUpperCase();
|
||||||
|
|
||||||
|
PrintWriter out = response.getWriter();
|
||||||
|
|
||||||
|
out.println(inputString);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue