LoginCl的代码:public class LoginCl
extends HttpServlet {
@Override
public void init(){
try{
System.out.println("initb被调用一次"
);
//从文件中读
FileReader f=
new FileReader("F:\\testfile.txt"
);
BufferedReader br=
new BufferedReader(f);
String str=
br.readLine();
ServletContext sc=
this.getServletContext();
sc.setAttribute("count"
,(Integer.parseInt(str)));
br.close();
}
catch(Exception e){
e.printStackTrace();
}
}
@Override
public void destroy(){
try{
System.out.println("destroy被调用一次"
);
//向文件中写人数据
FileWriter fw=
new FileWriter("F:\\testfile.txt"
);
BufferedWriter bw=
new BufferedWriter(fw);
int count=(Integer)
this.getServletContext().getAttribute("count"
);
bw.write(count +""
);
//注意一定要关闭
bw.close();
}
catch(Exception e){
e.printStackTrace();
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Connection ct=
null;
Statement sm=
null;
ResultSet rs=
null;
try{
String name=request.getParameter("user_name"
);
String pwd=request.getParameter("user_pwd"
);
Class.forName("oracle.jdbc.driver.OracleDriver"
);
ct=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","outln","outln"
);
sm=
ct.createStatement();
rs=sm.executeQuery("select user_pwd from Person where user_name='"+name+"'"
);
if(rs.next()){
//进来了说明该用户存在
if(rs.getString(1).equals(pwd)){
//进来了说明密码正确
String check=request.getParameter("check"
);
if(check!=
null){
//在服务器创建cookie
Cookie n=
new Cookie("user_name"
,name);
Cookie p=
new Cookie("user_pwd"
,pwd);
//设置时间
n.setMaxAge(14 * 24 * 3600
);
p.setMaxAge(14 * 24 * 3600
);
//回写到客户端
response.addCookie(n);
response.addCookie(p);
}
HttpSession hs=request.getSession(
true);
hs.setMaxInactiveInterval(30
);
hs.setAttribute("name"
,name);
//hs.setAttribute("pwd",pwd);
//没登录一次,就把服务器ServletContext的访问次数加1
int count=(Integer)
this.getServletContext().getAttribute("count"
);
this.getServletContext().setAttribute("count",(count+1
));
response.sendRedirect("Wel");
//这里要写的是你要转向的servlet的URL
}
else response.sendRedirect("login");
//密码错误
}
else response.sendRedirect("login");
//该用户不存在
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
if(ct!=
null) ct.close();
if(sm!=
null) ct.close();
if(rs!=
null) ct.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}welcome的代码:
作用:防止用户非法登录,同时对两周内(这是自己设定的)保存密码的用户进行处理
public class Wel
extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try{
PrintWriter pw=
response.getWriter();
//防止用户非法登录
HttpSession hs=
request.getSession();
String user_name=(String)hs.getAttribute("name"
);
String name="",pwd=""
;
if(user_name==
null){
Cookie myCookie[]=
request.getCookies();
if(myCookie!=
null){
for(
int i=0;i<myCookie.length;i++
){
if(myCookie[i].getName().equals("user_name"
))
name=
myCookie[i].getValue();
else if(myCookie[i].getName().equals("user_pwd"
))
pwd=
myCookie[i].getValue();
}
System.out.println("------" + name + "/" +
pwd);
if(!name.equals("")&& !pwd.equals(""
)){
response.sendRedirect("LoginCl?user_name="+name+"&user_pwd="+pwd+""
);
return;
}
}
response.sendRedirect("login"
);
}
pw.println("您的id为:" + request.getRemoteAddr()); pw.println("您的id为:" + request.getRemoteHost());
int a=(Integer)
this.getServletContext().getAttribute("count"
);
pw.print(a);
}
catch(Exception e){
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request,response);
}
}