KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > helloweb > HelloWebServlet


1 /*
2  * HelloWebServlet.java
3  *
4  */

5
6 package helloweb;
7
8 import java.io.*;
9 import java.net.*;
10
11 import javax.servlet.*;
12 import javax.servlet.http.*;
13
14 /**
15  *
16  * @version
17  */

18 public class HelloWebServlet extends HttpServlet {
19     
20     /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
21      * @param request servlet request
22      * @param response servlet response
23      */

24     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
25     throws ServletException, IOException {
26         response.setContentType("text/html;charset=UTF-8");
27         PrintWriter out = response.getWriter();
28         
29         
30         out.println("<html>");
31         out.println("<head>");
32         out.println("<title>Servlet HelloWebServlet</title>");
33         out.println("</head>");
34         out.println("<body>");
35         out.println("<p>Enter your name:");
36         out.println("<form method=\"get\">");
37         out.println("<input type=\"text\" name=\"name\" size=\"25\">");
38         out.println("<br>");
39         out.println("<p>");
40         out.println("<input type=\"submit\" value=\"Submit\">");
41         out.println("</form>");
42         String JavaDoc name = request.getParameter("name");
43         if ( name != null ) {
44             try {
45                 out.println(getHelloServiceSEIPort().sayHello(name));
46             } catch(java.rmi.RemoteException JavaDoc ex) {
47                 out.println("<p>Caught an exception <p>" + ex);
48             }
49         }
50         out.println("</body>");
51         out.println("</html>");
52         
53         out.close();
54     }
55     
56     // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
57
/** Handles the HTTP <code>GET</code> method.
58      * @param request servlet request
59      * @param response servlet response
60      */

61     protected void doGet(HttpServletRequest request, HttpServletResponse response)
62     throws ServletException, IOException {
63         processRequest(request, response);
64     }
65     
66     /** Handles the HTTP <code>POST</code> method.
67      * @param request servlet request
68      * @param response servlet response
69      */

70     protected void doPost(HttpServletRequest request, HttpServletResponse response)
71     throws ServletException, IOException {
72         processRequest(request, response);
73     }
74     
75     /** Returns a short description of the servlet.
76      */

77     public String JavaDoc getServletInfo() {
78         return "Short description";
79     }
80     // </editor-fold>
81

82     private helloweb.HelloService getHelloService() {
83         helloweb.HelloService helloService = null;
84         try {
85             javax.naming.InitialContext JavaDoc ic = new javax.naming.InitialContext JavaDoc();
86             helloService = (helloweb.HelloService) ic.lookup("java:comp/env/service/HelloService");
87         } catch(javax.naming.NamingException JavaDoc ex) {
88             // TODO handle JNDI naming exception
89
}
90         return helloService;
91     }
92     
93     private helloweb.HelloServiceSEI getHelloServiceSEIPort() {
94         helloweb.HelloServiceSEI helloServiceSEIPort = null;
95         try {
96             helloServiceSEIPort = getHelloService().getHelloServiceSEIPort();
97         } catch(javax.xml.rpc.ServiceException JavaDoc ex) {
98             // TODO handle service exception
99
}
100         return helloServiceSEIPort;
101     }
102 }
103
Popular Tags