Sending Values From Servlet to Html and Again From Second Servlet to Second Html
Servlet Collaboration In Java Using RequestDispatcher and HttpServletResponse
What is Servlet Collaboration?
The commutation of information among servlets of a particular Java spider web application is known as Servlet Collaboration. This enables passing/sharing information from one servlet to the other through method invocations.
What are the principle ways provided by Java to achieve Servlet Collaboration?
The servlet api provides two interfaces namely:
- javax.servlet.RequestDispatcher
- javax.servlet.http.HttpServletResponse
These two interfaces include the methods responsible for achieving the objective of sharing information between servlets.
Using RequestDispatcher Interface
The RequestDispatcher interface provides the option of dispatching the client's request to another web resource, which could be an HTML page, another servlet, JSP etc. It provides the following two methods:
- public void forward(ServletRequest request, ServletResponse response)throws ServletException, java.io.IOException:
The forward() method is used to transfer the client request to some other resource (HTML file, servlet, jsp etc). When this method is called, the command is transferred to the next resource called. On the other hand, the include() method is used to include the content of the calling file into the called file. Subsequently calling this method, the control remains with the calling resource, but the processed output is included into the chosen resource.
The following diagram explains the way it works:
- public void include(ServletRequest request, ServletResponse response)throws ServletException, java.io.IOException:
The include() method is used to include the contents of the calling resource into the called one. When this method is called, the command still remains with the calling resources. It only includes the processed output of the calling resource into the chosen ane.
The following diagram explains how it works:
- Example of using RequestDispatcher for Servlet Collaboration
The following example explains how to use RequestDispatcher interface to achieve Servlet Collaboration:
alphabetize.html
html
<
html
>
<
head
>
<
body
>
<
form
action
=
"login"
method
=
"post"
>
Name:<
input
type
=
"text"
name
=
"userName"
/><
br
/>
Countersign:<
input
blazon
=
"password"
name
=
"userPass"
/><
br
/>
<
input
type
=
"submit"
value
=
"login"
/>
</
form
>
</
torso
>
</
html
>
- Login.java
Java
import
java.io.*;
import
javax.servlet.*;
import
javax.servlet.http.*;
public
class
Login
extends
HttpServlet {
public
void
doPost(HttpServletRequest req,
HttpServletResponse res)
throws
ServletException, IOException
{
res.setContentType(
"text/html"
);
PrintWriter out = response.getWriter();
String due north = request.getParameter(
"userName"
);
Cord p = request.getParameter(
"userPass"
);
if
(p.equals(
"Thanos"
){
RequestDispatcher rd = request.getRequestDispatcher(
"servlet2"
);
rd.forrard(request, response);
}
else
{
out.print(
"Password mismatch"
);
RequestDispatcher rd = request.getRequestDispatcher(
"/index.html"
);
rd.include(asking, response);
}
}
}
- Welcome.java
Java
import
java.io.*;
import
javax.servlet.*;
import
javax.servlet.http.*;
public
class
Welcome
extends
HttpServlet {
public
void
doPost(HttpServletRequest request,
HttpServletResponse response)
throws
ServletException, IOException
{
response.setContentType(
"text/html"
);
PrintWriter out = response.getWriter();
String n = request.getParameter(
"userName"
);
out.print(
"Welcome "
+ n);
}
}
- web.xml
html
<
web-app
>
<
servlet
>
<
servlet-name
>Login</
servlet-name
>
<
servlet-class
>Login</
servlet-grade
>
</
servlet
>
<
servlet
>
<
servlet-name
>WelcomeServlet</
servlet-name
>
<
servlet-class
>Welcome</
servlet-grade
>
</
servlet
>
<
servlet-mapping
>
<
servlet-proper noun
>Login</
servlet-name
>
<
url-design
>/servlet1</
url-pattern
>
</
servlet-mapping
>
<
servlet-mapping
>
<
servlet-name
>WelcomeServlet</
servlet-proper noun
>
<
url-pattern
>/servlet2</
url-blueprint
>
</
servlet-mapping
>
<
welcome-file-listing
>
<
welcome-file
>index.html</
welcome-file
>
</
welcome-file-list
>
</
web-app
>
Output:
- alphabetize.html
- If password matches:
- If password doesn't lucifer:
Using HttpServletResponse Interface
- The HttpServletResponse interface is entrusted with managing Http responses. To achieve servlet collaboration, information technology uses the post-obit method:
public void sendRedirect(String URL)throws IOException;
- This method is used redirect response to another resource, which may be a servlet, jsp or an html file. The statement accepted by it, is a URL which tin can exist both, absolute and relative. Information technology works on the client side and uses the browser'southward URL bar to make a asking.
Example of using sendRedirect() for redirection
- The following example of a web application created using servlet takes the text written in the text field in the webpage, and directs information technology to the servlet. The servlet and so redirects information technology to google, which so produces search results based on the text written.
index.html
html
<
html
>
<
head
>
<
trunk
>
<
course
action
=
"search"
method
=
"GET"
>
<
input
type
=
"text"
name
=
"name"
>
<
input
type
=
"submit"
value
=
"search"
>
</
class
>
</
body
>
</
html
>
Java
import
java.io.IOException;
import
javax.servlet.ServletException;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
public
class
MySearcher
extends
HttpServlet {
protected
void
doGet(HttpServletRequest
request,
HttpServletResponse response)
throws
ServletException, IOException
{
String proper name = request.getParameter(
"proper name"
);
}
}
- web.xml
html
<
web-app
>
<
servlet
>
<
servlet-name
>MySearcher</
servlet-name
>
<
servlet-course
>MySearcher</
servlet-class
>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>MySearcher</
servlet-name
>
<
url-pattern
>/search</
url-pattern
>
</
servlet-mapping
>
<
welcome-file-list
>
<
welcome-file
>alphabetize.html</
welcome-file
>
</
welcome-file-list
>
</
web-app
>
Output:
- index.html
- Search upshot
What is the departure between forward() method of RequestDiispatcher and sendRedirect() of HttpServletResponse?
- Although the ii methods appear to exercise the aforementioned matter, there are still differences between the 2, which are as follows:
forward() | sendRedirect() |
It works on the server side | It works on the client side |
It sends the same request and response objects to another resource. | Information technology always send a new request |
It works but within the server. | It can be used inside and outside the server. |
Source: https://www.geeksforgeeks.org/servlet-collaboration-java-using-requestdispatcher-httpservletresponse/
0 Response to "Sending Values From Servlet to Html and Again From Second Servlet to Second Html"
Postar um comentário