1 20 package org.apache.cactus.sample.servlet; 21 22 import java.io.IOException ; 23 import java.io.PrintWriter ; 24 25 import java.util.Hashtable ; 26 27 import javax.servlet.RequestDispatcher ; 28 import javax.servlet.ServletConfig ; 29 import javax.servlet.ServletException ; 30 import javax.servlet.http.Cookie ; 31 import javax.servlet.http.HttpServlet ; 32 import javax.servlet.http.HttpServletRequest ; 33 import javax.servlet.http.HttpServletResponse ; 34 import javax.servlet.http.HttpSession ; 35 36 44 public class SampleServlet extends HttpServlet 45 { 46 56 public void doGet(HttpServletRequest theRequest, 57 HttpServletResponse theResponse) throws IOException 58 { 59 PrintWriter pw = theResponse.getWriter(); 60 61 theResponse.setContentType("text/html"); 62 63 pw.print("<html><head/><body>"); 64 pw.print("A GET request"); 65 pw.print("</body></html>"); 66 } 67 68 77 public String checkMethod(HttpServletRequest theRequest) 78 { 79 return theRequest.getMethod(); 80 } 81 82 88 public void setSessionVariable(HttpServletRequest theRequest) 89 { 90 HttpSession session = theRequest.getSession(false); 91 92 session.setAttribute("name_setSessionVariable", 93 "value_setSessionVariable"); 94 } 95 96 101 public void setRequestAttribute(HttpServletRequest theRequest) 102 { 103 theRequest.setAttribute("name_setRequestAttribute", 104 "value_setRequestAttribute"); 105 } 106 107 113 public Hashtable getRequestParameters(HttpServletRequest theRequest) 114 { 115 Hashtable params = new Hashtable (); 116 117 params.put("param1", theRequest.getParameter("param1")); 118 params.put("param2", theRequest.getParameter("param2")); 119 120 return params; 121 } 122 123 129 public String getRequestHeader(HttpServletRequest theRequest) 130 { 131 return theRequest.getHeader("testheader"); 132 } 133 134 139 public Hashtable getRequestCookies(HttpServletRequest theRequest) 140 { 141 Hashtable allCookies = new Hashtable (); 142 143 Cookie [] cookies = theRequest.getCookies(); 144 145 if (cookies != null) 146 { 147 for (int i = 0; i < cookies.length; i++) 148 { 149 Cookie cookie = cookies[i]; 150 151 allCookies.put(cookie.getName(), cookie.getValue()); 152 } 153 } 154 155 return allCookies; 156 } 157 158 164 public void setResponseHeader(HttpServletResponse theResponse) 165 { 166 theResponse.setHeader("responseheader", "this is a response header"); 167 } 168 169 175 public void setResponseCookie(HttpServletResponse theResponse) 176 { 177 Cookie cookie = new Cookie ("responsecookie", 178 "this is a response cookie"); 179 180 cookie.setDomain("jakarta.apache.org"); 181 theResponse.addCookie(cookie); 182 } 183 184 196 public void doForward(HttpServletRequest theRequest, 197 HttpServletResponse theResponse, ServletConfig theConfig) 198 throws IOException , ServletException 199 { 200 RequestDispatcher rd = 201 theConfig.getServletContext().getRequestDispatcher( 202 "/test/test.jsp"); 203 204 rd.forward(theRequest, theResponse); 205 } 206 207 219 public void doInclude(HttpServletRequest theRequest, 220 HttpServletResponse theResponse, ServletConfig theConfig) 221 throws IOException , ServletException 222 { 223 RequestDispatcher rd = 224 theConfig.getServletContext().getRequestDispatcher( 225 "/test/test.jsp"); 226 227 rd.include(theRequest, theResponse); 228 } 229 } 230 | Popular Tags |