xuanke java_HandleLogin.java

package myservlet.control;

import mybean.data.*;

import java.sql.*;

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.util.*;

public class HandleLogin extends HttpServlet{

public void init(ServletConfig config) throws ServletException{

super.init(config);

try{

Class.forName("com.mysql.jdbc.Driver");

}

catch(Exception e){}

}

public String handleString(String s){

try{ byte bb[]=s.getBytes("iso-8859-1");

s=new String(bb);

}

catch(Exception ee){}

return s;

}

public void doPost(HttpServletRequest request,HttpServletResponse response)

throws ServletException,IOException{

Connection con;

Statement sql;

String logname=request.getParameter("logname").trim(),

password=request.getParameter("password").trim();

logname=handleString(logname);

password=handleString(password);

String uri="jdbc:mysql://127.0.0.1/xuankebiao?"+

"user=root&password=&characterEncoding=gb2312";

boolean boo=(logname.length()>0)&&(password.length()>0);

try{

con=DriverManager.getConnection(uri);

String condition="select * from user where logname = '"+logname+

"' and password ='"+password+"'";

sql=con.createStatement();

if(boo){

ResultSet rs=sql.executeQuery(condition);

boolean m=rs.next();

if(m==true){

//调用登录成功的方法:

success(request,response,logname,password);

RequestDispatcher dispatcher=

request.getRequestDispatcher("xianshi.jsp");//转发

dispatcher.forward(request,response);

}

else{

String backNews="您输入的用户名不存在,或密码不般配";

//调用登录失败的方法:

fail(request,response,logname,backNews);

}

}

else{

String backNews="请输入用户名和密码";

fail(request,response,logname,backNews);

}

con.close();

}

catch(SQLException exp){

String backNews=""+exp;

fail(request,response,logname,backNews);

}

}

public void doGet(HttpServletRequest request,HttpServletResponse response)

throws ServletException,IOException{

doPost(request,response);

}

public void success(HttpServletRequest request,HttpServletResponse response

,String logname,String password) {

Login loginBean=null;

HttpSession session=request.getSession(true);

try{ loginBean=(Login)session.getAttribute("loginBean");

if(loginBean==null){

loginBean=new Login(); //创建新的数据模型

session.setAttribute("loginBean",loginBean);

loginBean=(Login)session.getAttribute("loginBean");

}

String name =loginBean.getLogname();

if(name.equals(logname)) {

loginBean.setBackNews(logname+"已经登录了");

loginBean.setLogname(logname);

}

else { //数据模型存储新的登录用户

loginBean.setBackNews(logname+"登录成功");

loginBean.setLogname(logname);

}

}

catch(Exception ee){

loginBean=new Login();

session.setAttribute("loginBean",loginBean);

loginBean.setBackNews(logname+"登录成功");

loginBean.setLogname(logname);

}

}

public void fail(HttpServletRequest request,HttpServletResponse response

,String logname,String backNews) {

response.setContentType("text/html;charset=utf-8");

try {

PrintWriter out=response.getWriter();

out.println("

");

out.println("

"+logname+"登录反馈结果
"+backNews+"

") ;

out.println("登录页面");

//out.println("主页");

out.println("");

}

catch(IOException exp){}

}

}

一键复制

编辑

Web IDE

原始数据

按行查看

历史

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值