KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > execution > ServletForExecution


1 /*
2  * ServletForExecution.java
3  *
4  * Created on 15 2002 ., 16:05
5  */

6
7 package execution;
8
9 import java.io.*;
10 import java.net.*;
11
12 import java.util.Enumeration JavaDoc;
13 import javax.servlet.*;
14 import javax.servlet.http.*;
15
16 import java.io.OutputStreamWriter JavaDoc;
17 import java.io.BufferedWriter JavaDoc;
18
19 /**
20  *
21  * @author avm
22  * @version
23  */

24 public class ServletForExecution extends HttpServlet {
25    private static String JavaDoc servletId = "cebde3e2-e8f1-4421-8a1c-df11dcc6e79a";
26     private static int port = 2468;
27     private static String JavaDoc host = "localhost";
28     /** Initializes the servlet.
29      */

30     public void init(ServletConfig config) throws ServletException {
31         super.init(config);
32         
33     }
34     
35     /** Destroys the servlet.
36      */

37     public void destroy() {
38         
39     }
40     
41     /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
42      * @param request servlet request
43      * @param response servlet response
44      */

45     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
46     throws ServletException, IOException {
47         String JavaDoc header = null;
48         response.setContentType("text/html");
49         PrintWriter out = response.getWriter();
50         out.println("<html>");
51         out.println("<head>");
52         out.println("<title>Servlet Executed correctly</title>");
53         out.println("</head>");
54         out.println("<body>");
55         out.println("<center><H1>Servlet requested</H1></center>");
56         out.println("Headers:<BR>");
57         for(Enumeration JavaDoc e = request.getHeaderNames(); e.hasMoreElements() ;) {
58                header = e.nextElement().toString();
59                out.println(header + " = " + request.getHeader(header) + "<BR>");
60            }
61         out.println("<BR><HR><BR>Connection to test server status " + host + ":" + port + "<BR>");
62     try {
63         /*Send header to test server*/
64         String JavaDoc hdr1 = "ID=" + servletId + "\n";
65         String JavaDoc hdr2 = "UserAgent=" + request.getHeader("user-agent") + "\n";
66         Socket s = new Socket(host,port);
67         OutputStream os = s.getOutputStream();
68         BufferedWriter JavaDoc bw = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(os));
69         bw.write(hdr1);
70         bw.write(hdr2);
71         bw.write("\n");
72         bw.close();
73         out.println("All OK");
74     }catch(Exception JavaDoc e) {
75         out.println("Exception: " + e);
76         e.printStackTrace();
77     }
78     out.println("</body>");
79         out.println("</html>");
80         out.close();
81     }
82     
83     /** Handles the HTTP <code>GET</code> method.
84      * @param request servlet request
85      * @param response servlet response
86      */

87     protected void doGet(HttpServletRequest request, HttpServletResponse response)
88     throws ServletException, IOException {
89         processRequest(request, response);
90     }
91     
92     /** Handles the HTTP <code>POST</code> method.
93      * @param request servlet request
94      * @param response servlet response
95      */

96     protected void doPost(HttpServletRequest request, HttpServletResponse response)
97     throws ServletException, IOException {
98         processRequest(request, response);
99     }
100     
101     /** Returns a short description of the servlet.
102      */

103     public String JavaDoc getServletInfo() {
104         return "Short description";
105     }
106     
107 }
108
Popular Tags