You are on page 1of 6

A RequestDispatcher object can be used to forward a request to the

resource or to include the resource in a response. The resource can be


dynamic or static.

The pathname specified may be relative, although it cannot access outside


the current application.
This method returns null if the servlet container cannot return a
RequestDispatcher.

Forward
Forwards a request from a servlet to another resource (servlet or
JSP file) on the server.
We can use one servlet to do preliminary processing of a request
and another resource to generate the output response.
Any response from the first servlet will be automatically cleared
before forwarding to second servlet.
The forwared Servlet will be using the same request and response
object send by first servlet.

Include
Includes the content of a resource (servlet or JSP page) in the
response. In essence, this method enables programmatic server-side
includes.
The included servlet cannot change the response status code or set
headers of first servlet, any attempt to make a change is ignored.

Example
Html code

1. <!DOCTYPE html>
2. <html>
3. <head>
4. <metacharset="ISO-8859-1">
5. <title>Insert title here</title>
6. </head>
7. <body>
8.
9. Candidjava RequestDispatcher Forward example <br><br>
10.
11. <formaction="TestForwardController"method="post">
12.
13. Enter username :<inputtype="text"name="username"><br>
14. Enter password :<inputtype="password"name="password"><br>
15. <inputtype="submit"value="Login">
16.
17.
18. </form>
19.
20. <br><br><br><br>
21. Candidjava RequestDispatcher Include example <br><br>
22. <formaction="TestIncludeController"method="post">
23.
24. Enter username :<inputtype="text"name="username"><br>
25. Enter password :<inputtype="password"name="password"><br>
26. <inputtype="submit"value="Login">
27.
28.
29. </form>
30.
31. </body>
32. </html>

Servlet Include

1. package com.candidjava;
2.
3. import java.io.IOException;
4. import java.io.PrintWriter;
5.
6. import javax.servlet.RequestDispatcher;
7. import javax.servlet.ServletException;
8. import javax.servlet.http.HttpServlet;
9. import javax.servlet.http.HttpServletRequest;
10. import javax.servlet.http.HttpServletResponse;
11.
12. /**
13. * Servlet implementation class TestIncludeController
14. */
15. publicclassTestIncludeControllerextendsHttpServlet{
16.
17. protectedvoid doPost(HttpServletRequest request,HttpServletRes
ponse response)throwsServletException,IOException{
18. String un=request.getParameter("username");
19. String pw=request.getParameter("password");
20.
21. response.setContentType("text/html");
22. PrintWriterout=response.getWriter();
23. out.print("<b> Welcome to TestIncludeController(Servlet1)
<br>");
24. out.print("username received is "+un +"</b>");
25.
26. out.print("<br>");
27. out.print("<br>");
28. out.print("<br>");
29.
30. RequestDispatcher dis= request.getRequestDispatcher("/Test
Controller");
31. dis.include(request, response);
32. }
33.
34. }

Servlet Forward

1. package com.candidjava;
2.
3. import java.io.IOException;
4. import java.io.PrintWriter;
5.
6. import javax.servlet.RequestDispatcher;
7. import javax.servlet.ServletException;
8. import javax.servlet.http.HttpServlet;
9. import javax.servlet.http.HttpServletRequest;
10. import javax.servlet.http.HttpServletResponse;
11.
12. /**
13. * Servlet implementation class TestForwardController
14. */
15. publicclassTestForwardControllerextendsHttpServlet{
16.
17. protectedvoid doPost(HttpServletRequest request,HttpServletRes
ponse response)throwsServletException,IOException{
18. // TODO Auto-generated method stub
19.
20. String un=request.getParameter("username");
21. String pw=request.getParameter("password");
22.
23. response.setContentType("text/html");
24. PrintWriterout=response.getWriter();
25. out.print("<b> Welcome to TestForwardController(Servlet1)
<br>");
26. out.print("username received is "+un +"</b>");
27.
28. out.print("<br>");
29. out.print("<br>");
30. out.print("<br>");
31.
32. RequestDispatcher dis= request.getRequestDispatcher("/Test
Controller");
33. dis.forward(request, response);
34.
35. }
36.
37. }

TestController

1. package com.candidjava;
2.
3. import java.io.IOException;
4. import java.io.PrintWriter;
5.
6. import javax.servlet.ServletException;
7. import javax.servlet.http.HttpServlet;
8. import javax.servlet.http.HttpServletRequest;
9. import javax.servlet.http.HttpServletResponse;
10.
11. /**
12. * Servlet implementation class TestController
13. */
14. publicclassTestControllerextendsHttpServlet{
15.
16. protectedvoid doPost(HttpServletRequest request,HttpServletRes
ponse response)throwsServletException,IOException{
17. // TODO Auto-generated method stub
18.
19. String un=request.getParameter("username");
20.
21. response.setContentType("text/html");
22. PrintWriterout=response.getWriter();
23. out.print("<b> Welcome to TestController(Servlet2) <br>");
24. out.print("username received is "+un +"</b>");
25. }
26.
27. }

Advantages of JSON:
1. JSON is Faster:

JSON syntax is very easy to use. We have to use only -> as an syntax
which provide us a easy parsing of the data and faster execution of the data.
Since its syntax is very small and light weighted that’s the reason that it
executes the response in faster way.

2. Schema Support:

It has wide range of supported browser compatibility with the operating


systems so the applications made with the coding of JSON doesn’t require
much effort to make it all browser compatible. During development
developer thinks for the different different browsers but JSON provides
that functionality.

3. Server Parsing:

On the server side parsing is the important part that developers want if the
parsing will be fast on the server side then only user can get the fast
response of their response so in this case JSON server side parsing is strong
point that indicates us to use the JSON on server side.

4. Tool for sharing data:

JSON is the best tool for the sharing data of any size even audio, video etc.
This is because JSON stores the data in the arrays so data transfer makes
easier. For this reason, JSON is a superior file format for web APIs and for
web development.

Disadvantages of JSON:

First and foremost, in JSON has no error handling for JSON calls. If the
dynamic script insertion works, you get called and will get the response
perfectly. If not inserted, nothing happens. It just fails silently. For
example, you are not able to catch a 404 error from the server, Nor can you
cancel or restart the request. You can, however, timeout after waiting a
reasonable amount of time.
Another major drawback of JSON is that it can be quite dangerous if used
with untrusted services or untrusted browsers, because a JSON service
returns a JSON response wrapped in a function call, which will be
executed by the browser if it will be used with untrusted browser it can be
hacked, this makes the hosting Web Application Vulnerable to a variety of
attacks. If you are going to use JSON services, it’s very important to be
aware of the threats which JSON have in that and also be aware with the
things which can protect it. JSON only have limited supported tools which
we can use during JSON development.

You might also like