jsp页面如何获取登陆页面的用户名和密码

登陆页面里用户名密码里没有id,name,我想获取原密码,和新修改的密码做比较,不能相同,应该在jsp页面上怎么写,能做到

登陆界面的<input>标签中没有name属性你怎么可能获得到登陆界面的密码,下面有两种,应该有你要的。
<html>
<head>
<title>用户注册</title>
<script type="text/javascript" language="javascript">
function check()
{
with(document.all){
if(password1.value!=password2.value)
{
alert("您的密码不一致,请重新输入!");
password1.value = "";
password2.value = "";
}
else document.forms[0].submit();
}
}
</script>
</head>

<body>
<center>
<form action="addUserServlet" method="post" name="myform">
<h2>用户注册</h2>
<br>
用户名:<input type="text" name="newuser">
<br>
原密码:<input type="password" name="password1">
<br>
新密码:<input type="password" name="password2">
<br>
<input type="button" value="提交" onclick="check()">
<input type="reset" value="重置">
</form>
</center>
</body>
</html>
上面就是用script来比较,如果不相同,就会弹出一个窗口显示密码不一样然后清空密码框,如果两个密码相同就跳转acction地址。
上面是注册时候用的。
如果本身就有用户名和密码而你想要获取数据库里面的密码的话你还需要一个servlet以及一个数据库连接类。下面应该是你要的东西,获取数据库里面的密码和用户名并且与输入的进行比较:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userName=request.getParameter("username");//取得用户名
String password=request.getParameter("password");//取得密码
DBTest db=new DBTest();
boolean canLogin=db.loginSuccess(userName, password);
if (canLogin) {
System.out.println("登陆成功");
}else{
System.out.println("用户名或密码错误");
}
}

数据库连接类:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Statement;

public class DBTest {
boolean bInited = false;
//加载驱动
public void initJdBC() throws ClassNotFoundException{
//加载MYSQL JDBC驱动程序
Class.forName("com.mysql.jdbc.Driver");
bInited = true;
System.out.println("Success loading Mysql Driver!");
}
public Connection getConnection() throws ClassNotFoundException,SQLException{
if (!bInited) {
initJdBC();
}
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "数据库用户名", "连接数据库的密码");
return conn;
}
public boolean loginSuccess(String userName,String password){
boolean returnValue = false;
String sql = "select * from user";
Connection conn=null;
java.sql.Statement stmt=null;
ResultSet rs=null;
try {
conn=getConnection();
stmt = conn.createStatement();
rs=stmt.executeQuery(sql);
while (rs.next()) {
String userNameInDB=rs.getString("name");
String passwordInDB=rs.getString("pwd");
if (userNameInDB.equals(userName)&&passwordInDB.equals(password)) {
returnValue=true;
break;
}
}
} catch (ClassNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return returnValue;
}
}
上面没有做中文处理,中文用户名应该会错误,你自己做吧
原创的,你试试
温馨提示:答案为网友推荐,仅供参考
第1个回答  2022-01-24

登陆界面的<input>标签中没有name属性你怎么可能获得到登陆界面的密码,下面有两种,应该有你要的。
<html>
<head>
<title>用户注册</title>
<script type="text/javascript" language="javascript">
function check()
{
with(document.all){
if(password1.value!=password2.value)
{
alert("您的密码不一致,请重新输入!");
password1.value = "";
password2.value = "";
}
else document.forms[0].submit();
}
}
</script>
</head>

<body>
<center>
<form action="addUserServlet" method="post" name="myform">
<h2>用户注册</h2>
<br>
用户名:<input type="text" name="newuser">
<br>
原密码:<input type="password" name="password1">
<br>
新密码:<input type="password" name="password2">
<br>
<input type="button" value="提交" onclick="check()">
<input type="reset" value="重置">
</form>
</center>
</body>
</html>
上面就是用script来比较,如果不相同,就会弹出一个窗口显示密码不一样然后清空密码框,如果两个密码相同就跳转acction地址。
上面是注册时候用的。
如果本身就有用户名和密码而你想要获取数据库里面的密码的话你还需要一个servlet以及一个数据库连接类。下面应该是你要的东西,获取数据库里面的密码和用户名并且与输入的进行比较:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userName=request.getParameter("username");//取得用户名
String password=request.getParameter("password");//取得密码
DBTest db=new DBTest();
boolean canLogin=db.loginSuccess(userName, password);
if (canLogin) {
System.out.println("登陆成功");
}else{
System.out.println("用户名或密码错误");
}

第2个回答  2014-05-04
我也是新手 不过是在servlet里写 先获取表单的密码 然后和数据库的密码作比较 最后再反应
第3个回答  2014-05-04
感觉你说的逻辑好乱
相似回答