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).
Related
I need to implement a servlet with java and a tomcat server. I also need to use the MVC pattern.
So the model part is clear to me. But how do I seperate view and controler in this case? I thought my httpServlet class is my view, but how do I then implement the controller?
Model is your business data that you deal with. and finally you sent it to client to render in view(JSP)
View is your Jsp Pages which controller sends to the client, based on client request.
Controller is your Servlet which accept the client request and execute your business logic and select appropriate view(JSP) and return it to client.
see the below Example where TestServlet is your Controller, Index.jsp is you view.
public class TestServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//business logic that deal with the your Model
RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
rd.forward(request, response);
}
}
The httpServlet is the controller, The servlet needs to forward request to a JSP(Jsp is termed as view).
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
Is there a way to send parameters from servlet to jsp without redirecting the browser to that page?
RequestDispatcher disp = request.getRequestDispatcher("shoppingCart.jsp");
disp.forward(request, response);
There can be one way as below:
RequestDispatcher disp = request.getRequestDispatcher("shoppingCart.jsp"+"?myParam=myValue");
disp.forward(request, response);
If you are fine with "GET" method then you can solve this problem with appended parameters.
Well you can either set the attributes(used in case of internal communication with servlets or servlet to jsp or vice-versa) to the response object and forward the request you can achieve this as :
request.setAttribute("someKey","someValue");
You can also use the session scope to share the attributes between servlet and jsp like this:
Http session = request.getSession();
session.setAttribute("someKey","someValue");
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.
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.