本文目錄一覽:
jsp登陸界面源代碼
1、login.jsp文件
%@ page language=”java” contentType=”text/html; charset=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
tdinput 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/html; charset=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/html; charset=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:1433;DatabaseName=***”;
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/html; charset=utf-8″
pageEncoding=”utf-8″%
!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “”
html
head
meta http-equiv=”Content-Type” content=”text/html; charset=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”, “0;url=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”, “0;url=user_login.jsp”);
}
else if(passwordInDataBase.equals(password))
{
out.print(“script language=’javaScript’ alert(‘登錄成功’);/script”);
response.setHeader(“refresh”, “0;url=loginSucces.jsp”);
}
else
{
out.print(“script language=’javaScript’ alert(‘密碼錯誤’);/script”);
response.setHeader(“refresh”, “0;url=user_login.jsp”);
}
}
%
/body
/html
3、loginSucces.jsp文件
%@ page language=”java” contentType=”text/html; charset=utf-8″
pageEncoding=”utf-8″%
!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “”
html
head
meta http-equiv=”Content-Type” content=”text/html; charset=ISO-8859-1″
titleInsert 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/html; charset=utf-8″
pageEncoding=”utf-8″%
!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “”
html
head
meta http-equiv=”Content-Type” content=”text/html; charset=ISO-8859-1″
title登錄界面/title
/head
body background=”C:\Users\win8\workspace\Login\image\9dcbdc339e72a5663b5c289fb5573c13_10.jpg”
center
brbrbrbrbrbr
h1 style=”color:yellow”Login/h1
br
form name=”loginForm” action=”login_check.jsp” method=”post”
table Border=”0″
tr
td賬號/td
tdinput type=”text” name=”username”/td
/tr
tr
td密碼/td
tdinput type=”password” name=”password”
/td
/tr
/table
br
input type=”submit” value=”登錄” style=”color:#BC8F8F”
/form
/center
/body
/html
用jsp怎樣做一個用戶登錄界面
//jsp文件,登錄界面
%@page pageEncoding=”utf-8″ contentType=”text/html; charset = utf-8″%
body
h1Register/h1
hr /
form action=”login” method=”post”
用戶名:
input type=”text” name=”username” id=”text” /
密 碼:
input type=”password” name=”pwd” id=”text” //
input type=”submit” value=”登錄” id=”button” /
/form
/body
//這是server文件,web.xml部署名字login
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class AddUser extends HttpServlet {
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(“text/html;charset=utf-8”);
request.setCharacterEncoding(“utf-8”);
String username=request.getParameter(“username”);
String pwd=request.getParameter(“pwd”);
try{
if(username.equals(”name”)pwd.equals(“pwd”) ){
system.out.print(“登錄成功”);
}else{
system.out.print(“error”);
}
}catch(Exception e){
e.printStackTrace();
}
}
}
簡單的登錄界面,不需要數據庫,如果要鏈接數據庫就要判斷很多了
編寫用戶註冊於登錄的JSP頁面的全部程序代碼
3個jsp文件,第一個是login.jsp,第二個是judge.jsp,第三個是afterLogin.jsp
%@ page language=”java” contentType=”text/html; charset=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
tdinput type=”submit” value=”登錄” style=”background-color:pink” input type=”reset” value=”重置” style=”background-color:red”/td
/tr
/table
/form
/body
/html
%@ page language=”java” contentType=”text/html; charset=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”)) {
%
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/html; charset=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
用jsp編寫一個很簡單的登陸頁面
login.JSP
注: 由於時間倉促,程序可能有細小的問題!但絕對可以滿足你的需要了!如果你知道JSP什麼原理;那麼小的問題不是什麼問題了! 共同學習~~~
%@ page contentType = “text/html;charset=GB2312” %
%@ page import = “java.sql.*” %
html
head
title/title
/head
body bgcolor=LightBlue
div align=”center”
br
H1歡迎光臨企業辦公平台/H1
form name=”form1″ method=”post” action=”” target=”_self”
table width=”90%”
tr
td width=”50%” height=”30″ align=”right”用戶名:/td
td width=”50%” height=”30″ align=”left” input type=”text” name=”UserName”/td
/tr
tr
td width=”50%” height=”30″ align=”right”密碼:/td
td width=”50%” height=”30″ align=”left” input type=”password” name=”UserPassword”/td
/tr
td width=”100%” height=”40″ align=”center” colspan=”2″
input type=”submit” name=”sub” value=”登錄”
/td
/tr
/table
/form
/div
% try
{Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Connection conn=DriverManager.getConnection(“jdbc:odbc:a”,”root”,”database1″);
Statement stmt = conn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs = null;
String name =new String(request.getParameter(“
UserName”).getBytes(“8859_1”));
String passwd =new String(request.getParameter(“
UserPassword”).getBytes(“8859_1”));
if(String_sql==null)
{
out.println(“登陸名不可以為空”);
out.println(“HR”);
}
else{
String_sql=”select * from student where name =”+name+” and password=”+psword+;
rs=stmt.executeQuery(String_sql)
while(rs.next())
{ out.println(“登陸成功!”);
out.println(“您的用戶名是:”+name)
out.println(“您的密碼是:”+password);
rs.close();
stmt.close();
conn.close();}
}
Catch( Exception e )
{out.println(“font color=red size=5B”);
out.println(“出錯了!”);
out.println(“/B/font”);
}%
/body
/html
jsp下登錄界面的方法
關鍵是sql語句,如果你用jdbc+dao的話就是
select count(當前表的id名比如id) from user where userid= 接收的用戶名 and password=接收的密碼
然後取得值看該值是否大於0,如果大於0返回一個true值或者直接返回該值,然後判斷只要大於0就表示登錄成功,和數據庫中一樣。
你可以通過request跳轉,如果登錄成功就跳轉到登錄成功界面,並把你的登錄信息保存到session中。否則就直接重定向到登錄界面讓用戶重新登錄。
你可以去百度搜索
java jsp 登錄 csdn
這樣可以從csdn下載相關的登錄實例,裡面有詳細的代碼,可以最大程度上幫助你。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/182236.html