我正在尝试使用RequestDispatcher从servlet重定向页面,但它无法正常工作。我的servlet代码是这样的:
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
List<User> users = new LinkedList<User>();
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() throws FileNotFoundException, IOException{
super();
File file = new File("C:/Users/Misel/workspace/SalonNamestaja/src/org/SalonNamestaja/dataFiles/users.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
String[] parts;
while((line = bufferedReader.readLine()) != null) {
parts = line.split("\\|");
users.add(new User(parts[1], parts[2], parts[0], parts[4], parts[3], parts[5], parts[6]));
}
fileReader.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/text");
response.setCharacterEncoding("UTF-8");
String json = request.getParameter("jsonData");
ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(json, User.class);
//System.out.println(user.getName() + user.getPassword() + " | " + user.getLevel());
if(user.getLevel().equals("korisnik")) {
user = new LoginService().authenticate(users, user.getName(), user.getPassword());
//System.out.println("Postoji korisnik");
HttpSession session = request.getSession();
session.setAttribute("activeUser", user);
//response.getWriter().write("true");
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/dashboard.jsp");
requestDispatcher.forward(request, response);
//System.out.println("OK");
return;
}else if(user.getLevel().equals("admin")) {
user = new LoginService().authenticate_admin(users, user.getName(), user.getPassword());
System.out.println("Postoji admin");
HttpSession session = request.getSession();
session.setAttribute("activeUser", user);
response.getWriter().write("true");
return;
}else {
System.out.println("Korisnik ne postoji!");
response.getWriter().write("false");
}
}
我尝试使用sendRedirect()重定向,但这也不起作用。我找不到问题所在,所以如果有人可以帮助我。