DiagServer.java examples7/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45:

import java.io.*; import java.net.*; import sun.net.NetworkServer; /** * A program that echoes to standard output an HTTP * request, whether GET or POST. * * Usage: java DiagServer [port] * * If you spawn the server on a port already in use, you will be informed * "Server failed to start: java.net.BindException: Permission denied". * Note that the server’s default choice of ports is 80. * * Excerpted from * http://hotwired.lycos.com/webmonkey/99/36/index3a_page4.html?tw=backend. */ public class DiagServer extends NetworkServer { public static void main(String[] args) { int port = 80; try { port = Integer.parseInt(args[0]); } catch (Exception e) { //You might want to catch a NumberFormatException here, //or do some more sophisticated command line parsing. } try { DiagServer ds = new DiagServer(); ds.startServer(port); System.err.println("DiagServer started on port " + port); System.err.println(""); } catch (IOException ioe) { System.err.println("Server failed to start: " + ioe); } } //main public void serviceRequest() throws IOException { int byteCount = 0; while (clientIsOpen() && (byteCount <1000)) { byte b = (byte) clientInput.read();

1/2

DiagServer.java examples7/ 46: byteCount++; 47: System.out.write(b); 48: System.out.flush(); 49: } 50: } //serviceRequest 51: }

2/2

get.html

1/1

examples7/ 1: 2: .dtd"> 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:


CSCI E-259
foo:
bar: 0 1
baz:
qux:
quux:



http.cgi examples7/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37:

#!/usr/local/bin/perl # # http.cgi # # A Perl script that echoes the content of an HTTP request. # # Based on Steven E. Brenner’s cgi-lib.pl, available at # http://cgi-lib.berkeley.edu/. # # get request’s content type $type = $ENV{’CONTENT_TYPE’}; # get request’s content length $len = $ENV{’CONTENT_LENGTH’}; # get request’s request method $meth = $ENV{’REQUEST_METHOD’}; # read request if (!defined $meth || $meth eq ’’) { $in = $ENV{’QUERY_STRING’}; } elsif($meth eq ’GET’ || $meth eq ’HEAD’) { $in = $ENV{’QUERY_STRING’}; } elsif ($meth eq ’POST’) { if (($got = read(STDIN, $in, $len) != $len)) { die("Short Read: wanted $len, got $got\n"); } } else { die("Unknown request method: $meth\n"); } # echo content of request print <
1/1

post.html

1/1

examples7/ 1: 2: .dtd"> 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:


CSCI E-259
foo:
bar: 0 1
baz:
qux:
quux:



server.xml

1/1

examples7/base/conf/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41:



web.xml

1/3

examples7/base/conf/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45:

default org.apache.catalina.servlets.DefaultServlet debug 0 listings true 1 jsp org.apache.jasper.servlet.JspServlet fork false xpoweredBy false 3 default

web.xml

2/3

examples7/base/conf/ 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90:

/
jsp *.jsp 0 css text/css dtd application/xml-dtd gif image/gif htm text/html html text/html jpg image/jpeg js text/javascript pdf

web.xml examples7/base/conf/ 91: application/pdf 92:
93: 94: png 95: image/png 96: 97: 98: xml 99: application/xml 100: 101: 102: xsd 103: application/xml 104: 105: 106: xsl 107: application/xml 108: 109: 110: svg 111: image/svg+xml 112: 113: 114: svgz 115: image/svg+xml 116: 117: 118: 119: 120: index.html 121: index.htm 122: index.jsp 123: 124: 125:


3/3

happyenv.jsp

1/1

examples7/base/webapps/ROOT/ 1: 2: 3: .dtd"> 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19:


<% java.io.ByteArrayOutputStream os = new java.io.ByteArrayOutputStream(); %> <% java.io.PrintWriter pw = new java.io.PrintWriter(os); %> <% (new org.apache.xalan.xslt.EnvironmentCheck()).checkEnvironment(pw); %> Environment Happiness Page
 <% out.print(os); %> 


hello1.jsp

1/1

examples7/base/webapps/ROOT/ 1: 2: .dtd"> 3: 4: 5: 6: 7: 8: 9: 10: 11:


<% out.print("hello, world"); %> <% out.print("hello, world"); %>

hello2.jsp

1/1

examples7/base/webapps/ROOT/ 1: 2: 4: 5: 6: 7: ]]> 8: ]]> 9: 10: 11: 12: 13: <jsp:scriptlet> 14: out.print("hello, world"); 15: </jsp:scriptlet> 16: 17: 18: 19: 20: out.print("hello, world"); 21: 22: 23: 24: 25:

