Java/Servlets/Include

Материал из Java эксперт
Перейти к: навигация, поиск

Servlet include

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class IncludeServlet extends HttpServlet {
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, java.io.IOException {
        
        response.setContentType("text/html");
        java.io.PrintWriter out = response.getWriter();
        
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Include Servlet</title>");  
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Welcome To Our Universe</h1>");
        out.println("Imagine the rest of the page here.<br><br>");
        //Include the copyright information
        RequestDispatcher dispatcher = request.getRequestDispatcher("/CopyRight");
        dispatcher.include(request, response);
        out.println("</body>");
        out.println("</html>");
      
        out.close();
        
    } 
}





Servlet Multiple Include

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MultipleInclude extends HttpServlet {
  protected void doGet(HttpServletRequest request,
      HttpServletResponse response) throws ServletException,
      java.io.IOException {
    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Multiple Includes</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>Hello from Level 1</h1>");
    out.println("This text is displayed at Level 1.");
    RequestDispatcher dispatcher = request.getRequestDispatcher("/Level4");
    dispatcher.include(request, response);
    out.println("</body>");
    out.println("</html>");
    out.close();
  }
}
// here is another servlet
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Level4 extends HttpServlet {
  
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, java.io.IOException {
        
         java.io.PrintWriter out = response.getWriter();
        out.println("<h4>Hello from another doGet</h4>");
    out.println("Hello from another doGet.");
  }
}
/*
<!-- web.xml -->
    <servlet>
        <servlet-name>MultipleInclude</servlet-name>
        <servlet-class>MultipleInclude</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>MultipleInclude</servlet-name>
        <url-pattern>/MultipleInclude</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>Level4</servlet-name>
        <servlet-class>Level4</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Level4</servlet-name>
        <url-pattern>/Level4</url-pattern>
    </servlet-mapping>
*/