如何在JSP中使用MYSQL中的登录界面1.和创建登录的JSP界面,action指向servlet中的方法2.创建一个新的字符用于存储新的数字和字符String msg="";3.获取JSP中输入的数据。
String account = request.getParameter("name");4.Try语句调用后面的方法。
先写方法方法:连接数据库和MYSQL中方法一样try{String driverName="com.mysql.jdbc.Driver";//数据库用户名String userName="root";//密码String userPasswd="123456";//数据库名String dbName="xueshenbiao";//表名String tableName="nima";//联结字符串Stringurl="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+u serPasswd;Class.forName("com.mysql.jdbc.Driver").newInstance();java.sql.Connectionconnection=DriverManager.getConnection(url);Statement statement = connection.createStatement();重点如下:之前习惯了用select去巡查SQL中数据,现在因为是验证登录所以只要比对是否有一样的数据即可,现在采用SELECT count(*)用于计数,如果返回是0说明不存在登录的用户,返回1说明存在,返回>1说明不止一个。
5.接下来寻找个数用while(rs.next())寻找个数6.和删除一样用result返回参数.7.此时的result有了自己的判断力用于前面的判断。
跳回前面的try语句用If()else语句来判断登录的界面8.当=1时。
说明登录成功跳转,跳转之前还需要先遍历下原始数据。
if(result==1){msg="输入正确";qw = getData();request.setAttribute("qw", qw);request.getRequestDispatcher("index.jsp").forward(request, response);9.当==0的时候,缓存数据用于错误界面的输出elseif(result==0){msg="您输入的帐号和密码不正确请重新输入";request.setAttribute("msg", msg);request.getRequestDispatcher("error.jsp").forward(request, response);10.同样>1的时候也一样if(result >1){msg="有重复数据";request.setAttribute("msg", msg);request.getRequestDispatcher("error.jsp").forward(request, response);}11.跳转到error的JSP首先取出之前缓存的MSG值(此时的MSG是字符串)判断是否为空,是空则给她一个空置,不为空返回String,因为object不可返回String,所以用toString强制转换。
BODY部分输出MSG<%=msg %>完整代码:登录JSP<%@page language="java"import="java.util.*"pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServ erPort()+path+"/";%><!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><SCRIPT language="javascript">var advInitTop=0;function inix( ){x=document.getElementById("advLayer").style.pixelTop; }function move( ){document.getElementById("advLayer").style.pixelTop=advInitTop+document.body.scrollTop; }window.onscroll=move ; //当页面滚动时调用move( )函数</SCRIPT><BODY onload="inix( )"><base href="<%=basePath%>"><title>My JSP 'denglu.jsp' starting page</title><meta http-equiv="pragma"content="no-cache"><meta http-equiv="cache-control"content="no-cache"><meta http-equiv="expires"content="0"><meta http-equiv="keywords"content="keyword1,keyword2,keyword3"> <meta http-equiv="description"content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body bgcolor=cyan><DIV id="advLayer"style="position:absolute; left:550px;top:29px; width:144px; height:95px; z-index:1;"><A href="http://localhost:8080/tongxunlv/tongxunlv"><IMG src="882824467 _541795419.summ.jpg"width="180"height="230"border="0"/></A></DIV><font size=1><p>系统输入人员登录<form action="tongxunlv?openration=nimama"Method="post"><br>输入帐号:<input type=text name="name"><br>输入密码:<input type=text name="nima"><br>输入验证密码:<input type=text name="yanzhen"><br><input type=submit value="提交"></form>></body></html>Servlet中的部分代码elseif(opeationName.equals("nimama")){String msg="";String account = request.getParameter("name");if(account==null){account="";}String secret=request.getParameter("nima");if(secret==null){account="";}try{int result = doNimama(account,secret);List<shuxing>qw = new ArrayList<shuxing>();if(result==1){msg="输入正确";qw = getData();request.setAttribute("qw", qw);request.getRequestDispatcher("index.jsp").forward(request, response);}elseif(result==0){msg="您输入的帐号和密码不正确请重新输入";request.setAttribute("msg", msg);request.getRequestDispatcher("error.jsp").forward(request, response);}elseif(result >1){msg="有重复数据";request.setAttribute("msg", msg);request.getRequestDispatcher("error.jsp").forward(request, response);}}catch(Exception e){e.printStackTrace();}publicint doNimama(String account,String secret) {int result = 0;try{String driverName="com.mysql.jdbc.Driver";//数据库用户名String userName="root";//密码String userPasswd="123456";//数据库名String dbName="xueshenbiao";//表名String tableName="nima";//联结字符串Stringurl="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+u serPasswd;Class.forName("com.mysql.jdbc.Driver").newInstance();java.sql.Connectionconnection=DriverManager.getConnection(url);Statement statement = connection.createStatement();String condition="SELECT count(*) FROM nima WHERE name = "+"'"+account+"'andnima = "+"'"+secret+"' ";ResultSetrs = statement.executeQuery(condition);//获得数据结果集合ResultSetMetaDatarmeta = rs.getMetaData();//确定数据集的列数,亦字段数int numColumns=rmeta.getColumnCount();while(rs.next()){result =rs.getInt(1);}}catch(Exception e){e.printStackTrace();}return result;}。