在jsp中,如何实现普通用户和管理员登陆后跳转到不同的点jsp页面
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在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);
参考技术A 直接使用页面跳转标签就可以了,如果用servlet会稍微复杂一点
Django里自定义用户登陆及登陆后跳转到登陆前页面的实现
def logout(request): request.session.flush() return HttpResponseRedirect(request.META.get(‘HTTP_REFERER‘, ‘/‘)) class LoginView(View): def get(self,request): request.session[‘login_from‘] = request.META.get(‘HTTP_REFERER‘, ‘/‘) return render(request,‘login.html‘) def post(self,request): username=request.POST.get(‘username‘,None) password=request.POST.get(‘password‘,None) try: user=authenticate(request,username=username,password=password) except: user=None if user is not None: login(request,user) request.session[‘is_login‘] = True request.session[‘user_id‘] = str(user.id) request.session[‘user_name‘] = str(user) return HttpResponseRedirect(request.session[‘login_from‘]) else: return HttpResponse(u‘密码不对或者不存在‘)
以上是关于在jsp中,如何实现普通用户和管理员登陆后跳转到不同的点jsp页面的主要内容,如果未能解决你的问题,请参考以下文章