简体   繁体   中英

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

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).

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM