/*
* TestSunIdentificationServlet - Verify Sun's id in response header
* Also test the ServletOutputStream methods
*/
package servlet_tests;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.*;
public class TestSunIdentificationServlet extends HttpServlet{
private ServletContext context;
private ServletOutputStream out;
private String host;
private int port;
public void init(ServletConfig config) throws ServletException {
super.init(config);
System.out.println("[TestSunIdentificationServlet.init]");
context = config.getServletContext();
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
System.out.println("[TestSunIdentificationServlet.doGet]");
doPost(request, response);
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
System.out.println("[TestSunIdentificationServlet.doPost]");
response.setContentType("text/html");
out = response.getOutputStream();
HttpSession session = request.getSession();
out.print("");
out.println();
out.print("
");
boolean containsHeader = response.containsHeader("Server");
HttpServletResponseWrapper respWrapper =
new HttpServletResponseWrapper(response);
if (containsHeader != respWrapper.containsHeader("Server")) {
out.println("ERROR: resp.containsHeader() != respWrapper"
+ ".containsHeader()");
}
String s = new String("print(boolean) = ");
out.print(s);
out.print(containsHeader);
out.println("
");
out.print("println(boolean) = ");
out.println(containsHeader);
out.println("
");
int i1 = 2003;
out.print("print(int) = ");
out.print(i1);
out.println("
");
out.print("println(int) = ");
out.println(i1);
out.println("
");
long l1 = 200000033l;
out.print("print(long) = ");
out.print(l1);
out.println("
");
out.print("println(long) = ");
out.println(l1);
out.println("
");
float f1 = 22.33f;
out.print("print(float) = ");
out.print(f1);
out.println("
");
out.print("println(float) = ");
out.println(f1);
out.println("
");
double d1 = 3333333333.44;
out.print("print(double) = ");
out.print(d1);
out.println("
");
out.print("println(double) = ");
out.println(d1);
out.println("
");
if (session != null) {
host = request.getServerName();
port = request.getServerPort();
// Invoke TestCookieProperties.jsp and look at Server: response
// header.
invokeJsp();
}
out.println("");
out.println("");
out.flush();
}
private void invokeJsp() throws IOException {
try {
Socket sock = new Socket(host, new Integer(port).intValue());
OutputStream os = sock.getOutputStream();
String get = "GET " + "/" + context.getServletContextName() +
"/jsp/TestCookieProperties.jsp" + " HTTP/1.0\n";
os.write(get.getBytes());
os.write("\n".getBytes());
InputStream is = sock.getInputStream();
BufferedReader bis = new BufferedReader(new InputStreamReader(is));
String line = null;
while ((line = bis.readLine()) != null) {
if (line.startsWith("Server:")) {
break;
}
}
if (line != null) {
System.out.println("[TestSunIdentificationServlet.invokeJsp] "
+ line);
out.print("\"Server:\" response header has the followings: ");
out.println(line.substring("Server:".length() + 1,
line.length()) + "
");
} else {
out.println("Error: Missing Server: response header
");
}
} catch (Exception e) {
out.println("Exception : " + e + "
");
}
}
}