JSP中将表单数据提交给本页面的函数处理如何实现与数据库的连接对比
我写了代码如下,页面名称是login.jsp,formaction也写的是login.jsp,这里是不是就有问题?因为我提交后不能比对数据库,求牛人帮我看看<%@page...
我写了代码如下,页面名称是login.jsp,form action也写的是login.jsp,这里是不是就有问题?因为我提交后不能比对数据库,求牛人帮我看看
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="java.sql.*" %>
<%@ page import="com.mysql.jdbc.Driver"%>
<html>
<body>
<form action="login.jsp" method="post" name="form1" onsubmit="return on_submit">
用户名:<input type="text" name="username"/></br>
密码: <input type="password" name="password"/></br>
<input type="submit" value="登录"/> <a href="regist.jsp"><font color="red">新用户注册</font></a>
</form>
</body>
<script language="javascript">
function on_submit(){
if(form1.username.value==""){
alert("用户名不能为空,请重新输入");
form1.username.focus();
return false;
}
if(form1.password.value==""){
alert("密码不能为空,请重新输入");
form1.password.focus();
return false;
}else{
Connection con;
Statement sql;
ResultSet rs;
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e){
e.printStackTrace();
}
try
{
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/registDatabase","root","890510");
sql=conn.createStatement();
rs=sql.executeQuery("SELECT * FROM userTable where username='"+ name +"'and password='"+password+"'");
if(rs.next()){
response.sendRedirect("success.jsp");
}else{
alert("用户名或密码错误!");
}
}
}
</script>
</html> 展开
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="java.sql.*" %>
<%@ page import="com.mysql.jdbc.Driver"%>
<html>
<body>
<form action="login.jsp" method="post" name="form1" onsubmit="return on_submit">
用户名:<input type="text" name="username"/></br>
密码: <input type="password" name="password"/></br>
<input type="submit" value="登录"/> <a href="regist.jsp"><font color="red">新用户注册</font></a>
</form>
</body>
<script language="javascript">
function on_submit(){
if(form1.username.value==""){
alert("用户名不能为空,请重新输入");
form1.username.focus();
return false;
}
if(form1.password.value==""){
alert("密码不能为空,请重新输入");
form1.password.focus();
return false;
}else{
Connection con;
Statement sql;
ResultSet rs;
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e){
e.printStackTrace();
}
try
{
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/registDatabase","root","890510");
sql=conn.createStatement();
rs=sql.executeQuery("SELECT * FROM userTable where username='"+ name +"'and password='"+password+"'");
if(rs.next()){
response.sendRedirect("success.jsp");
}else{
alert("用户名或密码错误!");
}
}
}
</script>
</html> 展开
3个回答
展开全部
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="源贺咐java.sql.*" %>
<%@ page import="com.mysql.jdbc.Driver"%>
<html>
<body>
<%
String username=request.getParameter("username").toString();
String paw=request.getParameter("password").toString();
if((username!=""&&username!=null)||(paw!=""&&paw!=""&&paw!=null))
{
Connection con;
Statement sql;
ResultSet rs;
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e){
e.printStackTrace();
}
try
{
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/registDatabase","root","890510");
sql=conn.createStatement();
rs=sql.executeQuery("SELECT * FROM userTable where username='"雹纯+ name +"'and password='"+password+"'");
if(rs.next()){
response.sendRedirect("success.jsp");
}else{
alert("用户名或密码错误!");
}
%>
<form action="login.jsp" method="post" name="form1" onsubmit="return on_submit">
用户名:<input type="text" name="username"/></br>
密码: <input type="password" name="password"/></br>
<input type="submit" value="登录"/> <a href="regist.jsp"><font color="red">新用户注册</font></a>
</form>
</body>
<script language="javascript">
function on_submit(){
if(form1.username.value==""){
alert("用户名不能为空,请重新输入拍哗");
form1.username.focus();
return false;
}
if(form1.password.value==""){
alert("密码不能为空,请重新输入");
form1.password.focus();
return false;
}else{
}
}
</script>
</html>
<%@ page import="源贺咐java.sql.*" %>
<%@ page import="com.mysql.jdbc.Driver"%>
<html>
<body>
<%
String username=request.getParameter("username").toString();
String paw=request.getParameter("password").toString();
if((username!=""&&username!=null)||(paw!=""&&paw!=""&&paw!=null))
{
Connection con;
Statement sql;
ResultSet rs;
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e){
e.printStackTrace();
}
try
{
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/registDatabase","root","890510");
sql=conn.createStatement();
rs=sql.executeQuery("SELECT * FROM userTable where username='"雹纯+ name +"'and password='"+password+"'");
if(rs.next()){
response.sendRedirect("success.jsp");
}else{
alert("用户名或密码错误!");
}
%>
<form action="login.jsp" method="post" name="form1" onsubmit="return on_submit">
用户名:<input type="text" name="username"/></br>
密码: <input type="password" name="password"/></br>
<input type="submit" value="登录"/> <a href="regist.jsp"><font color="red">新用户注册</font></a>
</form>
</body>
<script language="javascript">
function on_submit(){
if(form1.username.value==""){
alert("用户名不能为空,请重新输入拍哗");
form1.username.focus();
return false;
}
if(form1.password.value==""){
alert("密码不能为空,请重新输入");
form1.password.focus();
return false;
}else{
}
}
</script>
</html>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你这个代码太紊乱了。你怎么能在javascript中直接这样写java代码?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
javascript不是java@@
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询