我有一个仪表板,在其中可以获取会话属性以显示一些数据。
ApplicantBean currentApplicant = (ApplicantBean) session.getAttribute("currentApplicant");
但是过一会儿,如果刷新仪表板页面,它将显示错误java.lang.NullPointerException
。我该如何做,以便如果会话过期,它将重定向到登录页面?我已经尝试过下面的代码,但是什么也没发生。
<%
if (session != null) {
if (session.getAttribute("currentApplicant") != null) {
ApplicantBean currentApplicant = (ApplicantBean) session.getAttribute("currentApplicant");
}
else {
response.sendRedirect("appLogin.jsp");
}
}
%>
下面是我的代码:
appDashboard.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import="com.ptj.jdbc.ApplicantBean" import="com.ptj.jdbc.ConnectionManager"
import="java.sql.*" import="java.io.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>PTJ | Dashboard</title>
<link href="css/bootstrap/bootstrap.min.css" rel="stylesheet">
<link href="css/font-awesome/css/font-awesome.css" rel="stylesheet">
<link href="css/animate.css" rel="stylesheet">
<link href="css/main.css" rel="stylesheet">
</head>
<body class="fixed-sidebar">
<%
ApplicantBean currentApplicant = (ApplicantBean) session.getAttribute("currentApplicant");
%>
<div id="wrapper">
<nav class="navbar-default navbar-static-side" role="navigation">
<div class="sidebar-collapse">
<ul class="nav metismenu" id="side-menu">
<li class="nav-header">
<div class="dropdown profile-element">
<span> <img alt="image" class="img-circle"
src="img/<%=currentApplicant.getImage()%>" />
</span> <a data-toggle="dropdown" class="dropdown-toggle" href="#">
<span class="clear"> <span class="block m-t-xs"> <strong
class="font-bold"><%=currentApplicant.getFullname()%></strong>
</span> <span class="text-muted text-xs block">Art Director <b
class="caret"></b></span>
</span>
</a>
<ul class="dropdown-menu animated fadeInRight m-t-xs">
<li><a href="appProfile.jsp">Profile</a></li>
<li class="divider"></li>
<li><a href="index.jsp">Logout</a></li>
</ul>
</div>
<div class="logo-element">IN+</div>
</li>
<li class="active"><a><i class="fa fa-th-large"></i> <span
class="nav-label">Dashboards</span> <span class="fa arrow"></span></a>
<ul class="nav nav-second-level collapse">
<li class="active"><a href="appDashboard.jsp">Dashboard</a></li>
<li><a href="appProfile.jsp">Profile</a></li>
<li><a href="appSetting.jsp">Setting</a></li>
<li><a href="blank.jsp">Blank Page</a></li>
</ul></li>
</ul>
</div>
</nav>
<div id="page-wrapper" class="gray-bg">
<div class="row border-bottom">
<nav class="navbar navbar-static-top " role="navigation"
style="margin-bottom: 0">
<div class="navbar-header">
<a class="navbar-minimalize minimalize-styl-2 btn btn-primary "
href="#"><i class="fa fa-bars"></i> </a>
</div>
<ul class="nav navbar-top-links navbar-right"
style="margin-top: 11px">
<li>
<form action="Logout" method="post">
<button class="btn btn-sm btn-white" type="submit">
<i class="fa fa-sign-out"></i> Log out
</button>
</form>
</li>
</ul>
</nav>
</div>
<div class="row wrapper border-bottom white-bg page-heading">
<div class="col-sm-4">
<h2>Available Job</h2>
</div>
</div>
<div class="wrapper wrapper-content animated fadeInRight">
<div class="row">
content here
</div>
</div>
<div class="footer">
<div class="pull-right">
<!-- 10GB of <strong>250GB</strong> Free. -->
</div>
<div>
<strong>Copyright</strong> PTJ © 2018
</div>
</div>
</div>
</div>
<!-- Mainly scripts -->
<script src="js/jquery-3.1.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/plugins/metisMenu/jquery.metisMenu.js"></script>
<script src="js/plugins/slimscroll/jquery.slimscroll.min.js"></script>
<!-- Custom and plugin javascript -->
<script src="js/inspinia.js"></script>
<script src="js/plugins/pace/pace.min.js"></script>
ApplicantLoginServlet.java
package com.ptj.jdbc;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class AppLoginServlet
*/
@WebServlet("/AppLogin")
public class ApplicantLoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
ApplicantBean applicant = new ApplicantBean();
applicant.setUsername(request.getParameter("appusername"));
applicant.setPassword(request.getParameter("apppassword"));
applicant = ApplicantDA.Login(applicant);
if(applicant.isValid()) {
HttpSession session = request.getSession(true); //creating session
session.setAttribute("currentApplicant", applicant);
//session.setMaxInactiveInterval(30); //30 seconds
PrintWriter out = response.getWriter();
int id = applicant.getID();
String firstTime = applicant.getFirstTime();
if(firstTime.equalsIgnoreCase("yes") ) {
response.setContentType("text/html");
response.setContentType("text/html");
out.println("<center>Successfully Logged In!<br>Redirect in 5 seconds...</center>");
out.println("<script type=\"text/javascript\">");
out.println("setTimeout(function(){");
out.println("window.location= \"appSetup.jsp\";");
out.println("}, 5000);");
out.println("</script>");
System.out.println("ID : " + id);
}
else {
response.setContentType("text/html");
response.setContentType("text/html");
out.println("<center>Successfully Logged In!<br>Redirect in 5 seconds...</center>");
out.println("<script type=\"text/javascript\">");
out.println("setTimeout(function(){");
out.println("window.location= \"appDashboard.jsp\";");
out.println("}, 5000);");
out.println("</script>");
}
//response.sendRedirect("appDashboard.jsp");
}
else {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
out.println("<script type=\"text/javascript\">");
out.println("alert('Sorry, invalid username or password.');");
out.println("window.location= \"appLogin.jsp\"");
out.println("</script>");
//response.sendRedirect("InvalidLogin.jsp");
}
}
catch (Throwable theException) {
System.out.println(theException);
}
}
}