在PHP中实现404错误页面的方法,我们可以创建一个名为404.php
的文件,将其放在网站的根目录下,在这个文件中,我们可以使用HTML和PHP来构建一个简单的404错误页面,以下是一个示例:
<?php header("HTTP/1.0 404 Not Found"); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>404 - Page Not Found</title> </head> <body> <h1>404 - Page Not Found</h1> <p>Sorry, the page you are looking for does not exist.</p> </body> </html>
在Java中实现404错误页面的方法,我们可以创建一个继承自HttpServlet
的类,并重写doGet
方法,在这个方法中,我们可以设置响应的内容类型为text/html
,并输出一个简单的404错误页面,以下是一个示例:
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 NotFoundServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); response.getWriter().println("<!DOCTYPE html>"); response.getWriter().println("<html lang=\"en\">"); response.getWriter().println("<head>"); response.getWriter().println("<meta charset=\"UTF-8\">"); response.getWriter().println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">"); response.getWriter().println("<title>404 - Page Not Found</title>"); response.getWriter().println("</head>"); response.getWriter().println("<body>"); response.getWriter().println("<h1>404 - Page Not Found</h1>"); response.getWriter().println("<p>Sorry, the page you are looking for does not exist.</p>"); response.getWriter().println("</body>"); response.getWriter().println("</html>"); } }
还没有评论,来说两句吧...