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
This commit is contained in:
Adam InTae Gerard
2016-12-12 05:18:50 -06:00
committed by Eugen
parent 3db2e80ab6
commit a3e623622b
10 changed files with 113 additions and 2 deletions

View File

@@ -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(
"<!DOCTYPE html><html>" +
"<head>" +
"<meta charset=\"UTF-8\" />" +
"<title>HTML Rendered by Servlet</title>" +
"</head>" +
"<body>" +
"<h1>HTML Rendered by Servlet</h1></br>" +
"<p>This page was rendered by the ExampleOne Servlet!</p>" +
"</body>" +
"</html>"
);
}
}

View File

@@ -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);
}
}

View File

@@ -11,7 +11,7 @@
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />
<context:component-scan base-package="org.baeldung.spring.controller" />
<context:component-scan base-package="com.baeldung.spring.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />

View File

@@ -34,6 +34,25 @@
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- JSP Servlet -->
<servlet>
<servlet-name>ExampleOne</servlet-name>
<servlet-class>com.baeldung.jsp.ExampleOne</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ExampleOne</servlet-name>
<url-pattern>/jsp/ExampleOne</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ExampleThree</servlet-name>
<servlet-class>com.baeldung.jsp.ExampleThree</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ExampleThree</servlet-name>
<url-pattern>/jsp/ExampleThree</url-pattern>
</servlet-mapping>
<!-- additional config -->
<session-config>

View File

@@ -0,0 +1,10 @@
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<head>
<title>Java Binding Example</title>
</head>
<body>
<h1>Bound Value</h1>
<p>You said: ${text}</p>
</body>
</html>

View File

@@ -0,0 +1,13 @@
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<head>
<title>Java in Static Page Example</title>
</head>
<body>
<h1>Java in Static Page Example</h1>
<% String[] arr = {"What's up?", "Hello", "It's a nice day today!"};
String greetings = arr[(int)(Math.random() * arr.length)];
%>
<p><%= greetings %></p>
</body>
</html>

View File

@@ -0,0 +1,12 @@
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>JSP Examples</title>
</head>
<body>
<h1>Simple JSP Examples</h1>
<p>Invoke HTML rendered by Servlet: <a href="ExampleOne" target="_blank">here</a></p>
<p>Java in static page: <a href="ExampleTwo.jsp" target="_blank">here</a></p>
<p>Java injected by Servlet: <a href="ExampleThree?message=hello!" target="_blank">here</a></p>
</body>
</html>