Skip to content

Commit 2c1ccfb

Browse files
committed
Some cleanup of the Servlet samples
Added test for cookie sample Signed-off-by: arjantijms <arjan.tijms@gmail.com>
1 parent e4737e6 commit 2c1ccfb

File tree

32 files changed

+413
-194
lines changed

32 files changed

+413
-194
lines changed

servlet/async-servlet/pom.xml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
43
<modelVersion>4.0.0</modelVersion>
54

65
<parent>
76
<groupId>org.javaee7</groupId>
87
<artifactId>servlet</artifactId>
98
<version>1.0-SNAPSHOT</version>
109
</parent>
11-
10+
1211
<artifactId>servlet-async-servlet</artifactId>
1312
<packaging>war</packaging>
14-
13+
1514
<name>Java EE 7 Sample: servlet - async-servlet</name>
1615
</project>

servlet/async-servlet/src/main/java/org/javaee7/servlet/async/MyAsyncServlet.java

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
*/
4040
package org.javaee7.servlet.async;
4141

42+
import java.io.IOException;
43+
4244
import javax.annotation.Resource;
4345
import javax.enterprise.concurrent.ManagedExecutorService;
4446
import javax.servlet.AsyncContext;
@@ -49,32 +51,30 @@
4951
import javax.servlet.http.HttpServlet;
5052
import javax.servlet.http.HttpServletRequest;
5153
import javax.servlet.http.HttpServletResponse;
52-
import java.io.IOException;
5354

5455
/**
5556
* @author Arun Gupta
5657
*/
5758
@WebServlet(urlPatterns = "/MyAsyncServlet", asyncSupported = true)
5859
public class MyAsyncServlet extends HttpServlet {
5960

60-
// @Resource(lookup="java:comp/DefaultManagedExecutorService")
61+
private static final long serialVersionUID = 3709640331218336841L;
62+
6163
@Resource
6264
ManagedExecutorService executor;
6365

6466
/**
65-
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
66-
* methods.
67+
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
6768
*
6869
* @param request servlet request
6970
* @param response servlet response
7071
* @throws ServletException if a servlet-specific error occurs
7172
* @throws IOException if an I/O error occurs
7273
*/
73-
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
74-
throws ServletException, IOException {
75-
AsyncContext ac = request.startAsync();
74+
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
75+
AsyncContext asyncContext = request.startAsync();
7676

77-
ac.addListener(new AsyncListener() {
77+
asyncContext.addListener(new AsyncListener() {
7878
@Override
7979
public void onComplete(AsyncEvent event) throws IOException {
8080
event.getSuppliedResponse().getWriter().println("onComplete");
@@ -96,29 +96,29 @@ public void onStartAsync(AsyncEvent event) throws IOException {
9696
event.getSuppliedResponse().getWriter().println("onStartAsync");
9797
}
9898
});
99-
executor.submit(new MyAsyncService(ac));
99+
100+
executor.submit(new MyAsyncService(asyncContext));
100101
}
101102

102103
class MyAsyncService implements Runnable {
103104

104-
AsyncContext ac;
105+
AsyncContext asyncContext;
105106

106-
public MyAsyncService(AsyncContext ac) {
107-
this.ac = ac;
107+
public MyAsyncService(AsyncContext asyncContext) {
108+
this.asyncContext = asyncContext;
108109
}
109110

110111
@Override
111112
public void run() {
112113
try {
113-
ac.getResponse().getWriter().println("Running inside MyAsyncService");
114+
asyncContext.getResponse().getWriter().println("Running inside MyAsyncService");
114115
} catch (IOException e) {
115116
throw new IllegalStateException(e);
116117
}
117-
ac.complete();
118+
asyncContext.complete();
118119
}
119120
}
120121

121-
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
122122
/**
123123
* Handles the HTTP <code>GET</code> method.
124124
*
@@ -128,8 +128,7 @@ public void run() {
128128
* @throws IOException if an I/O error occurs
129129
*/
130130
@Override
131-
protected void doGet(HttpServletRequest request, HttpServletResponse response)
132-
throws ServletException, IOException {
131+
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
133132
processRequest(request, response);
134133
}
135134

@@ -142,8 +141,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
142141
* @throws IOException if an I/O error occurs
143142
*/
144143
@Override
145-
protected void doPost(HttpServletRequest request, HttpServletResponse response)
146-
throws ServletException, IOException {
144+
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
147145
processRequest(request, response);
148146
}
149147

@@ -155,5 +153,5 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
155153
@Override
156154
public String getServletInfo() {
157155
return "Short description";
158-
}// </editor-fold>
156+
}
159157
}

servlet/cookies/pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
<version>1.0-SNAPSHOT</version>
99
<relativePath>../pom.xml</relativePath>
1010
</parent>
11+
1112
<groupId>org.javaee7</groupId>
1213
<artifactId>servlet-cookies</artifactId>
1314
<version>1.0-SNAPSHOT</version>
1415
<packaging>war</packaging>
16+
1517
<name>Java EE 7 Sample: servlet - cookies</name>
1618
</project>
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.javaee7.servlet.cookies;
41+
42+
import java.io.IOException;
43+
import java.io.PrintWriter;
44+
45+
import javax.servlet.ServletException;
46+
import javax.servlet.annotation.WebServlet;
47+
import javax.servlet.http.HttpServlet;
48+
import javax.servlet.http.HttpServletRequest;
49+
import javax.servlet.http.HttpServletResponse;
50+
51+
/**
52+
* @author Arun Gupta
53+
*/
54+
@WebServlet(urlPatterns = { "/ClientCookieServlet" })
55+
public class ClientCookieServlet extends HttpServlet {
56+
57+
private static final long serialVersionUID = -1944396991856607131L;
58+
59+
/**
60+
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
61+
*
62+
* @param request servlet request
63+
* @param response servlet response
64+
* @throws ServletException if a servlet-specific error occurs
65+
* @throws IOException if an I/O error occurs
66+
*/
67+
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
68+
response.setContentType("text/html;charset=UTF-8");
69+
70+
try (PrintWriter out = response.getWriter()) {
71+
out.println("<!DOCTYPE html>");
72+
out.println("<html>");
73+
out.println("<head>");
74+
out.println("<title>Servlet ClientCookieServlet</title>");
75+
out.println("</head>");
76+
out.println("<body>");
77+
out.println("<h1>Servlet ClientCookieServlet at " + request.getContextPath() + "</h1>");
78+
79+
80+
out.println("<script type=\"text/javascript\">");
81+
out.println("document.write(document.cookie);");
82+
out.println("</script>");
83+
84+
out.println("</body>");
85+
out.println("</html>");
86+
}
87+
}
88+
89+
/**
90+
* Handles the HTTP <code>GET</code> method.
91+
*
92+
* @param request servlet request
93+
* @param response servlet response
94+
* @throws ServletException if a servlet-specific error occurs
95+
* @throws IOException if an I/O error occurs
96+
*/
97+
@Override
98+
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
99+
processRequest(request, response);
100+
}
101+
102+
/**
103+
* Handles the HTTP <code>POST</code> method.
104+
*
105+
* @param request servlet request
106+
* @param response servlet response
107+
* @throws ServletException if a servlet-specific error occurs
108+
* @throws IOException if an I/O error occurs
109+
*/
110+
@Override
111+
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
112+
processRequest(request, response);
113+
}
114+
}

