解决Jsp放在WEB-INF之下无法运行的情况

介绍一下WEB-INF的作用

1.Jsp页面可以放在WEB-INF中

         http://localhost:8080/2020-1-10webProject/lesson05.jsp

        1.1 如果是把Jsp放在Webcontent的目录下,在浏览器访问的时候,可以直接访问项目名/jsp名字

                这种方式使得页面数据不安全

         1.2如果是把JSP放在WEB-INF目录下面,这个页面的数据比较安全,如果想访问WEB-INF目录下面的JSP

              只能通过转发读取,一般是通过servlet转发

举一个例子吧:目录是在WEB-INF/test.jsp。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
test.....
</body>
</html>


运行截图如下:

 加上Servlet之后:

package web_servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/test.jsp").forward(request, response);
    }
}

调通后的运行截图:

猜你喜欢

转载自www.cnblogs.com/whr-blogs/p/12182233.html
今日推荐