在jsp中,如何实现普通用户和管理员登陆后跳转到不同的点jsp页面
我是新手,虽然分不多但能说的细致一些吗?我的代码如下,但是只能跳跳转到Welcome.jsp:LoginServlet.java的主要部分如下://核对用户名和密码,决定...
我是新手,虽然分不多但能说的细致一些吗?
我的代码如下,但是只能跳跳转到Welcome.jsp:
LoginServlet.java的主要部分如下:
//核对用户名和密码,决定是否通过登陆验证
DB_User dbuser = new DB_User();
User user = dbuser.get(UserName, EnPwd);
int userid=0;
if (user != null) {
// 登录成功时,把strUserName设成session变量UserName
userid=user.getId();
request.getSession(true).setAttribute("userid",String.valueOf(userid));
request.getSession(true).setAttribute("username", UserName);
String name = request.getSession(false).getAttribute("username").toString();
if( name.equals("admin") )
{
try {
request.getRequestDispatcher("./Welcome1.jsp").forward(request,response);
}
catch (Throwable t) {
getServletContext().log(t.getMessage());
}
}
else {
try{
request.getRequestDispatcher("./Welcome.jsp").forward(request,response);
}
catch (Throwable t) {
getServletContext().log(t.getMessage());
}
}
} else {
// 否则(记录集为空),表明失败:
request.setAttribute("error", "用户名与密码不匹配,登录失败,请重试!");
try {
request.getRequestDispatcher("./Login.jsp").forward(request, response);
}
catch (Throwable t) {
getServletContext().log(t.getMessage());
}
}
}
}
Login.jsp的主要部分如下:
<form name="login" method="POST" action="./LoginServlet">
<tr>
<td align=right height=32 width=45%>用户名:</td>
<td width="55%"><input type="hidden" name="returl" value="">
<input type="text" name="logname" value="">
</td>
</tr>
<tr>
<td align=right height=32>密码:</td>
<td><input type="password" name="logpwd"></td>
</tr>
<tr>
<td align=right height=32>验证码:</td>
<td><input type="text" name="random" value="">
<image src="./Random.jsp">
</td>
</tr>
<tr>
<td align=center colspan=2 height=32>
<input type="submit" name="login" value="登 录">
<input type="reset" name="reset" value="重 置">
</td>
</tr>
</form> 展开
我的代码如下,但是只能跳跳转到Welcome.jsp:
LoginServlet.java的主要部分如下:
//核对用户名和密码,决定是否通过登陆验证
DB_User dbuser = new DB_User();
User user = dbuser.get(UserName, EnPwd);
int userid=0;
if (user != null) {
// 登录成功时,把strUserName设成session变量UserName
userid=user.getId();
request.getSession(true).setAttribute("userid",String.valueOf(userid));
request.getSession(true).setAttribute("username", UserName);
String name = request.getSession(false).getAttribute("username").toString();
if( name.equals("admin") )
{
try {
request.getRequestDispatcher("./Welcome1.jsp").forward(request,response);
}
catch (Throwable t) {
getServletContext().log(t.getMessage());
}
}
else {
try{
request.getRequestDispatcher("./Welcome.jsp").forward(request,response);
}
catch (Throwable t) {
getServletContext().log(t.getMessage());
}
}
} else {
// 否则(记录集为空),表明失败:
request.setAttribute("error", "用户名与密码不匹配,登录失败,请重试!");
try {
request.getRequestDispatcher("./Login.jsp").forward(request, response);
}
catch (Throwable t) {
getServletContext().log(t.getMessage());
}
}
}
}
Login.jsp的主要部分如下:
<form name="login" method="POST" action="./LoginServlet">
<tr>
<td align=right height=32 width=45%>用户名:</td>
<td width="55%"><input type="hidden" name="returl" value="">
<input type="text" name="logname" value="">
</td>
</tr>
<tr>
<td align=right height=32>密码:</td>
<td><input type="password" name="logpwd"></td>
</tr>
<tr>
<td align=right height=32>验证码:</td>
<td><input type="text" name="random" value="">
<image src="./Random.jsp">
</td>
</tr>
<tr>
<td align=center colspan=2 height=32>
<input type="submit" name="login" value="登 录">
<input type="reset" name="reset" value="重 置">
</td>
</tr>
</form> 展开
2016-03-09 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
在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);
}
}
}
参考代码如下:
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);
}
}
}
展开全部
用户表中应该有字段标识用户权限,登陆后,从数据库取出这个字段,用它来判断跳转。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
String name = request.getSession(false).getAttribute("username").toString();
删除 。
if( name.equals("admin") )
改成:
if( UserName.equals("admin") )
如果还不行,你打印一下username看看是admin吗?注意大小写和空格
删除 。
if( name.equals("admin") )
改成:
if( UserName.equals("admin") )
如果还不行,你打印一下username看看是admin吗?注意大小写和空格
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
照我这个做就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");
}
%>
<%
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");
}
%>
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<%
if(session.getAttribute("login")=="admin")
{
response.sendRedirect("login.jsp");
}
else if(session.getAttribute("login")=="user")) {
response.sendRedirect("XXX.jsp");
}
%>
if(session.getAttribute("login")=="admin")
{
response.sendRedirect("login.jsp");
}
else if(session.getAttribute("login")=="user")) {
response.sendRedirect("XXX.jsp");
}
%>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询