servlet/cookies/src/main/java/org/javaee7/servlet/cookies/TestServlet.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@
4141

4242
import java.io.IOException;
4343
import java.io.PrintWriter;
44+
4445
import javax.servlet.ServletException;
45-
import javax.servlet.SessionCookieConfig;
46-
import javax.servlet.annotation.MultipartConfig;
4746
import javax.servlet.annotation.WebServlet;
4847
import javax.servlet.http.Cookie;
4948
import javax.servlet.http.HttpServlet;
@@ -54,21 +53,21 @@
5453
* @author Arun Gupta
5554
*/
5655
@WebServlet(urlPatterns = { "/TestServlet" })
57-
@MultipartConfig(location = "/tmp")
5856
public class TestServlet extends HttpServlet {
5957

58+
private static final long serialVersionUID = -1944396991856607131L;
59+
6060
/**
61-
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
62-
* methods.
61+
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
6362
*
6463
* @param request servlet request
6564
* @param response servlet response
6665
* @throws ServletException if a servlet-specific error occurs
6766
* @throws IOException if an I/O error occurs
6867
*/
69-
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
70-
throws ServletException, IOException {
68+
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
7169
response.setContentType("text/html;charset=UTF-8");
70+
7271
try (PrintWriter out = response.getWriter()) {
7372
out.println("<!DOCTYPE html>");
7473
out.println("<html>");
@@ -77,33 +76,36 @@ protected void processRequest(HttpServletRequest request, HttpServletResponse re
7776
out.println("</head>");
7877
out.println("<body>");
7978
out.println("<h1>Servlet TestServlet at " + request.getContextPath() + "</h1>");
80-
SessionCookieConfig cookies = request.getServletContext().getSessionCookieConfig();
81-
out.println("Found cookie: " + cookies.getName());
79+
80+
if (request.getCookies() != null) {
81+
for (Cookie cookie : request.getCookies()) {
82+
out.println("Found cookie: " + cookie.getName());
83+
}
84+
}
8285

86+
// General cookie
8387
Cookie cookie = new Cookie("myCookieKey", "myCookieValue");
8488
cookie.setMaxAge(60);
8589
response.addCookie(cookie);
90+
8691
out.println("<br><br>Set a new cookie");
8792

93+
// Http only cookie
8894
cookie = new Cookie("myHttpOnlyCookieKey", "myHttpOnlyCookieValue");
8995
cookie.setHttpOnly(true);
9096
cookie.setMaxAge(60);
9197
response.addCookie(cookie);
98+
9299
out.println("<br>Set a new HTTPOnly Cookie<br><br>");
93100
out.println("Check what cookies are visible by");
94-
out.println("<a href=\"http://"
95-
+ request.getServerName()
96-
+ ":"
97-
+ request.getServerPort()
98-
+ request.getContextPath()
99-
+ "/index-cookies.jsp\">clicking here</a>");
101+
out.println("<a href=\"http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath()
102+
+ "/index-cookies.jsp\">clicking here</a>");
100103

101104
out.println("</body>");
102105
out.println("</html>");
103106
}
104107
}
105108

106-
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
107109
/**
108110
* Handles the HTTP <code>GET</code> method.
109111
*
@@ -113,8 +115,7 @@ protected void processRequest(HttpServletRequest request, HttpServletResponse re
113115
* @throws IOException if an I/O error occurs
114116
*/
115117
@Override
116-
protected void doGet(HttpServletRequest request, HttpServletResponse response)
117-
throws ServletException, IOException {
118+
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
118119
processRequest(request, response);
119120
}
120121

@@ -127,8 +128,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
127128
* @throws IOException if an I/O error occurs
128129
*/
129130
@Override
130-
protected void doPost(HttpServletRequest request, HttpServletResponse response)
131-
throws ServletException, IOException {
131+
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
132132
processRequest(request, response);
133133
}
134134

@@ -140,6 +140,6 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
140140
@Override
141141
public String getServletInfo() {
142142
return "Short description";
143-
}// </editor-fold>
143+
}
144144

145145
}

0 commit comments

Comments
 (0)