總述
前置準備,jsp界面
<form action ="<%=request.getContextPath()%>/UserWXPServlet" method = "post">
用戶名<input type = "text" name = "username"/><br/>
密碼<input type = "text" name = "password"/><br/>
<input type = "submit" value ="提交"/>
</form>
成功後跳轉頁面
歡迎:${sessionScope.user.username}登陸!<br/>
我這沒有寫失敗頁面,大家可以自己寫,失敗頁面也沒什麼好說的了entity和登錄的實現
private String username;
private String password;
public User() {
}
public User(String user, String password) {
super();
this.username = user;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean dologin(User user){
Properties pro = new Properties();
InputStream is = UserWXPServlet.class.getClassLoader().getResourceAsStream("user_wxp.properties");
String password = null;
System.out.println(is+"--------->"+pro);
if(user==null){
return false;
}
try {
pro.load(is);
password = pro.getProperty(user.getUsername());
if(user.getPassword()!=null&&user.getPassword().equals(password)){
System.out.println("登陸成功");
return true;
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}
登錄成功返回true,失敗則返回false。Servlet和相應的邏輯工具類
/**
* 每一個用戶存放一個session。便於各種操作!!!
*/
public static Map<String, HttpSession> mapSession = new HashMap<String,HttpSession>();
public static void userLogout(String username){
if(mapSession.get(username)!=null){
//得到需要退出的用戶的session
HttpSession session = mapSession.get(username);
//在map<username,session>中移除該用戶,記住想要退出該用戶,必須將該session廢除或是remove掉user
mapSession.remove(username);
//得到session的所屬性合集
Enumeration e = session.getAttributeNames();
//刪除所有屬性
while(e.hasMoreElements()){
String sessionName = (String) e.nextElement();
session.removeAttribute(sessionName);
}
//廢除該session
session.invalidate();
}
}
Servlet的代碼如下:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
User user = new User(username, password);
UserService userService = new UserService();
HttpSession session = request.getSession();
if(userService.dologin(user)){
//登錄成功之後將用戶插入到session中
session.setAttribute("user", user);
if(cheackSession(username)){
//如果該session在此之前已經存在,則將該用戶進行退出操作
DbUtil.userLogout(username);
}
//將新的session存放到map<username,session>中
DbUtil.mapSession.put(username, session);
//操作成功,跳轉,此處最好爲重定向,讓別人知道登錄成功了
request.getRequestDispatcher("login").forward(request, response);
return ;
}
//此處跳轉到失敗頁面,如果讀者有興趣,可以自行添加
}
/**
* 檢查是否已經含有此session
* @param username
* @return true:已經存在,該刪! false:未存在
*/
private boolean cheackSession(String username){
HttpSession session = DbUtil.mapSession.get(username);
if(session!=null){
return true;
}
return false;
}
<servlet>
<description>
用於測試 ,用戶不可重複登錄
</description>
<display-name>UserWXPServlet</display-name>
<servlet-name>UserWXPServlet</servlet-name>
<servlet-class>com.fingard.rabbit.wxp_test.Servlet.UserWXPServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserWXPServlet</servlet-name>
<url-pattern>/UserWXPServlet</url-pattern>
</servlet-mapping>
以上就是所有實現的代碼,大家如果有什麼建議,還望能夠一起探討。有什麼問題,本人會盡快進行解答的。