Skip to content

Commit 918630e

Browse files
committed
Improve Server section
1 parent 8e9468a commit 918630e

File tree

1 file changed

+104
-34
lines changed

1 file changed

+104
-34
lines changed

_posts/2015-03-17-server.md

Lines changed: 104 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ date: 2015-03-17 17:34:27
66
order: 30
77
---
88

9+
First of all it's important to say that Pippo is based on __Servlet 3.1__ so, it needs a Servlet container to run properly.
10+
911
Most server-side Java applications (e.g. web or service-oriented) are intended to run within a container.
1012
The traditional way to package these apps for distribution is to bundle them as a WAR file.
1113
Of course you can use the above model for your application development or you can use the simple way.
@@ -19,37 +21,89 @@ public class HelloWorld {
1921

2022
public static void main(String[] args) {
2123
Pippo pippo = new Pippo();
22-
pippo.GET("/", routeContext -> routeContext.send("Hello World!"));
24+
pippo.GET("/", routeContext -> routeContext.send("Hello World!")); // add a route
2325
pippo.start();
2426
}
2527

2628
}
2729
```
2830

31+
Sure, the number of lines can decrease considerable if I use variant:
32+
33+
```java
34+
public class HelloWorld {
35+
36+
public static void main(String[] args) {
37+
Pippo.send("Hello World!");
38+
}
39+
40+
}
41+
```
42+
if you consider that in first variant they are too many lines of code.
43+
2944
You can run _HelloWorld_ class from your IDE (or command line) as a normal (desktop) application.
3045
The `default port` for embedded web server is __8338__ so open your internet browser and type `http://localhost:8338` to
3146
see the result.
3247

33-
You can change some aspects of the embedded web server using `WebServerSettings`:
48+
You can change some aspects (settings) of the embedded web server using:
49+
50+
```java
51+
new Pippo()
52+
.setPort(8081) // change port to 8081
53+
.start();
54+
```
55+
56+
or
3457

3558
```java
3659
Pippo pippo = new Pippo();
37-
pippo.getServer().getSettings().port(8081);
60+
61+
WebServer server = pippo.getServer(); // if you want a fine tuning of the web server
62+
server.getSettings().port(8081);
63+
server.setPippoFilterPath("/pippo/*");
64+
3865
pippo.start();
3966
```
4067

41-
In above snippet I changed programmatically the port to _8081_.
42-
4368
Probably the best approach to specify the server port for an embedded server is via application settings.
4469
Simple set the `server.port` variable from your `src\main\resources\main\conf\application.properties` with your value:
4570

46-
```
71+
```properties
4772
# Control the port that Pippo binds
4873
server.port = 8081
4974
```
5075

51-
Pippo detects automatically the template engine using __ServiceLoader__.
52-
Pippo comes (out of the box) with some web servers:
76+
Using the same `application.properties` you can change other server parameters like
77+
78+
```properties
79+
# Control the network ip address Pippo binds
80+
# Specify 0.0.0.0 for all available interfaces
81+
server.host = localhost
82+
83+
# Specify the context path of the application
84+
server.contextPath = /
85+
86+
# HTTPS
87+
#server.keystoreFile =
88+
#server.keystorePassword =
89+
#server.truststoreFile =
90+
```
91+
92+
By default, Pippo use context path `/` (root) but you can change it using
93+
- `server.contextPath` property
94+
- programmatically
95+
```java
96+
new Pippo()
97+
.setPort(8081)
98+
.setFilterPath("/pippo/*") // <<<
99+
.start();
100+
```
101+
102+
Pippo detects automatically the web server using __ServiceLoader__.
103+
If you want to use a web server in your application, you must add pippo-<server name> as dependency for your project.
104+
Other option is to set programmatically the desired web server using `Pippo#setServer(WebServer server)`.
105+
106+
Pippo comes (out of the box) with some web servers (most popular):
53107

