本文目录一览:
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;
}
}
上面没有做中文处理,中文用户名应该会错误,你自己做吧
原创的,你试试
jsp怎么获取当前页面的url
当一个url过来时,如:,在hello.jsp页面,我们可以这样得到url:
代码如下:
% String basepath
=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort() ;
String Path = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"+request.getContextPath()+"/";
String uri=request.getRequestURI();
uri=uri.substring(uri.lastIndexOf("/")+1); //获得是最后的hello.jsp
%
(上例中,Path路径就是图中的)
说明:
1.request.getContextPath() 返回站点的根目录,如:‘’/demo‘’
2.request.getRealpath("/")得到的是实际的物理路径,也就是你的项目所在服务器中的路径
3.request.getScheme() 等到的是协议名称,默认是http
4.request.getServerName() 得到的是在服务器的配置文件中配置的服务器名称 比如:localhost .baidu.com 等等
5.request.getServerPort() 得到的是服务器的配置文件中配置的端口号 比如 8080等等
OK,满意的话请好评!O(∩_∩)O~
jsp页面如何获取登陆页面的用户名和密码
用post请求提交到服务器后端 用request.getParameter(控件的Name属性)获取