-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLoginServlet.java
More file actions
64 lines (48 loc) · 1.82 KB
/
LoginServlet.java
File metadata and controls
64 lines (48 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package guoyachen.mvc.servlet;
import guoyachen.mvc.factory.DAOFactory;
import guoyachen.mvc.vo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
/**
* @Author:Guo Yachen
* @Date:2020/7/11 15:20
* @Description:
* @Feature:
*/
public class LoginServlet extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse resp)
{
String path = "index.jsp";
String username = req.getParameter("username");
String password = req.getParameter("password");
List<String> info = new ArrayList<String>();
User user = new User();
user.setUsername(username);
user.setPassword(password);
try {
//这三个一定要在输出流之前
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
resp.setCharacterEncoding("utf-8");
PrintWriter out = resp.getWriter();
ApplicationContext context = new ClassPathXmlApplicationContext("applicationcontext.xml");
DAOFactory Factory =(DAOFactory) context.getBean("Factory");
if(DAOFactory.getUserDAOInstance().findLogin(user))
{
out.print("<script>alert('登录成功');window.location.href='admin.jsp'</script>");
}
else
{
out.print("<script>alert('登录失败,请重新登录');window.location.href='index.jsp'</script>");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}