韩顺平servlet笔记(完整代码) - 图文 下载本文

}

//分页显示

public ArrayList getResultByPage(int pageNow,int pageSize){ ArrayList al=new ArrayList(); try{ int rowCount=0;//共有几条记录(查表) int start=0;//开始第几条 //首先得到rowCount ConnDB cd=new ConnDB(); ct=cd.getConn(); ps=ct.prepareStatement(\ rs=ps.executeQuery(); if(rs.next()){ rowCount=rs.getInt(1); } //计算pageCount if(rowCount%pageSize==0){ pageCount=rowCount/pageSize; }else{ pageCount=rowCount/pageSize+1; }

// ps=ct.prepareStatement(\ start=(pageNow-1)*pageSize;

ps=ct.prepareStatement(\ rs=ps.executeQuery(); while(rs.next()){ //将rs中的每条记录封装到UserBean ub UserBean ub=new UserBean(); ub.setUserId(rs.getInt(1)); ub.setUserName(rs.getString(2)); ub.setPassword(rs.getString(3)); ub.setEmail(rs.getString(4)); ub.setGrade(rs.getInt(5)); //将ub放入arrayliat al.add(ub); } } catch(Exception ex){ ex.printStackTrace(); } finally{ this.close(); } return al; }

//验证用户

public boolean checkUser(String u,String p){ boolean b=false; try{ //得到链接 ConnDB cd=new ConnDB(); ct=cd.getConn(); ps=ct.prepareStatement(\ rs=ps.executeQuery(); if(rs.next()){ //说明用户存在 String dbPassword=rs.getString(1); if(dbPassword.equals(p)){ //真的合法 b=true; }

} }

catch (Exception ex){ ex.printStackTrace(); }

finally{ this.close(); }

return b; }

//关闭资源

public void close(){ try{ if(rs!=null){ rs.close(); } if(ps!=null){ ps.close(); } if(ct!=null){ ct.close(); } }

catch (Exception ex){ ex.printStackTrace(); } } } /**

* @(#)Login.java * *

* @author

* @version 1.00 2017/2/10 */

//登陆界面

package com.tsinghua; import javax.servlet.http.*; import java.io.*;

public class Login extends HttpServlet{

//处理get请求

public void doGet(HttpServletRequest req,HttpServletResponse res){ //业务逻辑 try { //中文乱码解决方法 res.setContentType(\

PrintWriter pw = res.getWriter();

String info=req.getParameter(\ //welcome中非法登陆情况用sendRedirect返回的值 if(info!=null){ pw.println(\你的用户名或是密码错误!
\ }

//返回登陆界面 pw.println(\ pw.println(\

pw.println(\ pw.println(\登陆界面\

pw.println(\

pw.println(\用户名:
\ pw.println(\密码:
\

pw.println(\两周内不在重新登录
\

pw.println(\ pw.println(\ pw.println(\ pw.println(\ }

catch (Exception ex){ ex.printStackTrace(); } }

public void doPost(HttpServletRequest req,HttpServletResponse res){ this.doGet(req,res); } } /** * @(#)LoginCl.java * *

* @author

* @version 1.00 2017/2/10 */

//验证(登陆处理) package com.tsinghua; import javax.servlet.http.*; import java.io.*; import java.sql.*;

public class LoginCl extends HttpServlet{ //重写init函数 public void init(){ try { //添加网站访问次数功能 //创建一个FileReader

FileReader f=new FileReader(\ BufferedReader br=new BufferedReader(f); //读出一行数据

String numVal=br.readLine(); //一定要关闭文件流 br.close();

//将times值放入到servletContext

this.getServletContext().setAttribute(\ System.out.println(\被调用\ }

catch (Exception ex) { ex.printStackTrace(); } } //重写destroy函数 public void destroy(){ try {

//创建一个FileWriter,从内存数据向磁盘文件(即写文件,输出流) FileWriter fw=new FileWriter(\ BufferedWriter bw=new BufferedWriter(fw); //写入数据

bw.write(this.getServletContext().getAttribute(\ //一定要关闭文件流 bw.close(); System.out.println(\被调用\ }

catch (Exception ex) { ex.printStackTrace(); }

}

//处理get请求

public void doGet(HttpServletRequest req,HttpServletResponse res){

//业务逻辑

Connection ct=null;

PreparedStatement ps=null; ResultSet rs=null; try { //接受用户名和密码 String u=req.getParameter(\ String p=req.getParameter(\ //调用userBeanCl UserBeanCl ubc=new UserBeanCl(); if(ubc.checkUser(u,p)){ //合法 String keep=req.getParameter(\ if(keep!=null){ //将用户名和密码保存在客户端(cookie) //创建cookie Cookie name=new Cookie(\ Cookie pass=new Cookie(\ //设置时间 name.setMaxAge(14*24*3600); pass.setMaxAge(14*24*3600); //回写到客户端 res.addCookie(name); res.addCookie(pass); }

//将验证成功的信息,写入session //1表示得到session

HttpSession hs=req.getSession(true); //修改session 的存在时间,设为20秒 hs.setMaxInactiveInterval(20); //把信息放session

hs.setAttribute(\ //这里u不需要双引号,不然session保存的不是用户名而是u

//将servletContext中的viitTime所对应的值++

String times=this.getServletContext().getAttribute(\ //对times值++

this.getServletContext().setAttribute(\

//跳转(welcome)

res.sendRedirect(\ }else{ //说明用户不存在 //跳转 res.sendRedirect(\ } }

catch (Exception ex){ ex.printStackTrace(); }finally{ try{ if(rs!=null){ rs.close(); } if(ps!=null){ ps.close(); } if(ct!=null){ ct.close(); }