關於不同角色登錄jsp源碼的信息

本文目錄一覽:

在jsp中,如何實現普通用戶和管理員登陸後跳轉到不同的點jsp頁面

在jsp中可以通過角色控制表跳轉不同的頁面。

參考代碼如下:

package myservlet;

import mybean.*;

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.sql.*;

public class IsLogin extends HttpServlet{

public void init(ServletConfig config) throws ServletException{

super.init(config);

}

public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{

//接收參數

String user=request.getParameter(“user”);

String password=request.getParameter(“password”);

String actor=request.getParameter(“actor”);

//加載驅動,建立連接

Connection con;

Statement sql;

ResultSet rs;

try{

Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver”);

}catch(Exception e){

System.out.print(e);

}

try{

String uri=”jdbc:sqlserver://127.0.0.1:1433;DatabaseName=student”;

con=DriverManager.getConnection(uri,”sa”,”940712″);//數據庫的登錄名 sa 940712

sql=con.createStatement();

//通過if語句判斷角色,將其賬號密碼與數據庫的userInf內的信息進行比對(角色的賬號密碼統一存儲在UserInf表中)

//若正確,轉發至角色對應的登錄成功界面;若沒有,統一轉發至出錯界面,提供返回鏈接供重新登錄

if(actor==”student”){

rs=sql.executeQuery(“select userIs,password from userInf where actor=’student'”);

while(rs.next()){

if(user==rs.getString(1) password==rs.getString(2)){

RequestDispatcher dispatcher=request.getRequestDispatcher(“loginSuccessS.jsp”);

dispatcher.forward(request,response);

}

}

RequestDispatcher dispatcher=request.getRequestDispatcher(“loginError.jsp”);

dispatcher.forward(request,response);

}

//普通用戶角色控制

else if(actor==”teacher”){

rs=sql.executeQuery(“select userIs,password from userInf where actor=’teacher'”);

while(rs.next()){

if(user==rs.getString(1) password==rs.getString(2)){

RequestDispatcher dispatcher=request.getRequestDispatcher(“loginSuccessT.jsp”);

dispatcher.forward(request,response);

}

}

RequestDispatcher dispatcher=request.getRequestDispatcher(“loginError.jsp”);

dispatcher.forward(request,response);

}

//管理員角色控制

else if(actor==”admin”){

rs=sql.executeQuery(“select userIs,password from userInf where actor=’admin'”);

while(rs.next()){

if(user==rs.getString(1) password==rs.getString(2)){

RequestDispatcher dispatcher=request.getRequestDispatcher(“loginSuccessA.jsp”);

dispatcher.forward(request,response);

}

}

RequestDispatcher dispatcher=request.getRequestDispatcher(“loginError.jsp”);

dispatcher.forward(request,response);

}

}catch(SQLException e){

//System.out.print(“您的賬號或密碼錯誤,請返回重新輸入”);

RequestDispatcher dispatcher=request.getRequestDispatcher(“loginError.jsp”);

dispatcher.forward(request,response);

}

}

}

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裏面不同的角色權限進入不同的頁面,我知道它的實現的原理但是不曉得怎麼寫代碼

1.

動態INCLUDE用jsp:include動作實現 jsp:include page=”included.jsp” flush=”true” /它總是會檢查所含文件中的變化,適合用於包含動態頁面,並且可以帶參數。

靜態INCLUDE用include偽碼實現,定不會檢查所含文件的變化,適用於包含靜態頁面%@ include file=”included.htm” %

2.

客戶訪問這個頁面的時候,只要該文件沒有發生過更改,JSP引擎就直接調用已經裝載的Servlet。如果已經做過修改的話,那就會再次執行以上過程,翻譯、編譯並裝載。其實這就是所謂的「第一人懲罰」。因為首次訪問的時候要執行一系列以上的過程,所以會耗費一些時間;以後的訪問就不會這樣了。

3.

jsp是由servlet發展過來的,你應該知道jsp主要是用來做頁面顯示的,早期jsp沒出現之前servlet擔當這一角色,servlet編寫前端頁面時非常繁瑣效率低的,jsp實在servlet的基礎上做了一層封裝,更傾向於表現層,現在的servlet更傾向於業務邏輯層,這樣做的目的也就是分層.把業務層和表現層的代碼分離開來,便於開發和維護.jsp在運行的第一次速度會比較慢,因為第一次他需要編譯成servlet的文件,實際上你運行的就是一個jsp翻譯過來的servlet.

原創文章,作者:KTHH,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/139870.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
KTHH的頭像KTHH
上一篇 2024-10-04 00:23
下一篇 2024-10-04 00:23

相關推薦

  • 用不同的方法求素數

    素數是指只能被1和自身整除的正整數,如2、3、5、7、11、13等。素數在密碼學、計算機科學、數學、物理等領域都有着廣泛的應用。本文將介紹幾種常見的求素數的方法,包括暴力枚舉法、埃…

    編程 2025-04-29
  • 雲智直聘 源碼分析

    本文將會對雲智直聘的源碼進行分析,包括前端頁面和後端代碼,幫助讀者了解其架構、技術實現以及對一些常見的問題進行解決。通過本文的閱讀,讀者將會了解到雲智直聘的特點、優勢以及不足之處,…

    編程 2025-04-29
  • Python函數名稱相同參數不同:多態

    Python是一門面向對象的編程語言,它強烈支持多態性 一、什麼是多態多態是面向對象三大特性中的一種,它指的是:相同的函數名稱可以有不同的實現方式。也就是說,不同的對象調用同名方法…

    編程 2025-04-29
  • Java 監控接口返回信息報錯信息怎麼處理

    本文將從多個方面對 Java 監控接口返回信息報錯信息的處理方法進行詳細的闡述,其中包括如何捕獲異常、如何使用日誌輸出錯誤信息、以及如何通過異常處理機制解決報錯問題等等。以下是詳細…

    編程 2025-04-29
  • 從不同位置觀察同一個物體,看到的圖形一定不同

    無論是在平時的生活中,還是在科學研究中,都會涉及到觀察物體的問題。而我們不僅要觀察物體本身,還需要考慮觀察的位置對觀察結果的影響。從不同位置觀察同一個物體,看到的圖形一定不同。接下…

    編程 2025-04-28
  • 使用Python爬蟲獲取電影信息的實現方法

    本文將介紹如何使用Python編寫爬蟲程序,來獲取和處理電影數據。需要了解基本的Python編程語言知識,並使用BeautifulSoup庫和Requests庫進行爬取。 一、準備…

    編程 2025-04-28
  • 兩個域名指向同一IP不同端口打開不同網頁的實現方法

    本文將從以下幾個方面詳細闡述兩個域名指向同一個IP不同端口打開不同網頁的實現方法。 一、域名解析 要實現兩個域名指向同一個IP不同端口,首先需要進行域名解析。在域名解析的時候,將這…

    編程 2025-04-28
  • Python網站源碼解析

    本文將從多個方面對Python網站源碼進行詳細解析,包括搭建網站、數據處理、安全性等內容。 一、搭建網站 Python是一種高級編程語言,適用於多種領域。它也可以用於搭建網站。最常…

    編程 2025-04-28
  • 如何用python鍵盤控制角色

    本文將從多個方面詳細闡述如何用python鍵盤控制角色。 一、安裝pygame庫 首先我們需要安裝pygame庫,它是基於SDL庫的Python多媒體庫,用於開發2D遊戲和多媒體應…

    編程 2025-04-28
  • Python爬取網頁信息

    本文將從多個方面對Python爬取網頁信息做詳細的闡述。 一、爬蟲介紹 爬蟲是一種自動化程序,可以模擬人對網頁進行訪問獲取信息的行為。通過編寫代碼,我們可以指定要獲取的信息,將其從…

    編程 2025-04-28

發表回復

登錄後才能評論