Skip to content

Commit a3e6236

Browse files
ThoughtscriptEugen
authored andcommitted
BAEL-426: Guide to JSP (eugenp#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
1 parent 3db2e80 commit a3e6236

10 files changed

Lines changed: 113 additions & 2 deletions

File tree

‎spring-mvc-java/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
1515
- [A Quick Guide to Spring MVC Matrix Variables](http://www.baeldung.com/spring-mvc-matrix-variables)
1616
- [Intro to WebSockets with Spring](http://www.baeldung.com/websockets-spring)
1717
- [File Upload with Spring MVC](http://www.baeldung.com/spring-file-upload)
18-
- [Spring MVC Content Negotiation](http://www.baeldung.com/spring-mvc-content-negotiation-json-xml)
18+
- [Spring MVC Content Negotiation](http://www.baeldung.com/spring-mvc-content-negotiation-json-xml)

‎spring-mvc-java/src/test/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
### Relevant Articles:
22
- [Circular Dependencies in Spring](http://www.baeldung.com/circular-dependencies-in-spring)
3+
- [Guide to JSP](http://www.baeldung.com/guide-to-jsp)

‎spring-mvc-xml/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
1313
- [Basic Forms with Spring MVC](http://www.baeldung.com/spring-mvc-form-tutorial)
1414
- [Returning Image/Media Data with Spring MVC](http://www.baeldung.com/spring-mvc-image-media-data)
1515
- [Geolocation by IP in Java](http://www.baeldung.com/geolocation-by-ip-with-maxmind)
16+
- [Guide to JSP](http://www.baeldung.com/guide-to-jsp)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.jsp;
2+
3+
import java.io.IOException;
4+
import java.io.PrintWriter;
5+
import javax.servlet.ServletException;
6+
import javax.servlet.http.HttpServlet;
7+
import javax.servlet.http.HttpServletRequest;
8+
import javax.servlet.http.HttpServletResponse;
9+
10+
public class ExampleOne extends HttpServlet {
11+
12+
private static final long serialVersionUID = 1L;
13+
14+
@Override
15+
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
16+
response.setContentType("text/html");
17+
PrintWriter out = response.getWriter();
18+
out.println(
19+
"<!DOCTYPE html><html>" +
20+
"<head>" +
21+
"<meta charset=\"UTF-8\" />" +
22+
"<title>HTML Rendered by Servlet</title>" +
23+
"</head>" +
24+
"<body>" +
25+
"<h1>HTML Rendered by Servlet</h1></br>" +
26+
"<p>This page was rendered by the ExampleOne Servlet!</p>" +
27+
"</body>" +
28+
"</html>"
29+
);
30+
}
31+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.jsp;
2+
3+
import java.io.IOException;
4+
import javax.servlet.ServletException;
5+
import javax.servlet.annotation.WebServlet;
6+
import javax.servlet.http.HttpServlet;
7+
import javax.servlet.http.HttpServletRequest;
8+
import javax.servlet.http.HttpServletResponse;
9+
10+
@WebServlet(
11+
name = "ExampleThree",
12+
description = "JSP Servlet With Annotations",
13+
urlPatterns = {"/ExampleThree"}
14+
)
15+
public class ExampleThree extends HttpServlet {
16+
private static final long serialVersionUID = 1L;
17+
18+
@Override
19+
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
20+
String message = request.getParameter("message");
21+
request.setAttribute("text", message);
22+
request.getRequestDispatcher("/jsp/ExampleThree.jsp").forward(request, response);
23+
}
24+
}

‎spring-mvc-xml/src/main/resources/contentManagementWebMvcConfig.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
1212

1313
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />
14-
<context:component-scan base-package="org.baeldung.spring.controller" />
14+
<context:component-scan base-package="com.baeldung.spring.controller" />
1515
<bean id="viewResolver"
1616
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
1717
<property name="prefix" value="/WEB-INF/view/" />

‎spring-mvc-xml/src/main/webapp/WEB-INF/web.xml‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,25 @@
3434
<url-pattern>/</url-pattern>
3535
</servlet-mapping>
3636

37+
<!-- JSP Servlet -->
38+
39+
<servlet>
40+
<servlet-name>ExampleOne</servlet-name>
41+
<servlet-class>com.baeldung.jsp.ExampleOne</servlet-class>
42+
</servlet>
43+
<servlet-mapping>
44+
<servlet-name>ExampleOne</servlet-name>
45+
<url-pattern>/jsp/ExampleOne</url-pattern>
46+
</servlet-mapping>
47+
<servlet>
48+
<servlet-name>ExampleThree</servlet-name>
49+
<servlet-class>com.baeldung.jsp.ExampleThree</servlet-class>
50+
</servlet>
51+
<servlet-mapping>
52+
<servlet-name>ExampleThree</servlet-name>
53+
<url-pattern>/jsp/ExampleThree</url-pattern>
54+
</servlet-mapping>
55+
3756
<!-- additional config -->
3857

3958
<session-config>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
2+
<!DOCTYPE html>
3+
<head>
4+
<title>Java Binding Example</title>
5+
</head>
6+
<body>
7+
<h1>Bound Value</h1>
8+
<p>You said: ${text}</p>
9+
</body>
10+
</html>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
2+
<!DOCTYPE html>
3+
<head>
4+
<title>Java in Static Page Example</title>
5+
</head>
6+
<body>
7+
<h1>Java in Static Page Example</h1>
8+
<% String[] arr = {"What's up?", "Hello", "It's a nice day today!"};
9+
String greetings = arr[(int)(Math.random() * arr.length)];
10+
%>
11+
<p><%= greetings %></p>
12+
</body>
13+
</html>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
2+
<html>
3+
<head>
4+
<title>JSP Examples</title>
5+
</head>
6+
<body>
7+
<h1>Simple JSP Examples</h1>
8+
<p>Invoke HTML rendered by Servlet: <a href="ExampleOne" target="_blank">here</a></p>
9+
<p>Java in static page: <a href="ExampleTwo.jsp" target="_blank">here</a></p>
10+
<p>Java injected by Servlet: <a href="ExampleThree?message=hello!" target="_blank">here</a></p>
11+
</body>
12+
</html>

0 commit comments

Comments
 (0)