List of usage examples for javax.servlet ServletResponse getWriter
public PrintWriter getWriter() throws IOException;
PrintWriter
object that can send character text to the client. From source file:o2.web.HelloWorldServlet.java
@Override public void service(ServletRequest req, ServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub ApplicationContext context = new ClassPathXmlApplicationContext("knights.xml"); Knight knight = (Knight) context.getBean("knight"); knight.embarkOnQuest();/* ww w.jav a2 s.c o m*/ response.setContentType("text/html;charset=utf-8"); response.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); String title = "Webapp Demo"; out.println( "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"); out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\">"); out.println("<head>"); out.println("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" />"); out.println("<title>" + title + "</title>"); out.println("<body>"); out.println("<h1>Hello World!O2</h1>"); out.println("</body>"); out.println("</html>"); }
From source file:org.sakaiproject.nakamura.http.i18n.I18nFilterTest.java
private void writeToResponse(final String key, final boolean useOutputStream) throws IOException, ServletException { doAnswer(new Answer<Object>() { public Object answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); ServletResponse response = (ServletResponse) args[1]; try { if (!useOutputStream) { response.getWriter().write("<html><body>" + key + "</body></html>"); } else { response.getOutputStream() .write(("<html><body>" + key + "</body></html>").getBytes("UTF-8")); }/*from ww w. j a v a2 s . co m*/ } catch (IOException e) { // doesn't matter } return null; } }).when(chain).doFilter(isA(ServletRequest.class), isA(ServletResponse.class)); }
From source file:org.mentawai.util.DebugServletFilter.java
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { PrintWriter writer = res.getWriter(); CharResponseWrapper wrapper = new CharResponseWrapper((HttpServletResponse) res); chain.doFilter(req, wrapper);//from ww w. j av a2 s. c o m writer.println(wrapper.toString()); Object debug = req.getAttribute(DEBUG_KEY); if (debug == null) { HttpSession session = ((HttpServletRequest) req).getSession(); if (session != null) { debug = session.getAttribute(DEBUG_KEY); session.removeAttribute(DEBUG_KEY); } } if (debug != null) { if (COMMENTED) writer.println("\n\n<!-- MENTAWAI DEBUG: Begin \n\n"); else writer.println("<pre>\n\n"); writer.println("- - - - - - - - - - - - - - - - Mentawai DEBUG - - - - - - - - - - - - - - - - \n\n"); writer.println(debug.toString()); writer.println("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \n\n"); if (COMMENTED) writer.println("\n\n" + htmlRedFont + "Mentawai Web Framework - Version:" + ApplicationManager.MENTAWAI_VERSION + " Build:" + ApplicationManager.MENTAWAI_BUILD); else writer.println(htmlRedFont + "<b>Mentawai Web Framework</b> - Version:<b>" + ApplicationManager.MENTAWAI_VERSION + " </b> Build:<b>" + ApplicationManager.MENTAWAI_BUILD + "</b>" + htmlCloseFont); if (COMMENTED) writer.println("\n\n MENTA DEBUG : End -->\n"); else writer.println("</pre>\n"); } }
From source file:wicket.protocol.http.MockServletContext.java
/** * Wicket does not use the RequestDispatcher, so this implementation just * returns a dummy value.// w w w . j a v a 2s.c om * * @param name * The name of the resource to get the dispatcher for * @return The dispatcher */ public RequestDispatcher getRequestDispatcher(final String name) { return new RequestDispatcher() { public void forward(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException { servletResponse.getWriter().write("FORWARD TO RESOURCE: " + name); } public void include(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException { servletResponse.getWriter().write("INCLUDE OF RESOURCE: " + name); } }; }
From source file:org.openmhealth.reference.filter.ExceptionFilter.java
/** * Sends the response to the user.//www .j a v a 2 s. c o m * * @param response * The response object to use to send the response. * * @param statusCode * The status code to send if the response is an HttpServletResponse * object. * * @param message * The message to send to the user or null if no message should be * sent. * * @throws IOException * There was an error sending the response. */ private void sendResponse(final ServletResponse response, final int statusCode, final String message) throws IOException { // If it's a HTTP response, set the status code. if (response instanceof HttpServletResponse) { ((HttpServletResponse) response).setStatus(statusCode); } // Write the message. if (message != null) { response.getWriter().write(message); } }
From source file:net.unit8.zurui.less.filter.ZuruiLessFilter.java
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; String zuruiLess = readZuruiLess(); String lessPath = request.getServletContext().getRealPath(request.getRequestURI()); String requestLess = FileUtils.readFileToString(new File(lessPath), "UTF-8"); LessCompiler lessCompiler = new LessCompiler(); try {//from ww w.j a v a 2 s .c o m String css = lessCompiler.compile(zuruiLess + requestLess); res.getWriter().write(css); } catch (LessException e) { throw new ServletException(e); } }
From source file:org.jetbrains.teamcity.widgets.JsonResponseHelper.java
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { DefaultHttpClient client = new DefaultHttpClient(); String url = servletRequest.getParameter("url"); //HttpGet httpget = new HttpGet("http://buildserver/guestAuth/app/rest/investigations?locator=state:TAKEN"); HttpGet httpget = new HttpGet("http://buildserver/" + url); httpget.setHeader("Accept", "application/json"); HttpResponse response = client.execute(httpget); HttpEntity entity = response.getEntity(); servletResponse.getWriter().write(EntityUtils.toString(entity)); }
From source file:UpperCaseFilter.java
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { ServletResponse newResponse = response; if (request instanceof HttpServletRequest) { newResponse = new CharResponseWrapper((HttpServletResponse) response); }//www . j a v a 2 s . c o m chain.doFilter(request, newResponse); if (newResponse instanceof CharResponseWrapper) { String text = newResponse.toString(); if (text != null) { text = text.toUpperCase(); response.getWriter().write(text); } } }
From source file:org.wso2.carbon.identity.entitlement.filter.EntitlementFilter.java
private void completeAuthorization(String decision, ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws EntitlementFilterException { try {/* w w w. j ava2 s.c om*/ if (decision.equals(EntitlementConstants.PERMIT)) { if (((HttpServletRequest) servletRequest).getRequestURI().contains("/updateCacheAuth.do")) { pepProxy.clear(); log.info("PEP cache has been updated"); servletResponse.getWriter().print("PEP cache has been updated"); } else { filterChain.doFilter(servletRequest, servletResponse); } } else if (decision.equals(EntitlementConstants.DENY)) { log.debug("User not authorized to perform the action"); servletRequest.getRequestDispatcher(authRedirectURL).forward(servletRequest, servletResponse); } else if (decision.equals(EntitlementConstants.NOT_APPLICABLE)) { log.debug("No applicable policies found"); servletRequest.getRequestDispatcher(authRedirectURL).forward(servletRequest, servletResponse); } else if (decision.equals(EntitlementConstants.INDETERMINATE)) { log.debug("Indeterminate"); servletRequest.getRequestDispatcher(authRedirectURL).forward(servletRequest, servletResponse); } else { log.error("Unrecognized decision returned from PDP"); servletRequest.getRequestDispatcher(authRedirectURL).forward(servletRequest, servletResponse); } } catch (Exception e) { log.error("Error occurred while completing authorization", e); throw new EntitlementFilterException("Error occurred while completing authorization", e); } }
From source file:org.ambraproject.cas.filter.GetGuidReturnEmailFilter.java
/** * For the HttpServletRequest Parameter <em>guid</em>, query the user database for that * user's Email Address, write that Email Address to the HttpServletResponse, and then exit. * <p/>/*from w w w . j a v a2s . c o m*/ * Returns <strong>only</strong> the user's Email Address. * <p/> * <strong>Caveat</strong>: Does <strong>not</strong> call <em>FilterChain.doFilter(...)</em>, * but rather simply writes the String (which it hopes is the Email Address) to the Response * and exits. * * @param request The incoming HttpServletRequest from which the Parameter <em>guid</em> is read * @param response The HttpServletResponse to which the results String is written * @param filterChain Passed in, but not used; * <em>FilterChain.doFilter(...)</em> is <strong>never called</strong>. * @throws IOException Unlikely to be thrown except for an infrastructure failure * @throws ServletException Potentially thrown by interactions with <em>request</em> * and <em>response</em>, but never explicitly raised in this method */ public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain filterChain) throws IOException, ServletException { HttpServletResponse httpResponse = (HttpServletResponse) response; if (log.isDebugEnabled()) { log.debug("Received the guid parameter: " + request.getParameter("guid")); } httpResponse.setHeader("Pragma", "no-cache"); httpResponse.setHeader("Cache-Control", "no-store"); httpResponse.setDateHeader("Expires", -1); final PrintWriter writer = response.getWriter(); try { String emailFromGuid = databaseService.getEmailAddressFromGuid(request.getParameter("guid")); if (log.isDebugEnabled()) { log.debug("Now passing back the email address: " + emailFromGuid); } if (emailFromGuid != null) { writer.write(emailFromGuid); } else { writer.write( "Unable to lookup email address from the CAS server. Please contact the system administrator."); } } catch (Exception e) { log.error("Unable to query an email address or to write that email address to ServletResponse." + " Attempted to query an email address for the guid = " + request.getParameter("guid"), e); // TODO: Replace this with some clever logic. This is NOT something to show to the user. writer.write("fake_guid_returned_from_cas"); } // This next command is somewhat unfortunate. A Filter should always invoke "doFilter(...)" at this point, but doing so can break this fragile email lookup. // A typical failure mode is to see the login page in the middle of the Edit User Profile page, where the user's Email Address should be. // The core issue is that one of the other Filters may require authentication, so it calls the "/login" URL and the default behavior // for that URL is to send the user to the Login page. Ambra is querying for a single String (the user's email address), so it takes the String // (which might be the entire Login page) and shows it to the user. // It is, therefore, recommended that Ambra acquire its users' email addresses in a different manner. return; // filterChain.doFilter(httpRequest, response); }