Response redirection in Servlets

We can redirect the response of the servlet to another application resource.This resource may be another servlet, an HTML page, or a JSP. T...


We can redirect the response of the servlet to another application resource.This resource may be another servlet, an HTML page, or a JSP. The resource (URL) must be available to the calling servlet, in the same servlet context.

There are two forms of response redirection that we discuss:

❑  Sending a standard redirect: Using a response.sendRedirect("myHtml.html")
page sends the HTML page as the response. If this page were another servlet, that servlet does not have access to the original request object, but its response would be sent. To have access to the request object, you must use the request dispatching technique discussed below.

❑ Sending a redirect to an error page: Here, an error code is sent as a parameter of the method, as in response.sendError(response.SC_NO_CONTENT). The error numbers are predefined constants of the response. The defined error page is displayed with the appropriate error message. What this page looks like is dependent on how this feature is configured for the application.Example 1 shows a servlet which redirects its response conditionally to either an error page, using the sendError method, or a standard HTML page, using the sendRedirect method, depending on the contents of the request. This servlet example is called from a form POST in an HTML page, generated by the servlet HTMLFormGeneratorRedirect (subclass of HTMLFormGenerator).

Example 1
package itso.servjsp.servletapi;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HTMLFormHandlerRedirect extends HTMLFormHandler {
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML><TITLE>HTMLFormHandler</TITLE></BODY>");
out.println("<H2>Servlet API Example -
HTMLFormHandlerRedirect</H2><HR>");
out.println("Hi <B>" + req.getParameter("firstname") + ",</B><P>");
String title = req.getParameter("title");
if (title.equals("Web Architect"))
res.sendError(res.SC_BAD_REQUEST, "Sorry, but Web architects can't
see the page");
else res.sendRedirect("HTMLFormHandlerRedirect.html");
out.println("And have worked with the following tools: <BR>");
out.println("</BODY></HTML>");
}}
Name

ADO,131,ASP,3,C++,61,CORE JAVA,1,CSS,115,HTML,297,index,5,JAVASCRIPT,210,OS,47,PHP,65,SAD,53,SERVLETS,23,SOFTWARE ENGINEERING,245,SQL,71,TCP/IP,1,XHTML,9,XML,18,
ltr
item
Best Online Tutorials | Source codes | Programming Languages: Response redirection in Servlets
Response redirection in Servlets
Best Online Tutorials | Source codes | Programming Languages
https://www.1000sourcecodes.com/2012/05/response-redirection-in-servlets.html
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/2012/05/response-redirection-in-servlets.html
true
357226456970214079
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content