在jsp中,如何实现普通用户和管理员登陆后跳转到不同的点jsp页面
发布网友
发布时间:2022-05-05 01:58
我来回答
共5个回答
热心网友
时间:2023-10-09 09:47
在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);
}
}
}
热心网友
时间:2023-10-09 09:47
用户表中应该有字段标识用户权限,登陆后,从数据库取出这个字段,用它来判断跳转。
热心网友
时间:2023-10-09 09:48
String name = request.getSession(false).getAttribute("username").toString();
删除 。
if( name.equals("admin") )
改成:
if( UserName.equals("admin") )
如果还不行,你打印一下username看看是admin吗?注意大小写和空格
热心网友
时间:2023-10-09 09:48
照我这个做就OK,祝你好运,呵呵
<%
ResultSet rs=null;
request.setCharacterEncoding("UTF-8");
String name=request.getParameter("username");
String password=request.getParameter("userpassword");
String check=request.getParameter("rand");
String rand=((String)session.getAttribute("check"));
String s=request.getParameter("selectlogrole");
try
{
if(s!=null)
{switch(s.charAt(0))
{
case's':
String sql01="select * from userinfo where username='"+name+"' and userpassword='"+password+"' and logrole='student'";
rs=query.executeQuery(sql01);
if(rs.next()&&rand.equals(check))
{
String Name=rs.getString("username");
session.setAttribute("name",Name);//记录用户名
session.setAttribute("login","success");//记录成功登录
%>
<script language="javascript">
alert("登录成功!!!");
window.location.href="sss.jsp";
</script>
<%
}else
{
%>
<script language="javascript">
alert("身份错误!!");
history.back();
</script>
<% }
break;
case't':
String sql02="select * from userinfo where username='"+name+"' and userpassword='"+password+"' and logrole='teacher'";
rs=query.executeQuery(sql02);
if(rs.next()&&rand.equals(check))
{
String Name=rs.getString("username");
session.setAttribute("name",Name);//记录用户名
session.setAttribute("login","success");//记录成功登录
%>
<script language="javascript">
alert("登录成功!!!");
window.location.href="ttt.jsp";
</script>
<%
}else
{
%>
<script language="javascript">
alert("身份错误!!");
history.back();
</script>
<%
}
break;
case'a':
String sql03="select * from userinfo where username='"+name+"' and userpassword='"+password+"' and logrole='admin'";
rs=query.executeQuery(sql03);
if(rs.next()&&rand.equals(check))
{
String Name=rs.getString("username");
session.setAttribute("name",Name);//记录用户名
session.setAttribute("login","success");//记录成功登录
%>
<script language="javascript">
alert("登录成功!!!");
window.location.href="ggg.jsp";
</script>
<%
}else
{
%>
<script language="javascript">
alert("身份错误!!");
history.back();
</script>
<%
}
break;
default:
out.println("你没有选择");
}
}
%>
<%
}catch(Exception e)
{
out.println("连接异常login_config");
}
%>
热心网友
时间:2023-10-09 09:49
<%
if(session.getAttribute("login")=="admin")
{
response.sendRedirect("login.jsp");
}
else if(session.getAttribute("login")=="user")) {
response.sendRedirect("XXX.jsp");
}
%>