1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package com.acme;
20 import java.io.IOException;
21 import java.io.OutputStreamWriter;
22 import java.io.PrintWriter;
23
24 import javax.servlet.ServletConfig;
25 import javax.servlet.ServletException;
26 import javax.servlet.http.HttpServlet;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
30 import org.eclipse.jetty.util.StringUtil;
31
32
33
34
35
36
37
38
39
40 public class RegTest extends HttpServlet
41 {
42
43
44 @Override
45 public void init(ServletConfig config) throws ServletException
46 {
47 super.init(config);
48 }
49
50
51 @Override
52 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
53 {
54 doGet(request, response);
55 }
56
57
58 @Override
59 public void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException
60 {
61 request.setCharacterEncoding("UTF-8");
62 PrintWriter pout=null;
63
64 try
65 {
66 pout =response.getWriter();
67 }
68 catch(IllegalStateException e)
69 {
70 pout=new PrintWriter(new OutputStreamWriter(response.getOutputStream(),"UTF-8"));
71 }
72
73 try
74 {
75 pout.write("<html>\n<body>\n");
76 pout.write("<h1>Rego Servlet</h1>\n");
77 pout.write("<table width=\"95%\">");
78 pout.write("<tr>\n");
79 pout.write("<th align=\"right\">getMethod: </th>");
80 pout.write("<td>" + notag(request.getMethod())+"</td>");
81 pout.write("</tr><tr>\n");
82 pout.write("<th align=\"right\">getContentLength: </th>");
83 pout.write("<td>"+Integer.toString(request.getContentLength())+"</td>");
84 pout.write("</tr><tr>\n");
85 pout.write("<th align=\"right\">getContentType: </th>");
86 pout.write("<td>"+notag(request.getContentType())+"</td>");
87 pout.write("</tr><tr>\n");
88 pout.write("<th align=\"right\">getRequestURI: </th>");
89 pout.write("<td>"+notag(request.getRequestURI())+"</td>");
90 pout.write("</tr><tr>\n");
91 pout.write("<th align=\"right\">getRequestURL: </th>");
92 pout.write("<td>"+notag(request.getRequestURL().toString())+"</td>");
93 pout.write("</tr><tr>\n");
94 pout.write("<th align=\"right\">getContextPath: </th>");
95 pout.write("<td>"+request.getContextPath()+"</td>");
96 pout.write("</tr><tr>\n");
97 pout.write("<th align=\"right\">getServletPath: </th>");
98 pout.write("<td>"+notag(request.getServletPath())+"</td>");
99 pout.write("</tr><tr>\n");
100 pout.write("<th align=\"right\">getPathInfo: </th>");
101 pout.write("<td>"+notag(request.getPathInfo())+"</td>");
102 pout.write("</tr><tr>\n");
103 pout.write("<th align=\"right\">getPathTranslated: </th>");
104 pout.write("<td>"+notag(request.getPathTranslated())+"</td>");
105 pout.write("</tr><tr>\n");
106 pout.write("<th align=\"right\">getQueryString: </th>");
107 pout.write("<td>"+notag(request.getQueryString())+"</td>");
108 pout.write("</tr><tr>\n");
109
110 pout.write("<th align=\"right\">getProtocol: </th>");
111 pout.write("<td>"+request.getProtocol()+"</td>");
112 pout.write("</tr><tr>\n");
113 pout.write("<th align=\"right\">getScheme: </th>");
114 pout.write("<td>"+request.getScheme()+"</td>");
115 pout.write("</tr><tr>\n");
116 pout.write("<th align=\"right\">getServerName: </th>");
117 pout.write("<td>"+notag(request.getServerName())+"</td>");
118 pout.write("</tr><tr>\n");
119 pout.write("<th align=\"right\">getServerPort: </th>");
120 pout.write("<td>"+Integer.toString(request.getServerPort())+"</td>");
121 pout.write("</tr><tr>\n");
122 pout.write("<th align=\"right\">getLocalName: </th>");
123 pout.write("<td>"+request.getLocalName()+"</td>");
124 pout.write("</tr><tr>\n");
125 pout.write("<th align=\"right\">getLocalAddr: </th>");
126 pout.write("<td>"+request.getLocalAddr()+"</td>");
127 pout.write("</tr><tr>\n");
128 pout.write("<th align=\"right\">getLocalPort: </th>");
129 pout.write("<td>"+Integer.toString(request.getLocalPort())+"</td>");
130 pout.write("</tr><tr>\n");
131 pout.write("<th align=\"right\">getRemoteUser: </th>");
132 pout.write("<td>"+request.getRemoteUser()+"</td>");
133 pout.write("</tr><tr>\n");
134 pout.write("<th align=\"right\">getUserPrincipal: </th>");
135 pout.write("<td>"+request.getUserPrincipal()+"</td>");
136 pout.write("</tr><tr>\n");
137 pout.write("<th align=\"right\">getRemoteAddr: </th>");
138 pout.write("<td>"+request.getRemoteAddr()+"</td>");
139 pout.write("</tr><tr>\n");
140 pout.write("<th align=\"right\">getRemoteHost: </th>");
141 pout.write("<td>"+request.getRemoteHost()+"</td>");
142 pout.write("</tr><tr>\n");
143 pout.write("<th align=\"right\">getRemotePort: </th>");
144 pout.write("<td>"+request.getRemotePort()+"</td>");
145 pout.write("</tr><tr>\n");
146 pout.write("<th align=\"right\">getRequestedSessionId: </th>");
147 pout.write("<td>"+request.getRequestedSessionId()+"</td>");
148 pout.write("</tr><tr>\n");
149 pout.write("<th align=\"right\">isSecure(): </th>");
150 pout.write("<td>"+request.isSecure()+"</td>");
151
152 pout.write("</tr><tr>\n");
153 pout.write("<th align=\"right\">isUserInRole(admin): </th>");
154 pout.write("<td>"+request.isUserInRole("admin")+"</td>");
155
156 pout.write("</tr></table>");
157
158 }
159 catch (Exception e)
160 {
161 getServletContext().log("dump "+e);
162 }
163
164
165 pout.write("</body>\n</html>\n");
166
167 pout.close();
168 }
169
170
171
172 @Override
173 public String getServletInfo()
174 {
175 return "Rego Servlet";
176 }
177
178
179 @Override
180 public synchronized void destroy()
181 {
182 }
183
184
185 private String notag(String s)
186 {
187 if (s==null)
188 return "null";
189 s=StringUtil.replace(s,"&","&");
190 s=StringUtil.replace(s,"<","<");
191 s=StringUtil.replace(s,">",">");
192 return s;
193 }
194 }