回 帖 发 新 帖 刷新版面

主题:求救  login 页面

刚开始学jsp哦


我做了个index.jsp,想给它加个login.jsp控制;
现在从login.jsp可以重定向到index.jsp了,

如何可以限制 必须经过login.jsp呢??
使用全局变量吗??
如何实现??请高手指教,谢谢; 

ps:我从 login.jsp转到index.jsp了,突然发现直接输入index.jsp就可以显示主页,那我的login.jsp岂不是没用了,真败了

回复列表 (共6个回复)

沙发

我试着用全局变量:

package loginUsing;
import java.io.*;

public class Loginer
{
    public Loginer()
    {
        this.logined=false;
    }
    private boolean logined;

    public void setLogined(boolean flag)
    {
        this.logined=flag;
    }
}

类放在D:\tomcat\common\classes\loginUsing  下;
login.jsp 这样写的:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
[color=008080][color=0000FF][color=FF0000]<%@page import="loginUsing.Loginer" %>[/color][/color][/color]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>登录页面</title>
</head>

[color=00FF00][color=00FFFF][color=FF00FF][color=C0C0C0][color=FF0000]<jsp:useBean id="test" class="Loginer" scope="session">
</jsp:useBean<% boolean loginedFlag=false;//全局变量
%>

<% test.setLogined(loginedFlag);%>
>[/color][/color][/color][/color][/color]

<% //<META  HTTP-EQUIV="refresh"  CONTENT="60">//页面刷新,备份在这里
String goodUser = "admin";
String goodPass = "admin";
HttpSession mySession = request.getSession(true);
String errorMessage = "请先登录";
boolean loginAttempt = false;
String myMethod = request.getMethod();
mySession.putValue("requestedUrl", "http://localhost:8080/app/index.jsp");
//out.println((String)mySession.getValue("requestedUrl"));//调试
if(request.getParameterValues("click") != null //&& request.getParameterValues("click")[0].trim().equals("确认")
)
{ loginAttempt = true; 
//out.println(loginAttempt);//调试
}
if (loginAttempt)
{
    String username = request.getParameterValues("user")[0].trim();
    String password = request.getParameterValues("pass")[0].trim();
    //out.println("username = /" + username + "/ & password = /" + password + "/<BR>");//调试
    if (goodUser.equals(username) && goodPass.equals(password))
    {
        response.sendRedirect((String)mySession.getValue("requestedUrl"));
        mySession.putValue("hasPermission", new Boolean(true));
        loginedFlag=true;
       [color=FF0000] test.setLogined(loginedFlag);[/color]
        errorMessage = "Unable to redirect: " 
+ (String)mySession.getValue("requestedUrl");
    }
    else
    {
        errorMessage = "请输入正确的用户名和口令";
    }
}
else
{
    errorMessage = "请先登录";
    if (mySession.getValue("requestedUrl") == null)
    {
        mySession.putValue("requestedUrl", "http://localhost:8080/app/login.jsp");
    }
    //out.println("set userReferrer to " + mySession.getValue("redirectTo") + "<BR>");
}
%>
<CENTER>
<font color=red><%=errorMessage%></font>
<TABLE align=center>
<FORM action="login.jsp" method=post name="login">
<TR><TD>用户:</TD><TD colspan="2"><input type=text name=user value=""></TD></TR>
<TR><TD>密码:</TD><TD colspan="2"><input type=password name=pass value=""></TD></TR>
<TR>
<td></td>
<TD><input type=submit name=click value="确认"></TD>
<TD> <input name="" type="reset" value="重置" ></TD>
</TR>
</FORM>
</TABLE>
</CENTER>
<script language="Javascript">
// <!--
if (document.forms.login != null)
document.forms.login.user.focus();
// -->
</script>

//////////////////////////////////////////////////////////////////

板凳

类的编译没有问题,可是访问login.jsp页面的时候提示exception;

javax.servlet.ServletException: Loginer
    at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:498)
    at org.apache.jsp.login_jsp._jspService(login_jsp.java:115)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:92)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:809)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:162)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:240)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:187)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:809)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:198)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:144)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:209)
    at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:595)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:432)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:954)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:138)
    at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:595)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:432)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:954)
    at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2459)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:132)
    at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:595)
    at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:118)
    at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:593)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:116)
    at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:593)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:432)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:954)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:126)
    at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:595)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:432)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:954)
    at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:152)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
    at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
    at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
    at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
    at java.lang.Thread.run(Thread.java:619)

3 楼

root cause:

java.lang.InstantiationException: Loginer
    at org.apache.jsp.login_jsp._jspService(login_jsp.java:51)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:92)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:809)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:162)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:240)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:187)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:809)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:198)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:144)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:209)
    at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:595)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:432)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:954)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:138)
    at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:595)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:432)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:954)
    at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2459)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:132)
    at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:595)
    at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:118)
    at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:593)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:116)
    at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:593)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:432)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:954)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:126)
    at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:595)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:432)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:954)
    at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:152)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
    at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
    at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
    at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
    at java.lang.Thread.run(Thread.java:619)

4 楼

who can help me ??????????

5 楼

自己搞定了,谢谢;


是这里不对;

<jsp:useBean id="test" class="Loginer" scope="session">
</jsp:useBean<% boolean loginedFlag=false;//全局变量
%>

应改为:class="package.Loginer"

6 楼

简单一点,用session

我来回复

您尚未登录,请登录后再回复。点此登录或注册