<%@ page contentType="text/html; charset=GB2312" language="java" import="java.sql.*,java.util.*" %>
<html>
<head>
<title>注册页面</title>
</head>
<body>
<from method="post" action="oo.jsp">
<table>
<tr>
<td>用户名:</td><td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码:</td><td><input type="text" name="unserpass"></td>
</tr>
<tr>
<td><input type="submit" value="注册" name="un"></td> <td><input type="reset" value="重置" name="up"></td>
</tr>
</table>
</from>
<% String username=request.getParameter("un");
String userpass=request.getParameter("up");
if(username=="")
{
out.print("用户名错误");
return ; }
if(userpass=="")
{
out.print("密码错误");
return ;
}
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:Date","sa","");
PreparedStatement ps=con.prepareStatement("insert into nn(username,userpass) values(?,?)");
ps.setString(1,username);
ps.setString(2,userpass);
int rows=ps.executeUpdate();
if(rows>0)
{
out.println("注册成功");
}
else
{
out.println("注册失败");
}
con.close();
}
catch(Exception e)
{
out.println("发生了异常:"+e.getMessage());
} %>
</body>
</html>请问各位大神、我直接在网页打开就是显示注册成功。我点注册和重置按钮根本没有用、但是数据库每次都会写入NULL空值、这是为什么呢?
<html>
<head>
<title>注册页面</title>
</head>
<body>
<from method="post" action="oo.jsp">
<table>
<tr>
<td>用户名:</td><td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码:</td><td><input type="text" name="unserpass"></td>
</tr>
<tr>
<td><input type="submit" value="注册" name="un"></td> <td><input type="reset" value="重置" name="up"></td>
</tr>
</table>
</from>
<% String username=request.getParameter("un");
String userpass=request.getParameter("up");
if(username=="")
{
out.print("用户名错误");
return ; }
if(userpass=="")
{
out.print("密码错误");
return ;
}
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:Date","sa","");
PreparedStatement ps=con.prepareStatement("insert into nn(username,userpass) values(?,?)");
ps.setString(1,username);
ps.setString(2,userpass);
int rows=ps.executeUpdate();
if(rows>0)
{
out.println("注册成功");
}
else
{
out.println("注册失败");
}
con.close();
}
catch(Exception e)
{
out.println("发生了异常:"+e.getMessage());
} %>
</body>
</html>请问各位大神、我直接在网页打开就是显示注册成功。我点注册和重置按钮根本没有用、但是数据库每次都会写入NULL空值、这是为什么呢?
String username=request.getParameter("un");
String userpass=request.getParameter("up");
//刚进来是不会进入这个if语句的。如果想进来需要这样写:if (username == null)因为你刚进来没有这个对象呢还。提交后才有
if(username=="")
{
out.print("用户名错误");
return ; }
if(userpass=="")
{
out.print("密码错误");
return ;
}
//所以上面这些都白写,不会执行。
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:Date","sa","");
PreparedStatement ps=con.prepareStatement("insert into nn(username,userpass) values(?,?)");
ps.setString(1,username);
ps.setString(2,userpass);
//刚一上来你就运行插入语句了,但是这时候的username和userpass都是null,写入数据库的当然也是null了。
int rows=ps.executeUpdate();
//成功插入了一条数据肯定会进来啊。
if(rows>0)
{
out.println("注册成功");
}
else
{
out.println("注册失败");
}
con.close();
}
catch(Exception e)
{
out.println("发生了异常:"+e.getMessage());
}
把下面这些代码写在一个jsp中
<%@ page contentType="text/html; charset=GB2312" language="java" import="java.sql.*,java.util.*" %>
<html>
<head>
<title>注册页面</title>
</head>
<body>
<from method="post" action="oo.jsp">
<table>
<tr>
<td>用户名:</td><td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码:</td><td><input type="text" name="unserpass"></td>
</tr>
<tr>
<td><input type="submit" value="注册" name="un"></td> <td><input type="reset" value="重置" name="up"></td>
</tr>
</table>
</from>
然后把保存到数据库的那些代码写到oo.jsp里面,这样你刚一进来就不会保存了,会等待你输入。然后你点击注册就会跳转到oo.jsp并执行你的储存到数据库的代码
这样该试过把IF里面的改成NULL、但是还是没用,直接输出用户名错误。能给出改法么?
首先你不能把输入和接收用户名密码的处理放在同一个文件里。
再一个java中==是比对的地址,你应该用!"".equlas(username)才对。 equals()函数才是比对字符用的。
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'test1.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">
</head>
<body>
<form method="post" name="myForm" action="test2.jsp">
<table>
<tr>
<td>用户名:</td><td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码:</td><td><input type="text" name="unserpass"></td>
</tr>
<tr>
<td><input type="submit" value="注册" name="un"></td> <td><input type="reset" value="重置" name="up"></td>
</tr>
</table>
</form>
</body>
</html>test2.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'test2.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>
<%
String username = request.getParameter("un");
String userpass = request.getParameter("up");
if ("".equals(username)) {
out.print("用户名不能为空!");
return;
} else if (username == null){
out.println("没有得到用户名输入框对象!");
}
if ("".equals(userpass)) {
out.print("密码不能为空!");
return;
} else if (userpass == null){
out.println("没有得到密码输入框对象!");
}
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:Date", "sa", "");
PreparedStatement ps = con.prepareStatement("insert into nn(username,userpass) values(?,?)");
ps.setString(1, username);
ps.setString(2, userpass);
int rows = ps.executeUpdate();
if (rows > 0) {
out.println("注册成功");
} else {
out.println("注册失败");
}
con.close();
} catch (Exception e) {
out.println("发生了异常:" + e.getMessage());
}
%> </body>
</html>
String username=request.getParameter("un");
String userpass=request.getParameter("up");
问题解答:
这里你取的是name=un,name=up
但是在你注册页面上面的name写的是name=username,name=unserpass
也就是说你这里取的话应该是这样取
String username=request.getParameter("username");
String userpass=request.getParameter("unserpass");
按你取的话 你取的是页面上面叫un和up的表单元数但是你页面上面没有这二个表单元素所以他就会取到null