Not Understanding Servlet RequestDispatcher - java

I have one doubt in Servlet RequstDispatcher. it has 2 methods(include and forward) right. my doubt is what happen when we use forward method following by include method..
RequestDispatcher rd = req.getRequestDispatcher("/S2");
rd.forward(req, res);
rd.include(req, res);
in this case these 2 methods executing fine or not ..
Thanks in Advance,

public void forward(ServletRequest request,ServletResponse response)throws ServletException,java.io.IOException
Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server.
public void include(ServletRequest request,ServletResponse response)throws ServletException,java.io.IOException
Includes the content or data of a resource (servlet, JSP page, or HTML file) in the response.
More information on : http://www.javatpoint.com/requestdispatcher-in-servlet

Related

How can I get the data of a server that returned the result, I want to get that result to update to another server

I have a servlet that returns results from the database, I want to get that result from another servlet
You can use RequestDispatcher for this
void forward(ServletRequest request, ServletResponse response) :
Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server
void include(ServletRequest request, ServletResponse response)
: includes the content of a resource (servlet, JSP page, HTML file) in the response

access WEB-INF/jsp from html

I have a simple application to test the communication between html and jsp. My jsp is located in
WEB-INF/test.jsp
Here is the structure of my files:
ProjectA
src
irstServlet.java
Web-Content
test1.html
WEB-INF
test.jsp
Here is the code from servlet
protected void doPost(HttpServletRequest request, response) throws ServletException, IOException {
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/test.jsp");
request.setAttribute("userName", request.getParameter("userName"););
dispatcher.forward(request, response);
}
First I have deploy in tomcat start my test1.html: It take me to the servlet: FirstServlet.java and I can enter userName there.
But after i enter the values in and press enter I expect it to forward me to test.jsp which is not working. I get the error:
The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Edited:
In my html I am trying to use it like:
<form method="POST" name="XX" action="/HelloWorldServlet">
Still not working.
Please can someone help me?
Your code does not look like it would compile at all.
Parameter response has no type - should be HttpServletResponse
There is a semicolon (;) after request.getParameter("userName")
Also I'm not sure why you're getting RequestDispatcher from servlet context rather than from the request - then again I've never checked if it makes any difference.
Anyway, I would rewrite doPost method like this:
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setAttribute("userName", request.getParameter("userName"));
req.getRequestDispatcher("/WEB-INF/test.jsp").forward(req, resp);
}
EDIT:
I'm assuimng you have either a correct servlet mapping in your web.xml:
<servlet>
<servlet-name>HelloWorldServlet</servlet-name>
<servlet-class>FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorldServlet</servlet-name>
<url-pattern>/HelloWorldServlet</url-pattern>
</servlet-mapping>
or your servlet is annotated with #WebServlet annotation:
#WebServlet("/HelloWorldServlet")
public class FirstServlet extends HttpServlet {
//your code
}
. If neither of those is true, that's your problem right there.

Communication between servlets and/or jsp

How can you sent a request from one servlet to another or one servlet to any jsp file?
Actually i want to send a request form one servlet named Demo to another jsp file abc.jsp
Using RequestDispatcher
programatically...
public class Demo extends HttpServlet{
public void doGet(HttpServletRequest req , HttpServletRespaonse res)
throws ServletException, IOException {
res.setContentType(text/html);
PrintWritter pr = res.getWriter();
pr.println("i am in servlet");
RequestDispatcher rd = req.getRequestDispatcher("abc.jsp");
rd.forward();
}
}
abc.jsp
<body>
<i am abc in abc.jsp>
</body>
You can either forward it or redirect it.
To forward, you can use RequestDispatcher
RequestDispatcher rd = request.getRequestDispatcher("abc.jsp");
rd.forward(request, response);
To redirect,
response.sendRedirect("abc.jsp");
FYI, Difference between the two,
In Forwarding, the same request object is forwarded to the next resource (Servlet or JSP) and in Redirecting client (browser) is asked to send a new request to the server for the next resource (servlet or JSP).

Servlet mapping for ajax call parameters

I have ajax calls that have the url http://localhost:80/Push/GetContacts?id=23 and the following servlet mapping:
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/GetContacts*</url-pattern>
</servlet-mapping>
The servlet does not get invoked on ajax calls. It returns a HTTP 404 not found response. What is the right URL pattern for my ajax calls?
Here is the ajax call that is not working because of the servlet.
jQuery.getJSON('http://localhost:8080/Push/GetContacts&callback=?', function(data) {
alert(data.data);
});
The servlet:
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{
try{
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.write("{data: helloworld}");
out.close();
}
catch(Exception e){
e.printStackTrace();
}
The url pattern needs to be just /GetContacts without the star. The parameters are not part of the servlet mapping and are ignored when finding the correct servlet. If you wanted to support an URL like /GetContacts/23 you could use a servlet mapping for /GetContacts/* and retrieve the id using request.getPathInfo.
Edit: as BalusC just noticed, the url in your ajax call is incorrect. The parameter callback should be separated by a question mark, not an ampersand: GetContacts?callback=...
Also, {data: helloworld} is invalid according to the json spec, both data and helloworld should be enclosed in quotes. But that is also independent from the 404 problem.

RequestDispatcher in Java

What exactly the main purpose of using RequestDispatcher, for example when it's executed in Filter like the following example:
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws ServletException, IOException {
HttpServletRequest aHttpServletRequest = (HttpServletRequest) request;
aHttpServletRequest.getRequestDispatcher("/init.jsp").include(request, response);
chain.doFilter(request, response);
}
In your case, the output of JSP is prepended to every page that filter is attached. This is a technique to easily add a common header to all your pages.
It's a little bit confusing when RequestDispatcher is used int his context. Normally, when you want to dispatch your request to another servlet or JSP for process, you use RequestDispatcher to forward to another resource. In this case, your request is not dispatched anywhere else, instead you include output generated by another resource to your current response.
The javadoc says is better than I can:
RequestDispatcher:
Defines an object that receives
requests from the client and sends
them to any resource (such as a
servlet, HTML file, or JSP file) on
the server. The servlet container
creates the RequestDispatcher object,
which is used as a wrapper around a
server resource located at a
particular path or given by a
particular name.
In other words, you obtain a RequestDispstcher when you want to include from, or forward to, another resource on the server.

Categories

Resources