Java by API/javax.servlet/ServletRequest — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 14:39, 31 мая 2010
Содержание
ServletRequest: getRemoteHost()
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 ExportRestriction extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,
IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
// Get the client"s hostname
String remoteHost = req.getRemoteHost();
// See if the client is allowed
if (!isHostAllowed(remoteHost)) {
out.println("Access <BLINK>denied</BLINK>");
} else {
out.println("Access granted");
// Display download links, etc...
}
}
// Disallow hosts ending with .ru
private boolean isHostAllowed(String host) {
return (!host.endsWith(".ru"));
}
}
ServletRequest: getScheme()
import java.io.IOException;
import java.io.PrintWriter;
import java.security.cert.X509Certificate;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class X509Snoop extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,
IOException {
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
X509Certificate[] certs = (X509Certificate[]) req
.getAttribute("javax.servlet.request.X509Certificate");
if (certs != null) {
for (int i = 0; i < certs.length; i++) {
out.println("Client Certificate [" + i + "] = " + certs[i].toString());
}
} else {
if ("https".equals(req.getScheme())) {
out.println("This was an HTTPS request, " + "but no client certificate is available");
} else {
out.println("This was not an HTTPS request, " + "so no client certificate is available");
}
}
}
}
ServletRequest: getServerName()
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class GetKey extends GenericServlet {
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
String key = getInitParameter("key");
String host = req.getServerName();
int port = req.getServerPort();
if (!keyFitsServer(key, host, port)) {
out.println("Pirated!");
} else {
out.println("Valid");
}
}
private boolean keyFitsServer(String key, String host, int port) {
return true;
}
}
ServletRequest.getServerPort()
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class GetKey extends GenericServlet {
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
String key = getInitParameter("key");
String host = req.getServerName();
int port = req.getServerPort();
if (!keyFitsServer(key, host, port)) {
out.println("Pirated!");
} else {
out.println("Valid");
}
}
private boolean keyFitsServer(String key, String host, int port) {
return true;
}
}