From a3e623622b857b39ce5b86df9f0bd9fcc7934eec Mon Sep 17 00:00:00 2001 From: Adam InTae Gerard Date: Mon, 12 Dec 2016 05:18:50 -0600 Subject: [PATCH] BAEL-426: Guide to JSP (#803) * JSP Servlets With Three Examples JSP Article! * Readme BAEL-426 - still needs permanent URL * Cleanup * BAEL-426 - Final spot for working code Tested. It's all good. * Root documentation added! * Cleanup * JSP added * Fixed Broken Module in Spring --- spring-mvc-java/README.md | 2 +- spring-mvc-java/src/test/README.md | 1 + spring-mvc-xml/README.md | 1 + .../java/com/baeldung/jsp/ExampleOne.java | 31 +++++++++++++++++++ .../java/com/baeldung/jsp/ExampleThree.java | 24 ++++++++++++++ .../contentManagementWebMvcConfig.xml | 2 +- .../src/main/webapp/WEB-INF/web.xml | 19 ++++++++++++ .../src/main/webapp/jsp/ExampleThree.jsp | 10 ++++++ .../src/main/webapp/jsp/ExampleTwo.jsp | 13 ++++++++ spring-mvc-xml/src/main/webapp/jsp/index.jsp | 12 +++++++ 10 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 spring-mvc-xml/src/main/java/com/baeldung/jsp/ExampleOne.java create mode 100644 spring-mvc-xml/src/main/java/com/baeldung/jsp/ExampleThree.java create mode 100644 spring-mvc-xml/src/main/webapp/jsp/ExampleThree.jsp create mode 100644 spring-mvc-xml/src/main/webapp/jsp/ExampleTwo.jsp create mode 100644 spring-mvc-xml/src/main/webapp/jsp/index.jsp diff --git a/spring-mvc-java/README.md b/spring-mvc-java/README.md index 996a7d96b1..783e29f2f2 100644 --- a/spring-mvc-java/README.md +++ b/spring-mvc-java/README.md @@ -15,4 +15,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [A Quick Guide to Spring MVC Matrix Variables](http://www.baeldung.com/spring-mvc-matrix-variables) - [Intro to WebSockets with Spring](http://www.baeldung.com/websockets-spring) - [File Upload with Spring MVC](http://www.baeldung.com/spring-file-upload) -- [Spring MVC Content Negotiation](http://www.baeldung.com/spring-mvc-content-negotiation-json-xml) +- [Spring MVC Content Negotiation](http://www.baeldung.com/spring-mvc-content-negotiation-json-xml) \ No newline at end of file diff --git a/spring-mvc-java/src/test/README.md b/spring-mvc-java/src/test/README.md index 20ee1918af..260bbdca97 100644 --- a/spring-mvc-java/src/test/README.md +++ b/spring-mvc-java/src/test/README.md @@ -1,2 +1,3 @@ ### Relevant Articles: - [Circular Dependencies in Spring](http://www.baeldung.com/circular-dependencies-in-spring) +- [Guide to JSP](http://www.baeldung.com/guide-to-jsp) \ No newline at end of file diff --git a/spring-mvc-xml/README.md b/spring-mvc-xml/README.md index e96a8d0392..097353c88c 100644 --- a/spring-mvc-xml/README.md +++ b/spring-mvc-xml/README.md @@ -13,3 +13,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Basic Forms with Spring MVC](http://www.baeldung.com/spring-mvc-form-tutorial) - [Returning Image/Media Data with Spring MVC](http://www.baeldung.com/spring-mvc-image-media-data) - [Geolocation by IP in Java](http://www.baeldung.com/geolocation-by-ip-with-maxmind) +- [Guide to JSP](http://www.baeldung.com/guide-to-jsp) \ No newline at end of file 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 new file mode 100644 index 0000000000..0b153bf8ec --- /dev/null +++ b/spring-mvc-xml/src/main/java/com/baeldung/jsp/ExampleOne.java @@ -0,0 +1,31 @@ +package com.baeldung.jsp; + +import java.io.IOException; +import java.io.PrintWriter; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +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( + "" + + "" + + "" + + "HTML Rendered by Servlet" + + "" + + "" + + "

HTML Rendered by Servlet


" + + "

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 new file mode 100644 index 0000000000..49fefcffde --- /dev/null +++ b/spring-mvc-xml/src/main/java/com/baeldung/jsp/ExampleThree.java @@ -0,0 +1,24 @@ +package com.baeldung.jsp; + +import java.io.IOException; +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +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"} +) +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); + } +} diff --git a/spring-mvc-xml/src/main/resources/contentManagementWebMvcConfig.xml b/spring-mvc-xml/src/main/resources/contentManagementWebMvcConfig.xml index e68c88d19d..41422d8954 100644 --- a/spring-mvc-xml/src/main/resources/contentManagementWebMvcConfig.xml +++ b/spring-mvc-xml/src/main/resources/contentManagementWebMvcConfig.xml @@ -11,7 +11,7 @@ http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"> - + diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml b/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml index 2240ac0a22..1ea3051426 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml @@ -34,6 +34,25 @@ / + + + + ExampleOne + com.baeldung.jsp.ExampleOne + + + ExampleOne + /jsp/ExampleOne + + + ExampleThree + com.baeldung.jsp.ExampleThree + + + ExampleThree + /jsp/ExampleThree + + diff --git a/spring-mvc-xml/src/main/webapp/jsp/ExampleThree.jsp b/spring-mvc-xml/src/main/webapp/jsp/ExampleThree.jsp new file mode 100644 index 0000000000..665eb86a30 --- /dev/null +++ b/spring-mvc-xml/src/main/webapp/jsp/ExampleThree.jsp @@ -0,0 +1,10 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Java Binding Example + + +

Bound Value

+

You said: ${text}

+ + \ No newline at end of file diff --git a/spring-mvc-xml/src/main/webapp/jsp/ExampleTwo.jsp b/spring-mvc-xml/src/main/webapp/jsp/ExampleTwo.jsp new file mode 100644 index 0000000000..7b2247638d --- /dev/null +++ b/spring-mvc-xml/src/main/webapp/jsp/ExampleTwo.jsp @@ -0,0 +1,13 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Java in Static Page Example + + +

Java in Static Page Example

+ <% String[] arr = {"What's up?", "Hello", "It's a nice day today!"}; + String greetings = arr[(int)(Math.random() * arr.length)]; + %> +

<%= greetings %>

+ + \ No newline at end of file diff --git a/spring-mvc-xml/src/main/webapp/jsp/index.jsp b/spring-mvc-xml/src/main/webapp/jsp/index.jsp new file mode 100644 index 0000000000..58c464125d --- /dev/null +++ b/spring-mvc-xml/src/main/webapp/jsp/index.jsp @@ -0,0 +1,12 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + JSP Examples + + +

Simple JSP Examples

+

Invoke HTML rendered by Servlet: here

+

Java in static page: here

+

Java injected by Servlet: here

+ + \ No newline at end of file