本文目录一览:
在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.