import java.io.File;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.naming.NamingException;
import javax.servlet.ServletContext;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.sql.SQLException;
import java.awt.datatransfer.*;
import java.util.Date;
import java.util.logging.Logger;
// @WebServlet("/Login")
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Login() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 会话处理
HttpSession session = request.getSession(false);
if (session == null) {
System.out.println("没有会话");
session = request.getSession(true);
Date date = new Date();
long lastdatetime = date.getTime();
String dateString = String.valueOf(lastdatetime);
session.setAttribute("lastdatetime", dateString);
} else {
System.out.println("有会话");
String lastDateTimeString = (String) session.getAttribute("lastdatetime");
System.out.println("上次的时间是:" + lastDateTimeString);
}
session.setMaxInactiveInterval(10);
// Cookie
Cookie[] sss = request.getCookies();
if (sss.length > 1) {
System.out.println("have");
this.printCookieList(sss, response);
} else {
System.out.println("no have");
Cookie myCookie = new Cookie(URLEncoder.encode("username", "utf-8"), URLEncoder.encode("牛宏飞", "utf-8"));
myCookie.setMaxAge(10);
response.addCookie(myCookie);
this.printCookieList(request.getCookies(), response);
}
// 设置响应内容类型
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
// 处理中文
String name = new String(request.getParameter("username").getBytes("ISO8859-1"), "UTF-8");
String pwd = new String(request.getParameter("pwd").getBytes("ISO8859-1"), "UTF-8");
out.println("用户名:" + name + "密码:" + pwd);
ConnectSql connectSql = new ConnectSql();
try {
connectSql.connectMysql();
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ServletContext servletContext = getServletContext();
out.println("realpath = <br/>" + servletContext.getRealPath("./") + File.pathSeparator + "nhfc99");
Logger logger = Logger.getLogger("username");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
private void printCookieList(Cookie[] object, HttpServletResponse response) throws UnsupportedEncodingException {
for (int i = 0; i < object.length; i++) {
Cookie oneCookie = object[i];
System.out.println("cookie name = " + URLDecoder.decode(oneCookie.getName(), "utf-8"));
System.out.println("cookie value = " + URLDecoder.decode(oneCookie.getValue(), "utf-8"));
}
}
}