SlideShare a Scribd company logo
Click to edit Master title style
Riza Muhammad Nurman ADP
Chapter 3
on
Advanced Programming
FACULTY
Riza Muhammad Nurman
Implementing Inter-Servlet Communication
Click to edit Master title style
Riza Muhammad Nurman ADP
Content
• Implement the request dispatcher mechanism
• Work with filters
Click to edit Master title style
Riza Muhammad Nurman ADP
Navigation Between Servlets
Validation
Servlet
Is the
User
Valid?
NoYes
Welcome
Servlet
Error
Servlet
Click to edit Master title style
Riza Muhammad Nurman ADP
Dispatching a Request
• RequestDispatcher is an object of the
javax.servlet.RequestDispatcher interface that allows inter-servlet
communication
• Once you obtain the RequestDispatcher object, you can perform the
following tasks:
– Forward request to another Web page
– Include content of the calling Web page in the called Web page
RequestDispatcher dispatch = request.getRequestDispatcher("/WelcomeServlet");
dispatch.forward(request, response);
Click to edit Master title style
Riza Muhammad Nurman ADP
Implementasi
Login.java
Login.java
LoginServlet.java
Welcome.java
VALID
INVALID
Click to edit Master title style
Riza Muhammad Nurman ADP
Kode HTML untuk login
1. <html>
2. <head>
3. <title>TODO supply a title</title>
4. <meta charset="UTF-8">
5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
6. <link rel="stylesheet" type="text/css" href="./css/style.css" />
7. </head>
8. <body>
9. <form name="formLogin" method="POST" action="LoginServlet" >
10. <h1>Login</h1>
11. <label><span>User ID: </span> <input type="text" id="userid" name="userid"/></label>
12. <label><span>Password: </span><input type="password" id="pass" name="pass"/></label>
13. <label>
14. <span>&nbsp;</span>
15. <input type="submit" value="Log In" class="button"/><input type="reset" value="Reset"
class="reset"/>
16. </label>
17. </form>
18. </body>
19. </html>
Click to edit Master title style
Riza Muhammad Nurman ADP
Kode HTML login dalam Servlet
1. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
2. throws ServletException, IOException {
3. response.setContentType("text/html;charset=UTF-8");
4. try (PrintWriter out = response.getWriter()) {
5. out.println("<!DOCTYPE html>");
6. out.println("<html>");
7. out.println("<head>");
8. out.println("<title>Servlet Login</title>");
9. out.println("</head>");
10. out.println("<body>");
11. out.println("<form name='formLogin' method='POST' action='LoginServlet'");
12. out.println("<h1>Login</h1>");
13. out.println("<label><span>User ID: </span> <input type='text' id='userid' name='userid'/></label>");
14. out.println("<label><span>Password: </span><input type='password' id='pass'
name='pass'/></label>");
15. out.println("<label>");
16. out.println(" <span>&nbsp;</span>");
17. out.println(" <input type='submit' value='Log In'/><input type='reset' value='Reset'/>");
18. out.println("</label>");
19. out.println("</form>");
20. out.println("</body>");
21. out.println("</html>");
22. }
23. }
Click to edit Master title style
Riza Muhammad Nurman ADP
Code Snippet LoginServlet
1. protected void processRequest(HttpServletRequest request, HttpServletResponse
response)
2. throws ServletException, IOException {
3. response.setContentType("text/html;charset=UTF-8");
4. try (PrintWriter out = response.getWriter()) {
5. String userid = request.getParameter("userid");
6. String password = request.getParameter("pass");
7.
8. if(userid.equals("admin") && password.equals("adminjuga")) {
9. RequestDispatcher dispatch = request.getRequestDispatcher("/Welcome");
10. dispatch.forward(request, response);
11. }
12. else {
13. RequestDispatcher dispatch = request.getRequestDispatcher("/Login");
14. dispatch.include(request, response);
15. out.println("Login Salah");
16. }
17. }
18. }
Click to edit Master title style
Riza Muhammad Nurman ADP
Welcome Servlet
1. protected void processRequest(HttpServletRequest request, HttpServletResponse
response)
2. throws ServletException, IOException {
3. response.setContentType("text/html;charset=UTF-8");
4. try (PrintWriter out = response.getWriter()) {
5. /* TODO output your page here. You may use following sample code. */
6. out.println("<!DOCTYPE html>");
7. out.println("<html>");
8. out.println("<head>");
9. out.println("<title>Servlet Welcome</title>");
10. out.println("</head>");
11. out.println("<body>");
12. out.println("<h1>WELCOME</h1>");
13. out.println("</body>");
14. out.println("</html>");
15. }
16. }
Click to edit Master title style
Riza Muhammad Nurman ADP
Transferring Data
1. * ketik : import javax.servlet.RequestDispatcher; di atas nama class
2. protected void processRequest(HttpServletRequest request, HttpServletResponse
response)
3. throws ServletException, IOException {
4. response.setContentType("text/html;charset=UTF-8");
5. try (PrintWriter out = response.getWriter()) {
6. String userid = request.getParameter("userid");
7. String password = request.getParameter("pass");
8.
9. if(userid.equals("admin") && password.equals("adminjuga")) {
10. RequestDispatcher dispatch = request.getRequestDispatcher("/Welcome");
11. dispatch.forward(request, response);
12. }
13. else {
14. request.setAttribute("result", "Login Salah");
15. RequestDispatcher dispatch = request.getRequestDispatcher("/Login");
16. dispatch.forward(request, response);
17. }
18. }
19. }
Click to edit Master title style
Riza Muhammad Nurman ADP
Transferring Data 2
1. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
2. throws ServletException, IOException {
3. response.setContentType("text/html;charset=UTF-8");
4. try (PrintWriter out = response.getWriter()) {
5. out.println("<!DOCTYPE html>");
6. out.println("<html>");
7. out.println("<head>");
8. out.println("<title>Servlet Login</title>");
9. out.println("</head>");
10. out.println("<body>");
11. out.println("<form name='formLogin' method='POST' action='LoginServlet'");
12. out.println("<h1>Login</h1>");
13. out.println("<label><span>User ID: </span> <input type='text' id='userid' name='userid'/></label>");
14. out.println("<label><span>Password: </span><input type='password' id='pass' name='pass'/></label>");
15. out.println("<label>");
16. out.println(" <span>&nbsp;</span>");
17. out.println(" <input type='submit' value='Log In'/><input type='reset' value='Reset'/>");
18. out.println("</label>");
19. out.println("</form>");
20. String data = (String)request.getAttribute("result");
21. if(data != null)
22. out.println(data);
23. out.println("</body>");
24. out.println("</html>");
25. }
26. }
Click to edit Master title style
Riza Muhammad Nurman ADP
Work With Filters
• Users use different types of client devices, such as desktops, laptops,
tablets, or mobile phone to access the Web applications stored in an
application server
• Each device displays the information in the application according to
the settings in the corresponding Web browser window
• Using servlet filters, you can customize the output of the application
so that it can be viewed in different browsers.
ServletServlet
Servlet
Client
Filter
Request Request
Response Response
Click to edit Master title style
Riza Muhammad Nurman ADP
User Interface On Client Devices
The Interface of KASKUS on the Desktop System
The Interface of KASKUS on the Mobile Phone Screen
Click to edit Master title style
Riza Muhammad Nurman ADP
Introduction to Filters
• A filter is an object that intercepts the requests and responses that
flow between a client and a servlet
– A filter can modify the headers and contents of a request coming from a Web
client and forward it to the target servlet
– A filter can also intercept and manipulate the headers and contents of the
response that is sent back by the servlet
Click to edit Master title style
Riza Muhammad Nurman ADP
IPFilter
1. public class IPFilter implements Filter {
2. String invalidip;
3. …
4. public IPFilter() {
5. }
6.
7. private void doBeforeProcessing(ServletRequest request,
ServletResponse response)
8. throws IOException, ServletException {
9. …
10. }
11. private void doAfterProcessing(ServletRequest request, ServletResponse
response)
12. throws IOException, ServletException {
13. ….
14. }
Click to edit Master title style
Riza Muhammad Nurman ADP
IPFilter 2
1. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
2. if (debug) { log("IPFilter:doFilter()"); }
3. doBeforeProcessing(request, response);
4. Throwable problem = null;
5. try {
6. InetAddress ip = InetAddress.getLocalHost();
7. String addressIP = ip.getHostAddress();
8. if(addressIP.equals(this.invalidip)) {
9. RequestDispatcher rd = request.getRequestDispatcher("Error");
10. rd.forward(request, response);
11. }
12. else {
13. RequestDispatcher rd = request.getRequestDispatcher("Welcome");
14. rd.forward(request, response);
15. }
16. chain.doFilter(request, response);
17. } catch (Throwable t) {
18. problem = t;
19. t.printStackTrace();
20. }
21. doAfterProcessing(request, response);
22. ….
23. }
Click to edit Master title style
Riza Muhammad Nurman ADP
Output Program
Click to edit Master title style
Riza Muhammad Nurman ADP

More Related Content

PPTX
ADP - Chapter 2 Exploring the java Servlet Technology
PPT
Exception Handling
PPT
20. Object-Oriented Programming Fundamental Principles
PDF
Spring Boot
PPT
JDBC Java Database Connectivity
PDF
The JavaScript Programming Language
DOCX
KISI-KISI SOAL DAN KARTU SOAL BAHASA INGGRIS.docx
PDF
[PBO] Pertemuan 13 - Membuat Aplikasi Desktop dengan JDBC DAO MVC
ADP - Chapter 2 Exploring the java Servlet Technology
Exception Handling
20. Object-Oriented Programming Fundamental Principles
Spring Boot
JDBC Java Database Connectivity
The JavaScript Programming Language
KISI-KISI SOAL DAN KARTU SOAL BAHASA INGGRIS.docx
[PBO] Pertemuan 13 - Membuat Aplikasi Desktop dengan JDBC DAO MVC

What's hot (20)

PPT
Java And Multithreading
PPTX
Implicit objects advance Java
PDF
Spring Framework - Core
DOCX
Story board pembelajaran bahasa inggris
PDF
Chap 4 PHP.pdf
DOCX
Membangun aplikasi client server dengan java
PPT
Java 8 Streams
PPTX
Kenakalan remaja
PPTX
React + Redux Introduction
PDF
modul ajar kelas x bahasa inggris 2024-2025
PPT
Hibernate
PPTX
JSP- JAVA SERVER PAGES
PPTX
Spring jdbc
PPTX
past Pefect tense
PPTX
Spring boot Introduction
PDF
Java 8 Default Methods
PPTX
what is functional component
PPTX
Bahan Ajar Procedure Text
PDF
Introduction to Spring Boot!
PPTX
Business plan-kursus-inggris
Java And Multithreading
Implicit objects advance Java
Spring Framework - Core
Story board pembelajaran bahasa inggris
Chap 4 PHP.pdf
Membangun aplikasi client server dengan java
Java 8 Streams
Kenakalan remaja
React + Redux Introduction
modul ajar kelas x bahasa inggris 2024-2025
Hibernate
JSP- JAVA SERVER PAGES
Spring jdbc
past Pefect tense
Spring boot Introduction
Java 8 Default Methods
what is functional component
Bahan Ajar Procedure Text
Introduction to Spring Boot!
Business plan-kursus-inggris
Ad

Similar to ADP- Chapter 3 Implementing Inter-Servlet Communication (20)

PDF
May 2010 - RestEasy
PDF
PDF
Apache Wicket Web Framework
KEY
Multi Client Development with Spring
PDF
An Introduction to Tornado
PPT
Jsp/Servlet
PDF
JavaServer Pages
PPS
Exploring the Java Servlet Technology
PDF
[Bristol WordPress] Supercharging WordPress Development
PDF
Spring Web Services: SOAP vs. REST
PPTX
Javatwo2012 java frameworkcomparison
PPT
Java Servlets
PDF
Rich Portlet Development in uPortal
PPT
Servlet 3.0
PDF
Intro to Laravel 4
PDF
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010
PPT
Spring 3.x - Spring MVC
PPT
Strut2-Spring-Hibernate
PPTX
REST API for your WP7 App
PPTX
Job Managment Portlet
May 2010 - RestEasy
Apache Wicket Web Framework
Multi Client Development with Spring
An Introduction to Tornado
Jsp/Servlet
JavaServer Pages
Exploring the Java Servlet Technology
[Bristol WordPress] Supercharging WordPress Development
Spring Web Services: SOAP vs. REST
Javatwo2012 java frameworkcomparison
Java Servlets
Rich Portlet Development in uPortal
Servlet 3.0
Intro to Laravel 4
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010
Spring 3.x - Spring MVC
Strut2-Spring-Hibernate
REST API for your WP7 App
Job Managment Portlet
Ad

More from Riza Nurman (20)

PPTX
TOT PHP DAY 1
PPTX
SE - Chapter 9 Pemeliharaan Perangkat Lunak
PPTX
SE - Chapter 8 Strategi Pengujian Perangkat Lunak
PPTX
SE - Chapter 7 Teknik Pengujian Perangkat Lunak
PPTX
SE - Chapter 6 Tim dan Kualitas Perangkat Lunak
PPTX
XML - Chapter 8 WEB SERVICES
PPTX
XML - Chapter 7 XML DAN DATABASE
PPTX
XML - Chapter 6 SIMPLE API FOR XML (SAX)
PPTX
XML - Chapter 5 XML DOM
PPTX
DBA BAB 5 - Keamanan Database
PPTX
DBA BAB 4 - Recovery Data
PPTX
DBA BAB 3 - Manage Database
PPTX
DBA BAB 2 - INSTALASI DAN UPGRADE SQL SERVER 2005
PPTX
DBA BAB 1 - Pengenalan Database Administrator
PDF
RMN - XML Source Code
PPTX
XML - Chapter 4
PPTX
XML - Chapter 3
PPTX
XML - Chapter 2
PPTX
XML - Chapter 1
PPTX
ADP - Chapter 5 Exploring JavaServer Pages Technology
TOT PHP DAY 1
SE - Chapter 9 Pemeliharaan Perangkat Lunak
SE - Chapter 8 Strategi Pengujian Perangkat Lunak
SE - Chapter 7 Teknik Pengujian Perangkat Lunak
SE - Chapter 6 Tim dan Kualitas Perangkat Lunak
XML - Chapter 8 WEB SERVICES
XML - Chapter 7 XML DAN DATABASE
XML - Chapter 6 SIMPLE API FOR XML (SAX)
XML - Chapter 5 XML DOM
DBA BAB 5 - Keamanan Database
DBA BAB 4 - Recovery Data
DBA BAB 3 - Manage Database
DBA BAB 2 - INSTALASI DAN UPGRADE SQL SERVER 2005
DBA BAB 1 - Pengenalan Database Administrator
RMN - XML Source Code
XML - Chapter 4
XML - Chapter 3
XML - Chapter 2
XML - Chapter 1
ADP - Chapter 5 Exploring JavaServer Pages Technology

Recently uploaded (20)

PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Institutional Correction lecture only . . .
PPTX
Lesson notes of climatology university.
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
RMMM.pdf make it easy to upload and study
PDF
Pre independence Education in Inndia.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
Cell Types and Its function , kingdom of life
PDF
Insiders guide to clinical Medicine.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Basic Mud Logging Guide for educational purpose
Final Presentation General Medicine 03-08-2024.pptx
GDM (1) (1).pptx small presentation for students
Renaissance Architecture: A Journey from Faith to Humanism
Microbial diseases, their pathogenesis and prophylaxis
Module 4: Burden of Disease Tutorial Slides S2 2025
Institutional Correction lecture only . . .
Lesson notes of climatology university.
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
RMMM.pdf make it easy to upload and study
Pre independence Education in Inndia.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Sports Quiz easy sports quiz sports quiz
Cell Types and Its function , kingdom of life
Insiders guide to clinical Medicine.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...

ADP- Chapter 3 Implementing Inter-Servlet Communication

  • 1. Click to edit Master title style Riza Muhammad Nurman ADP Chapter 3 on Advanced Programming FACULTY Riza Muhammad Nurman Implementing Inter-Servlet Communication
  • 2. Click to edit Master title style Riza Muhammad Nurman ADP Content • Implement the request dispatcher mechanism • Work with filters
  • 3. Click to edit Master title style Riza Muhammad Nurman ADP Navigation Between Servlets Validation Servlet Is the User Valid? NoYes Welcome Servlet Error Servlet
  • 4. Click to edit Master title style Riza Muhammad Nurman ADP Dispatching a Request • RequestDispatcher is an object of the javax.servlet.RequestDispatcher interface that allows inter-servlet communication • Once you obtain the RequestDispatcher object, you can perform the following tasks: – Forward request to another Web page – Include content of the calling Web page in the called Web page RequestDispatcher dispatch = request.getRequestDispatcher("/WelcomeServlet"); dispatch.forward(request, response);
  • 5. Click to edit Master title style Riza Muhammad Nurman ADP Implementasi Login.java Login.java LoginServlet.java Welcome.java VALID INVALID
  • 6. Click to edit Master title style Riza Muhammad Nurman ADP Kode HTML untuk login 1. <html> 2. <head> 3. <title>TODO supply a title</title> 4. <meta charset="UTF-8"> 5. <meta name="viewport" content="width=device-width, initial-scale=1.0"> 6. <link rel="stylesheet" type="text/css" href="./css/style.css" /> 7. </head> 8. <body> 9. <form name="formLogin" method="POST" action="LoginServlet" > 10. <h1>Login</h1> 11. <label><span>User ID: </span> <input type="text" id="userid" name="userid"/></label> 12. <label><span>Password: </span><input type="password" id="pass" name="pass"/></label> 13. <label> 14. <span>&nbsp;</span> 15. <input type="submit" value="Log In" class="button"/><input type="reset" value="Reset" class="reset"/> 16. </label> 17. </form> 18. </body> 19. </html>
  • 7. Click to edit Master title style Riza Muhammad Nurman ADP Kode HTML login dalam Servlet 1. protected void processRequest(HttpServletRequest request, HttpServletResponse response) 2. throws ServletException, IOException { 3. response.setContentType("text/html;charset=UTF-8"); 4. try (PrintWriter out = response.getWriter()) { 5. out.println("<!DOCTYPE html>"); 6. out.println("<html>"); 7. out.println("<head>"); 8. out.println("<title>Servlet Login</title>"); 9. out.println("</head>"); 10. out.println("<body>"); 11. out.println("<form name='formLogin' method='POST' action='LoginServlet'"); 12. out.println("<h1>Login</h1>"); 13. out.println("<label><span>User ID: </span> <input type='text' id='userid' name='userid'/></label>"); 14. out.println("<label><span>Password: </span><input type='password' id='pass' name='pass'/></label>"); 15. out.println("<label>"); 16. out.println(" <span>&nbsp;</span>"); 17. out.println(" <input type='submit' value='Log In'/><input type='reset' value='Reset'/>"); 18. out.println("</label>"); 19. out.println("</form>"); 20. out.println("</body>"); 21. out.println("</html>"); 22. } 23. }
  • 8. Click to edit Master title style Riza Muhammad Nurman ADP Code Snippet LoginServlet 1. protected void processRequest(HttpServletRequest request, HttpServletResponse response) 2. throws ServletException, IOException { 3. response.setContentType("text/html;charset=UTF-8"); 4. try (PrintWriter out = response.getWriter()) { 5. String userid = request.getParameter("userid"); 6. String password = request.getParameter("pass"); 7. 8. if(userid.equals("admin") && password.equals("adminjuga")) { 9. RequestDispatcher dispatch = request.getRequestDispatcher("/Welcome"); 10. dispatch.forward(request, response); 11. } 12. else { 13. RequestDispatcher dispatch = request.getRequestDispatcher("/Login"); 14. dispatch.include(request, response); 15. out.println("Login Salah"); 16. } 17. } 18. }
  • 9. Click to edit Master title style Riza Muhammad Nurman ADP Welcome Servlet 1. protected void processRequest(HttpServletRequest request, HttpServletResponse response) 2. throws ServletException, IOException { 3. response.setContentType("text/html;charset=UTF-8"); 4. try (PrintWriter out = response.getWriter()) { 5. /* TODO output your page here. You may use following sample code. */ 6. out.println("<!DOCTYPE html>"); 7. out.println("<html>"); 8. out.println("<head>"); 9. out.println("<title>Servlet Welcome</title>"); 10. out.println("</head>"); 11. out.println("<body>"); 12. out.println("<h1>WELCOME</h1>"); 13. out.println("</body>"); 14. out.println("</html>"); 15. } 16. }
  • 10. Click to edit Master title style Riza Muhammad Nurman ADP Transferring Data 1. * ketik : import javax.servlet.RequestDispatcher; di atas nama class 2. protected void processRequest(HttpServletRequest request, HttpServletResponse response) 3. throws ServletException, IOException { 4. response.setContentType("text/html;charset=UTF-8"); 5. try (PrintWriter out = response.getWriter()) { 6. String userid = request.getParameter("userid"); 7. String password = request.getParameter("pass"); 8. 9. if(userid.equals("admin") && password.equals("adminjuga")) { 10. RequestDispatcher dispatch = request.getRequestDispatcher("/Welcome"); 11. dispatch.forward(request, response); 12. } 13. else { 14. request.setAttribute("result", "Login Salah"); 15. RequestDispatcher dispatch = request.getRequestDispatcher("/Login"); 16. dispatch.forward(request, response); 17. } 18. } 19. }
  • 11. Click to edit Master title style Riza Muhammad Nurman ADP Transferring Data 2 1. protected void processRequest(HttpServletRequest request, HttpServletResponse response) 2. throws ServletException, IOException { 3. response.setContentType("text/html;charset=UTF-8"); 4. try (PrintWriter out = response.getWriter()) { 5. out.println("<!DOCTYPE html>"); 6. out.println("<html>"); 7. out.println("<head>"); 8. out.println("<title>Servlet Login</title>"); 9. out.println("</head>"); 10. out.println("<body>"); 11. out.println("<form name='formLogin' method='POST' action='LoginServlet'"); 12. out.println("<h1>Login</h1>"); 13. out.println("<label><span>User ID: </span> <input type='text' id='userid' name='userid'/></label>"); 14. out.println("<label><span>Password: </span><input type='password' id='pass' name='pass'/></label>"); 15. out.println("<label>"); 16. out.println(" <span>&nbsp;</span>"); 17. out.println(" <input type='submit' value='Log In'/><input type='reset' value='Reset'/>"); 18. out.println("</label>"); 19. out.println("</form>"); 20. String data = (String)request.getAttribute("result"); 21. if(data != null) 22. out.println(data); 23. out.println("</body>"); 24. out.println("</html>"); 25. } 26. }
  • 12. Click to edit Master title style Riza Muhammad Nurman ADP Work With Filters • Users use different types of client devices, such as desktops, laptops, tablets, or mobile phone to access the Web applications stored in an application server • Each device displays the information in the application according to the settings in the corresponding Web browser window • Using servlet filters, you can customize the output of the application so that it can be viewed in different browsers. ServletServlet Servlet Client Filter Request Request Response Response
  • 13. Click to edit Master title style Riza Muhammad Nurman ADP User Interface On Client Devices The Interface of KASKUS on the Desktop System The Interface of KASKUS on the Mobile Phone Screen
  • 14. Click to edit Master title style Riza Muhammad Nurman ADP Introduction to Filters • A filter is an object that intercepts the requests and responses that flow between a client and a servlet – A filter can modify the headers and contents of a request coming from a Web client and forward it to the target servlet – A filter can also intercept and manipulate the headers and contents of the response that is sent back by the servlet
  • 15. Click to edit Master title style Riza Muhammad Nurman ADP IPFilter 1. public class IPFilter implements Filter { 2. String invalidip; 3. … 4. public IPFilter() { 5. } 6. 7. private void doBeforeProcessing(ServletRequest request, ServletResponse response) 8. throws IOException, ServletException { 9. … 10. } 11. private void doAfterProcessing(ServletRequest request, ServletResponse response) 12. throws IOException, ServletException { 13. …. 14. }
  • 16. Click to edit Master title style Riza Muhammad Nurman ADP IPFilter 2 1. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 2. if (debug) { log("IPFilter:doFilter()"); } 3. doBeforeProcessing(request, response); 4. Throwable problem = null; 5. try { 6. InetAddress ip = InetAddress.getLocalHost(); 7. String addressIP = ip.getHostAddress(); 8. if(addressIP.equals(this.invalidip)) { 9. RequestDispatcher rd = request.getRequestDispatcher("Error"); 10. rd.forward(request, response); 11. } 12. else { 13. RequestDispatcher rd = request.getRequestDispatcher("Welcome"); 14. rd.forward(request, response); 15. } 16. chain.doFilter(request, response); 17. } catch (Throwable t) { 18. problem = t; 19. t.printStackTrace(); 20. } 21. doAfterProcessing(request, response); 22. …. 23. }
  • 17. Click to edit Master title style Riza Muhammad Nurman ADP Output Program
  • 18. Click to edit Master title style Riza Muhammad Nurman ADP