package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ysl.DBoracle;
import com.ysl.User;
public class AutomUser extends HttpServlet {
/**
* Constructor of the object.
*/
public AutomUser() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
/**
* 判断cookie是否存在该用户的信息
*
*/
Cookie[] cokies = request.getCookies();
String cookiesName = "";
String cookiesPass = "";
for (int i = 0; i < cokies.length; i++) {
if ("cookieName".equals(cokies[i].getName())) {
cookiesName = cokies[i].getValue();
}
if ("cookiePass".equals(cokies[i].getName())) {
cookiesPass = cokies[i].getValue();
}
}
/**
* 判断用户是否自动登录
*/
if (cookiesName != "" && cookiesPass != "") {
//登录成功
System.out.println("cookie......");
response.sendRedirect("../index.jsp");
return;
} else {
String name = request.getParameter("username");
String pass = request.getParameter("password");
//是否自动保存用户名或密码
String flag = request.getParameter("checked");
System.out.println(flag);
/**
* 判断用户是否保存用户名和密码
* 注意:通过用户名、密码到后台判断 如果判断成功后才保存到cookie中
* 例如:User
*
*/
User user = null;
if (user == null) {
System.out.println("User.....");
//保存cookie
if (flag != null) {
Cookie cookieName = new Cookie("cookieName", name);
Cookie cookiePass = new Cookie("cookiePass", pass);
cookieName.setMaxAge(60*60*100*1000);
cookiePass.setMaxAge(60*60*100*1000);
response.addCookie(cookieName);
response.addCookie(cookiePass);
System.out.println("savecookie......");
}
//登录成功
response.sendRedirect("../index.jsp");
System.out.println("newUser.....");
return;
}
}
//登录失败
System.out.println("endstart....");
response.sendRedirect("../User/Login.jsp");
System.out.println("end....");
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
评论7
最新资源