Java by API/javax.servlet/GenericServlet
Содержание
extends GenericServlet
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;
}
}
GenericServlet: getInitParameterNames()
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class GetInitParams extends GenericServlet {
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
out.println("Init Parameters:");
Enumeration e = getInitParameterNames();
while (e.hasMoreElements()) {
String name = (String) e.nextElement();
out.println(name + ": " + getInitParameter(name));
}
}
}
GenericServlet: getInitParameter(String arg0)
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 MainServlets extends HttpServlet {
int count;
public void init() throws ServletException {
String initial = getInitParameter("initial");
try {
count = Integer.parseInt(initial);
} catch (NumberFormatException e) {
count = 0;
}
}
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,
IOException {
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
count++;
out.println("Since loading (and with a possible initialization");
out.println("parameter figured in), this servlet has been accessed");
out.println(count + " times.");
}
}
GenericServlet: getServletContext()
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.GenericServlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class ServerSnoop extends GenericServlet {
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
ServletContext context = getServletContext();
out.println("req.getServerName(): " + req.getServerName());
out.println("req.getServerPort(): " + req.getServerPort());
out.println("context.getServerInfo(): " + context.getServerInfo());
out.println("getServerInfo() name: " + getServerInfoName(context.getServerInfo()));
out.println("getServerInfo() version: " + getServerInfoVersion(context.getServerInfo()));
out.println("context.getAttributeNames():");
Enumeration e = context.getAttributeNames();
while (e.hasMoreElements()) {
String name = (String) e.nextElement();
out.println(" context.getAttribute(\"" + name + "\"): " + context.getAttribute(name));
}
}
private String getServerInfoName(String serverInfo) {
int slash = serverInfo.indexOf("/");
if (slash == -1)
return serverInfo;
else
return serverInfo.substring(0, slash);
}
private String getServerInfoVersion(String serverInfo) {
// Version info is everything between the slash and the space
int slash = serverInfo.indexOf("/");
if (slash == -1)
return null;
int space = serverInfo.indexOf(" ", slash);
if (space == -1)
space = serverInfo.length();
return serverInfo.substring(slash + 1, space);
}
}
GenericServlet: init()
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 MainServlets extends HttpServlet {
int count;
public void init() throws ServletException {
String initial = getInitParameter("initial");
try {
count = Integer.parseInt(initial);
} catch (NumberFormatException e) {
count = 0;
}
}
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,
IOException {
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
count++;
out.println("Since loading (and with a possible initialization");
out.println("parameter figured in), this servlet has been accessed");
out.println(count + " times.");
}
}