怎么用JSP做个登录界面要调用数据库的

怎么用JSP做个登录界面要调用数据库的,第1张

你当前提交到还是login.jsp,如果就是要在login.jsp里完成的话,可以直接在body标签下面加入

<%

//验证的java语句,就是执行先获得提交的参数,可以用request.getparameter("username"),密码同上,然后就是连接数据库进行比较的代码,

但是最好还是action到相应的servlet来处理,你可以通过eclipse等ide来创建,具体的还是多百度几下,网上这种东西太多了

可以直接在jsp中写java代码,用<%%>括起来java代码就可以了;

页面与后台数据交互,就是后台有截获页面请求的方法,页面通过url请求把数据拼接到url中向后台请求,后台截获请求响应匹配的方法,并且拿到数据,然后处理完返回数据给页面,完成数据交互。

方式的话可以是form提交,ajax访问,servlet访问等。

1、login.jsp文件

<%@ page language="java" contentType="text/htmlcharset=GB18030"

pageEncoding="GB18030"%>

<%@ page import="java.util.*" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>登录页面</title>

</head>

<body>

<form name="loginForm" method="post" action="judgeUser.jsp">

<table>

<tr>

<td>用户名:<input type="text" name="userName" id="userName"></td>

</tr>

<tr>

<td>密码:<input type="password" name="password" id="password"></td>

</tr>

<tr>

<td><input type="submit" value="登录" style="background-color:pink"><input

type="reset" value="重置" style="background-color:red"></td>

</tr>

</table>

</form>

</body>

</html>

2、judge.jsp文件

<%@ page language="java" contentType="text/htmlcharset=GB18030"

pageEncoding="GB18030"%>

<%@ page import="java.util.*" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>身份验证</title>

</head>

<body>

<%

request.setCharacterEncoding("GB18030")

String name = request.getParameter("userName")

String password = request.getParameter("password")

if(name.equals("abc")&&password.equals("123")) {

3、afterLogin.jsp文件

%>

<jsp:forward page="afterLogin.jsp">

<jsp:param name="userName" value="<%=name%>"/>

</jsp:forward>

<%

}

else {

%>

<jsp:forward page="login.jsp"/>

<%

}

%>

</body>

</html>

<%@ page language="java" contentType="text/htmlcharset=GB18030"

pageEncoding="GB18030"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>登录成功</title>

</head>

<body>

<%

request.setCharacterEncoding("GB18030")

String name = request.getParameter("userName")

out.println("欢迎你:" + name)

%>

</body>

</html>

扩展资料:

java web登录界面源代码:

1、Data_uil.java文件

import java.sql.*

public class Data_uil

{

public  Connection getConnection()

{

try{

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")

}catch(ClassNotFoundException e)

{

e.printStackTrace()

}

String user="***"

String password="***"

String url="jdbc:sqlserver://127.0.0.1:1433DatabaseName=***"

Connection con=null

try{

con=DriverManager.getConnection(url,user,password)

}catch(SQLException e)

{

e.printStackTrace()

}

return con

}

public  String selectPassword(String username)

{

Connection connection=getConnection()

String sql="select *from login where username=?"

PreparedStatement preparedStatement=null

ResultSet result=null

String password=null

try{

preparedStatement=connection.prepareStatement(sql)

preparedStatement.setString(1,username)

result=preparedStatement.executeQuery()//可执行的     查询

if(result.next())

password=result.getString("password")

}catch(SQLException e){

e.printStackTrace()

}finally

{

close(preparedStatement)

close(result)

close(connection)

}

System.out.println("找到的数据库密码为:"+password)

return password 

}

public  void close (Connection con)

{

try{

if(con!=null)

{

con.close()

}

}catch(SQLException e)

{

e.printStackTrace()

}

}

public  void close (PreparedStatement preparedStatement)

{

try{

if(preparedStatement!=null)

{

preparedStatement.close()

}

}catch(SQLException e)

{

e.printStackTrace()

}

}

public  void close(ResultSet resultSet)

{

try{

if(resultSet!=null)

{

resultSet.close()

}

}catch(SQLException e)

{

e.printStackTrace()

}

}

}

2、login_check.jsp:文件

<%@ page language="java" contentType="text/htmlcharset=utf-8"

pageEncoding="utf-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8">

<title>验证用户密码</title>

</head>

<body>

<jsp:useBean id="util" class="util.Data_uil" scope="page" />

<%

String username=(String)request.getParameter("username")

String password=(String)request.getParameter("password")

if(username==null||"".equals(username))

{

out.print("<script language='javaScript'>alert('用户名不能为空')</script>")

response.setHeader("refresh", "0url=user_login.jsp")

}

else

{

System.out.println("输入的用户名:"+username)

String passwordInDataBase=util.selectPassword(username)

System.out.println("密码:"+passwordInDataBase)

if(passwordInDataBase==null||"".equals(passwordInDataBase))

{

out.print("<script language='javaScript'>alert('用户名不存在')</script>")

response.setHeader("refresh", "0url=user_login.jsp")

}

else if(passwordInDataBase.equals(password))

{

out.print("<script language='javaScript'>alert('登录成功')</script>")

response.setHeader("refresh", "0url=loginSucces.jsp")

}

else

{

out.print("<script language='javaScript'>alert('密码错误')</script>")

response.setHeader("refresh", "0url=user_login.jsp")

}

}

%>

</body>

</html>

3、loginSucces.jsp文件

<%@ page language="java" contentType="text/htmlcharset=utf-8"

pageEncoding="utf-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

<hr size="10" width="26%" align="left" color="green">

<font size="6" color="red" >登录成功 </font>

<hr size="10" width="26%" align="left" color="green">

</body>

</html>

4、user_login.jsp文件

<%@ page language="java" contentType="text/htmlcharset=utf-8"

pageEncoding="utf-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=ISO-8859-1">

<title>登录界面</title>

</head>

<body  background="C:\Users\win8\workspace\Login\image\9dcbdc339e72a5663b5c289fb5573c13_10.jpg" >

<center>

<br><br><br><br><br><br>

<h1 style="color:yellow">Login</h1>

<br>

<form name="loginForm" action="login_check.jsp" method="post"> 

<table Border="0" >

<tr >

<td>账号</td>

<td><input type="text" name="username"></td>

</tr>

<tr>

<td>密码</td>

<td><input type="password" name="password">

</td>

</tr>

</table>

<br>

<input type="submit" value="登录" style="color:#BC8F8F">

</form>

</center>

</body>

</html>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/6763386.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-27
下一篇 2023-03-27

发表评论

登录后才能评论

评论列表(0条)

保存