为什么我写成resp.sendRedirect("/WEB-INF/jsp/categoryList.jsp"); 这个跳转显示不出来
package com.per.maglley.controller;
import com.per.maglley.entity.User;
import com.per.maglley.utils.XmlDataSource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/login")
public class LoginController extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8");
req.setCharacterEncoding("UTF-8");
String username = req.getParameter("username");
String password = req.getParameter("password");
System.out.println("username:"+username);
System.out.println("password:"+password);
List<User> userData = XmlDataSource.getUserData();
boolean flag = false;
for(User user :userData){
if(user.getUserPassword().equals(password) && user.getUserName().equals(username)) {
flag = true;
break;
}
}
System.out.println(flag);
if(flag){
resp.sendRedirect("/WEB-INF/jsp/categoryList.jsp");
}else {
resp.sendRedirect(req.getContextPath() + "/login.html");
}
}
}
package com.per.maglley.service;
import com.per.maglley.entity.User;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
public class LoginFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
HttpSession session = request.getSession();
User user = (User) session.getAttribute("login");
boolean flag = false;
String uri = request.getRequestURI();
String[] url = {".js", ".ico", ".css", ".jsp", ".html", ".png", "/login"};
for (String str : url) {
if (uri.indexOf(str) != -1) {
flag = true;
break;
}
}
if (!flag) {
if (user != null) {
filterChain.doFilter(servletRequest, servletResponse);
return;
} else {
response.sendRedirect("/login.html");
return;
}
}
filterChain.doFilter(servletRequest, servletResponse);
}
}
package com.per.maglley.utils;
import com.per.maglley.entity.User;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
public class XmlDataSource {
private static List<User> userData = new ArrayList();
private static String dataFile;
static {
dataFile = XmlDataSource.class.getResource("/user.xml").getPath();
readUserXml();
}
private static void readUserXml(){
try {
dataFile= URLDecoder.decode(dataFile,"UTF-8");
System.out.println(dataFile);
SAXReader reader = new SAXReader();
//获取Document文档对象
Document document = reader.read(dataFile);
List<Node> nodes = document.selectNodes("/user/userid");
userData.clear();
for(Node node : nodes){
Element element =(Element) node;
String id = element.attributeValue("id");
String username = element.elementText("username");
String password = element.elementText("password");
User user = new User();
user.setUserName(username);
user.setUserPassword(password);
userData.add(user);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static List<User> getUserData() {
return userData;
}
}
16
收起
正在回答 回答被采纳积分+1
2回答
好帮手慕小班
2021-03-02 10:20:38
同学你好,
1、sendRedirect是重定向请求。重定向是浏览器会发送两次请求,先去服务器端拼好请求地址以后,再由浏览器重新发起的请求。
2、而WEB-INF作为安全目录,并不能直接访问。
所以如果页面在WEB-INF目录下,是不可以直接通过浏览器地址访问;因为WEB-INF目录是安全目录,不能直接访问该目录下的文件,只能通过转发访问,也就是需要先跳转到对应的controller中,然后通过转发跳转到WEB-INF下的页面中。
综上所述,同学的resp.sendRedirect("/WEB-INF/jsp/categoryList.jsp");书写有误,并不能正确访问到,同学尝试将地址为WEB-INF目录下的请求,将重定向换成转发再来试试。
祝学习愉快!
java工程师2020版
- 参与学习 人
- 提交作业 9393 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星