之后我对Session的详细操作,以及经验
这次的总结是在解决了下面的数据库操作问题后的总结,写在此归纳为同类。
在解决了操作数据库后,我又重新温习了下session的应用,这很重要。
对于session操作,我觉得分为三部分,:
第一,登录界面的时候,在前面首先判断session中用户是否已经存在记录
第二,登录处理的时候,防止用户非法进入
第三,wellcome界面,也是防止用户非法进入
对于登录界面,很好理解,就是当session没过期的时候,用户打开网页,不需要登录了,直接跳转到登录后界面。
什么是非法进入,比如某用户想不通过登录直接输入相应网址进入,如果没有session验证,网站就就总是开放的。
首先是登陆处理,这个是核心,在处理这不仅需要读取session,还需要写入session。
Session需要写在数据库验证之前,
这里我之前犯了个不大的错误,是关于res.sendRedirect跳转的错误,我将session认证和数据库认证并行写于doGet中,这样带来的错误就是在session认证中和数据库认证中同时存在
登录处理session代码:
- HttpSession
hs;//创建一个session - hs=req.getSession(true);
- String
usernow=(String)hs.getAttribute("user"); - if(usernow!=null){
- try
{ - res.sendRedirect("wellcome");
- }
catch (IOException e) { - e.printStackTrace();
- }
- }
else{ -
。。。数据库操作 - 在验证成功后的操作中还需要插入session:
- if(pw.equals(p)){//合法跳转
- hs.setAttribute("user",u
); - hs.setAttribute("pass",p
); - res.sendRedirect("wellcome");//
写你要到的servlet的那个URL+变量 - }
- ...
- }
登录界面跳转代码:
和上面的验证完全一样
- hs=req.getSession(true);
- String
usernow=(String)hs.getAttribute("user"); - if(usernow!=null){
- try
{ - res.sendRedirect("wellcome");
- }
catch (IOException e) { - e.printStackTrace();
- }
- }
- else{
- 打印登录界面
- }
Wellcome界面防止非法登录代码:
- HttpSession
hs;//创建session - String
names= req.getParameter("name");//得到传递过来的登录账户 - hs=req.getSession(true);//得到session
- String
user=(String)hs.getAttribute("user");//从session中得到已经登录账户 - String
pass=(String)hs.getAttribute("pass"); - if(user!=null){
- //此时查session表得到当前登录用户,在此处添加页面代码
- PrintWriter
pw; - try
{ - pw
= res.getWriter(); - pw.println("wellcome,"+user);
- }
catch (IOException e) { - //
TODO Auto-generated catch block - e.printStackTrace();
- }
- }else{
- //非法登录或者登录过期,重新登录
- try
{ - res.sendRedirect("login");
- }
catch (IOException e) { - //
TODO Auto-generated catch block - e.printStackTrace();
- }
- }
- }
2.servlet如何操作mysql数据库
关于如何操作数据库,因为之前有过C#操作sqlserver经验,所以很快入门
这里是我自己编写的一个类,放数据库操作的底层,包括加载驱动,驱动测试,判断mysql开启与否,查询,更新,关闭
关于操作数据库基本步骤:
1.先将包引进工程中--mysql-connector-java-5.1.24-bin.jar
2.import
3.创建数据库连接Connection
4.将连接的url,account,password
5.使用构造函数加载驱动,之后就可以进行查询或更新
6.使用定义的三个常数得到连接:
conn=DriverManager.getConnection(url,account,password);
7.创建语句执行对象:Statement
8.将语句执行:rs=stmt.executeQuery(SQL);rs=stmt.executeUpdate(SQL);
9.注意两种执行的返回值,查询返回ResultSet对象,更新返回受影响的int行数
在查询之前需要定义
- package
com.prince; - import
java.sql.*; - public
class mySqlConnection { - Connection
conn=null; - String
url="jdbc:MySQL://172.16.16.100:3306/mydb"; - String
account="root"; - String
password="123456"; - //构造函数加载驱动
- public
mySqlConnection(){ - try
{ - Class.forName("com.mysql.jdbc.Driver");
- }
- catch(Exception
e){ - System.out.println(e.getMessage());
- }
- }
- //驱动加载测试
- public
boolean mySqlConnection(){ - try
{ - Class.forName("com.mysql.jdbc.Driver");
- }
- catch(Exception
e){ - System.out.println(e.getMessage());
- return
false; - }
- return
true; - }
- //判断mySQL的开启与否
- public
boolean openOrNot() throws SQLException{ - conn=DriverManager.getConnection(url,account,password);
- if(conn.isClosed()){
- return
false; - }else{return
true;} - }
- //EQ方法用于查询数据库,返回结果集类型ResultSet
对象 - public
ResultSet EQ(String SQL){ - ResultSet
rs=null; - try{
- conn=DriverManager.getConnection(url,account,password);
- Statement
stmt=conn.createStatement(); - rs=stmt.executeQuery(SQL);
- }
- catch(Exception
e){ - System.out.println(e.getMessage());
- }
- return
rs; - }
- //UP用于更新修改数据库,返回int类型数据,表示影响到的行数,0为没影响
- public
int UP(String SQL){ - int
rs=0; - try{
- conn=
DriverManager.getConnection(url,account,password); - Statement
stmt=conn.createStatement(); - rs=stmt.executeUpdate(SQL);
- }
- catch(Exception
e){ - System.out.println(e.getMessage());
- }
- return
rs; - }
- //关闭数据库连接
- public
boolean close() throws SQLException{ - try{
- if(conn!=null){
- conn.close();
- }
- }
- catch(Exception
e){ - System.out.println(e.getMessage());
- return
false; - }
- if(conn.isClosed()){
- return
true; - }else{return
false;} - }
- }
- 在登录处理的java文件中使用数据库查询操作:
- public
void doGet (HttpServletRequest req, HttpServletResponse res){ - //req
用来获得浏览器的信息 - //res
用来发送信息 - res.setContentType("text/html;charset=gbk");//中文乱码解决
- mySqlConnection
msc=null; - ResultSet
rs=null; - hs=req.getSession(true);
- String
usernow=(String)hs.getAttribute("user"); - if(usernow!=null){
- try
{ - res.sendRedirect("wellcome");
- }
catch (IOException e) { - e.printStackTrace();
- }
- }
- else{
- try{
- //接收浏览器返回的用户名和密码
- String
u=req.getParameter("username"); - String
p=req.getParameter("passwd"); - //操作数据库
- msc=new
mySqlConnection(); - rs=msc.EQ("select
passwd from mydb.users where usernames='"+u+"' limit 1");//EQ是ExcuteQuerry查询的意思,调用查询函数 - //
验证: - if(rs.next()){//查到了一条记录,开始比对
- String
pw=rs.getString("passwd");//passwd是数据库中的列名 - if(pw.equals(p)){//合法跳转
- hs.setAttribute("user",u
); - hs.setAttribute("pass",p
); -
- res.sendRedirect("wellcome");//
写你要到的servlet的那个URL+变量 - }
- else{
- res.sendRedirect("login");
- }
- }
- else{
- //不合法
- res.sendRedirect("login");
- }
- }catch(Exception
ex){ - ex.printStackTrace();
- }
- finally{
- try{
- if(rs!=null){
- rs.close();
- }
- msc.close();//自定义关闭方法
- }catch(Exception
e){} - }
- }
- }