forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/eugenp/tutorials into BAE…
…L-10982
- Loading branch information
Showing
9 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
### Relevant Articles: | ||
- [Guide to Stream.reduce()](https://www.baeldung.com/java-stream-reduce) | ||
|
35 changes: 35 additions & 0 deletions
35
javax-servlets/src/main/java/com/baeldung/filters/EmptyParamFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() { | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
javax-servlets/src/main/java/com/baeldung/listeners/AppListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) { | ||
|
||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
javax-servlets/src/main/java/com/baeldung/listeners/RequestListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
javax-servlets/src/main/java/com/baeldung/servlets/CounterServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
javax-servlets/src/main/java/com/baeldung/servlets/UppercaseServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
### Relevant Articles: | ||
- [Building a Web Application with Spring Boot and Angular](https://www.baeldung.com/spring-boot-angular-web) | ||
|