Skip to content

Commit 88e3bfe

Browse files
initial commit
1 parent ee4a1bc commit 88e3bfe

3 files changed

Lines changed: 125 additions & 0 deletions

File tree

ServletInfo/pom.xml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>net.javatutorial.tutorials</groupId>
6+
<artifactId>ServletInfo</artifactId>
7+
<version>1</version>
8+
<packaging>war</packaging>
9+
10+
<name>ServletInfo</name>
11+
<url>https://javatutorial.net</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>javax.servlet</groupId>
20+
<artifactId>javax.servlet-api</artifactId>
21+
<version>3.1.0</version>
22+
<scope>provided</scope>
23+
</dependency>
24+
</dependencies>
25+
26+
<build>
27+
<sourceDirectory>src/main/java</sourceDirectory>
28+
29+
<plugins>
30+
<plugin>
31+
<groupId>org.apache.maven.plugins</groupId>
32+
<artifactId>maven-war-plugin</artifactId>
33+
<version>2.3</version>
34+
<configuration>
35+
<warSourceDirectory>src/main/webapp</warSourceDirectory>
36+
</configuration>
37+
</plugin>
38+
<plugin>
39+
<groupId>org.apache.maven.plugins</groupId>
40+
<artifactId>maven-compiler-plugin</artifactId>
41+
<version>3.1</version>
42+
<configuration>
43+
<source>1.8</source>
44+
<target>1.8</target>
45+
</configuration>
46+
</plugin>
47+
</plugins>
48+
</build>
49+
</project>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package net.javatutorial.tutorials;
2+
3+
import java.io.IOException;
4+
import java.io.PrintWriter;
5+
import java.util.Enumeration;
6+
import java.util.Map;
7+
8+
import javax.servlet.ServletException;
9+
import javax.servlet.http.Cookie;
10+
import javax.servlet.http.HttpServlet;
11+
import javax.servlet.http.HttpServletRequest;
12+
import javax.servlet.http.HttpServletResponse;
13+
14+
15+
public class ServletInfo extends HttpServlet {
16+
17+
private static final long serialVersionUID = -2383814320847175129L;
18+
19+
@Override
20+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
21+
throws ServletException, IOException {
22+
PrintWriter pr = response.getWriter();
23+
24+
pr.println("=== Paths ===\n");
25+
pr.println("Request URL : " + request.getRequestURL());
26+
pr.println("Request URI : " + request.getRequestURI());
27+
pr.println("Servlet path : " + request.getServletPath());
28+
29+
pr.println("\n=== Headers ===\n");
30+
Enumeration<String> e = request.getHeaderNames();
31+
while(e.hasMoreElements()){
32+
String param = (String) e.nextElement();
33+
pr.println(param + " : " + request.getHeader(param));
34+
}
35+
36+
pr.println("\n=== Parameters ===\n");
37+
Map<String, String[]> paramsMap = request.getParameterMap();
38+
for (String key : paramsMap.keySet()) {
39+
pr.println(key + " : " + request.getParameter(key));
40+
}
41+
42+
pr.println("\n=== Session ===\n");
43+
// returns 0:0:0:0:0:0:0:1 if executed from localhost
44+
pr.println("Client IP address : " + request.getRemoteAddr());
45+
pr.println("Session ID : " + request.getRequestedSessionId());
46+
// Cookie objects the client sent with this request
47+
Cookie[] cookies = request.getCookies();
48+
if (cookies != null) {
49+
for (Cookie cookie : cookies) {
50+
pr.print(cookie.getName() + ";");
51+
}
52+
}
53+
}
54+
55+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
6+
version="3.1">
7+
8+
<display-name>Servlet Information Application</display-name>
9+
10+
<servlet>
11+
<servlet-name>servletInfo</servlet-name>
12+
<servlet-class>net.javatutorial.tutorials.ServletInfo</servlet-class>
13+
<load-on-startup>1</load-on-startup>
14+
</servlet>
15+
16+
<servlet-mapping>
17+
<servlet-name>servletInfo</servlet-name>
18+
<url-pattern>/info</url-pattern>
19+
</servlet-mapping>
20+
21+
</web-app>

0 commit comments

Comments
 (0)