54108
- [Jetty](http://eclipse.org/jetty) `pippo-jetty`
55109
- [Undertow](http://undertow.io) `pippo-undertow`
@@ -58,35 +112,24 @@ Pippo comes (out of the box) with some web servers:
58112

59113
To use one of these servers just add a dependency in your project:
60114

61-
```
115+
```xml
62116
<dependency>
63-
<groupId>ro.pippo</groupId>
64-
<artifactId>pippo-jetty</artifactId>
65-
<version>${pippo.version}</version>
117+
<groupId>ro.pippo</groupId>
118+
<artifactId>pippo-jetty</artifactId>
119+
<version>${pippo.version}</version>
66120
</dependency>
67121
```
68122

69-
If you need to create support for another embedded web server that is not implemented in Pippo or third-party modules
70-
than all you need to do is to implement [WebServer]({{ site.coreurl }}/src/main/java/ro/pippo/core/WebServer.java) or to extends [AbstractWebServer]({{ site.coreurl }}/src/main/java/ro/pippo/core/AbstractWebServer.java).
71-
72-
If you want to make your embedded server plugable for Pippo than you must add `@MetaInfServices(WebServer.class)` annotation to
73-
your class
74-
75-
```java
76-
@MetaInfServices(WebServer.class)
77-
public class JettyServer extends AbstractWebServer<JettySettings> {
123+
In Pippo, the web servers are plug and play and interchangeable (you can change the web server without to change your code).
124+
You never work directly with classes from Jetty, Tomcat or other web server.
78125

79-
// attributes, methods, ...
80-
81-
}
82-
```
83-
84-
Are some situations when the settings provided by an `WebServer` instance are not enough for you.
126+
Are some situations (very rare) when the behavior provided by an `WebServer` instance supplied automatically by Pippo is not perfect.
127+
You want to have access to some advanced settings.
85128
In these situations you need to create a custom WebServer.
86129
The idea is to create a custom WebServer if you want to override some aspects (methods) of that server or
87130
if you want free access to the servlet container (Jetty, Tomcat, ...).
88131

89-
Show below the code for a `JettyServer` with persistent sessions
132+
Show below an concrete example of custom `JettyServer` with persistent sessions
90133

91134
```java
92135
public class MyJettyServer extends JettyServer {
@@ -114,15 +157,27 @@ public class MyJettyServer extends JettyServer {
114157
public class Main {
115158

116159
public static void main(String[] args) {
160+
// the routes are added via MyApplication class
117161
new Pippo(new MyApplication()).setServer(new MyJettyServer()).start();
118162
}
119163

120164
}
121165
```
122166

123-
The `WebServer` abstraction allows you to add a `Servlet`, `Filter` or `ServletContextListener` in any supported servlet container
124-
via `addListener(Class<? extends ServletContextListener> listener)` method.
125-
For the same task but more modular you can use `WebServerInitializer`.
167+
The `WebServer` abstraction allows you to add a `Servlet` or a `Filter`.
168+
Also, `WebServer` allows you to add servlet listeners in any supported servlet container
169+
via `addListener(Class<? extends EventListener> listener)` method.
170+
The Servlet's listener supported are (the list is not complete):
171+
- [javax.servlet.ServletContextListener](http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContextListener.html)
172+
- [javax.servlet.ServletContextAttributeListener](http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContextAttributeListener.html)
173+
- [javax.servlet.ServletRequestListener](http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequestListener.html)
174+
- [javax.servlet.ServletRequestAttributeListener](http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequestAttributeListener.html)
175+
- [javax.servlet.http.HttpSessionListener](http://docs.oracle.com/javaee/6/api/javax/servlet/http.HttpSessionListener.html)
176+
- [javax.servlet.http.HttpSessionBindingListener](http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpSessionBindingListener.html)
177+
- [javax.servlet.http.HttpSessionAttributeListener](http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpSessionAttributeListener.html)
178+
- [javax.servlet.http.HttpSessionActivationListener](http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpSessionActivationListener.html)
179+
180+
For the same task but using a more generic approach you can use `WebServerInitializer`.
126181
Below I present you a code snippet that add a servlet to Pippo:
127182

128183
``` java
@@ -178,8 +233,23 @@ public class ServletDemo {
178233

179234
}
180235
```
181-
DON'T forget to add `@MetaInfServices` on your implementation of `WebServerInitializer`!
236+
__DON'T__ forget to add `@MetaInfServices` on your implementation of `WebServerInitializer`!
182237
The full code is available in [pippo-demo-servlet]({{ site.demourl }}/pippo-demo-servlet) project.
183238

184-
A more complex demo project that shows you how to integrate [Jersey](https://jersey.java.net) is available in [pippo-demo-jersey]({{ site.demourl }}/pippo-demo-jersey) project.
185-
This demo is the code source of the article `Pippo and Jersey (JAX-RS): A Match Made in Heaven` that is availables on [DZone](https://dzone.com/articles/pippo-and-jersey-jax-rs).
239+
If you need to create support for another embedded web server that is not implemented in Pippo or third-party modules
240+
than all you need to do is to implement [WebServer]({{ site.coreurl }}/src/main/java/ro/pippo/core/WebServer.java) or to extends [AbstractWebServer]({{ site.coreurl }}/src/main/java/ro/pippo/core/AbstractWebServer.java).
241+
242+
If you want to make your embedded server plugable for Pippo than you must add `@MetaInfServices(WebServer.class)` annotation to
243+
your class
244+
245+
```java
246+
@MetaInfServices(WebServer.class)
247+
public class JettyServer extends AbstractWebServer<JettySettings> {
248+
249+
// attributes, methods, ...
250+
251+
}
252+
```
253+
254+
A more complex demo project that shows you how to integrate [Jersey](https://jersey.java.net) is available in [pippo-demo-jersey]({{ site.demourl }}/pippo-demo-jersey) project.
255+
This demo is the code source of the article `Pippo and Jersey (JAX-RS): A Match Made in Heaven` that is availables on [DZone](https://dzone.com/articles/pippo-and-jersey-jax-rs).

0 commit comments

Comments
 (0)