xml.jsp examples7/base/webapps/ROOT/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45:

String getDateTimeStr(Locale l) { DateFormat df = SimpleDateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, l); return df.format(new Date()); } Example JSP in XML format This is the output of a simple JSP using XML format.
Use a jsp:scriptlet to loop from 1 to 10:
// Note we need to declare CDATA because we don’t escape the less than symbol
]]>


1/2

xml.jsp examples7/base/webapps/ROOT/ 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57:

Use a jsp:expression to write the date and time in the browser’s locale: getDateTimeStr(request.getLocale())


This sentence is enclosed in a jsp:text element.



2/2

web.xml

1/1

examples7/base/webapps/ROOT/WEB-INF/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41:

hello Hello parameters Parameters visits Visits hello /servlet/hello parameters /servlet/parameters visits /servlet/visits

Hello.java

1/1

examples7/base/webapps/ROOT/WEB-INF/classes/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41:

import java.io.*; import javax.servlet.*; import javax.servlet.http.*; /** * Outputs "hello, world" in XHTML. * * @author Computer Science E-259 * * @throws IOException, ServletException */ public class Hello extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // output XHTML response.setContentType("text/html"); // grab PrintWriter PrintWriter out = response.getWriter(); // output "hello, world" in XHTML out.println(""); out.println(""); out.println(""); out.println(""); out.println("\t"); out.println("\t\tHello World!"); out.println("\t"); out.println("\t"); out.println("\t\thello, world"); out.println("\t"); out.println(""); } }

Parameters.java examples7/base/webapps/ROOT/WEB-INF/classes/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36:

import import import import

java.io.*; java.util.Enumeration; javax.servlet.*; javax.servlet.http.*;

/** * Outputs parameters submitted to it via an HTTP GET. * * @author Computer Science E-259 * * @throws IOException, ServletException */ public class Parameters extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // output plain text response.setContentType("text/plain"); // grab PrintWriter PrintWriter out = response.getWriter(); // grab submitted parameters Enumeration params = request.getParameterNames(); // output each parameter’s name and value while (params.hasMoreElements()) { String param = (String) params.nextElement(); out.println(param + ": " + request.getParameter(param)); } } }

1/1

Visits.java

1/2

examples7/base/webapps/ROOT/WEB-INF/classes/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45:

import import import import import import import

java.io.IOException; java.io.PrintWriter; javax.servlet.ServletException; javax.servlet.http.HttpServlet; javax.servlet.http.HttpSession; javax.servlet.http.HttpServletRequest; javax.servlet.http.HttpServletResponse;

/** * Reports number of times a given user has visited the page in the * current session. * * @author Computer Science E-259 * * @throws IOException, ServletException */ public class Visits extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // output plain text response.setContentType("text/plain"); // grab PrintWriter PrintWriter out = response.getWriter(); // grab user parameter, else err String user = request.getParameter("user"); if (user == null) throw new ServletException("missing user parameter"); // grab current session HttpSession sess = request.getSession(); // grab user’s number of visits from session; if null, // initialize to 0 Integer visits = (Integer) sess.getAttribute(user); if (visits == null) visits = new Integer(0); // update session’s count of user’s number of visits sess.setAttribute(user, new Integer(visits.intValue() + 1));

Visits.java

2/2

examples7/base/webapps/ROOT/WEB-INF/classes/ 46: 47: 48: 49: 50: 51: }

// say hello out.println("Hey, " + user + "! " + "What’s it been, " + visits.intValue() + " time(s)?"); }

DiagServer.java 1/2 - HUIT Sites Hosting

2: 3: www.w3.org/TR/xhtml1/DTD/xhtml1-transitional .dtd">. 4: 5: . 6: <% java.io.ByteArrayOutputStream os = new java.io.ByteArrayOutputStream(); %>. 7: <% java.io.PrintWriter pw ...

20KB Sizes 1 Downloads 344 Views

Recommend Documents

No documents